Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Release Notes
- Firebase AI: All Imagen models are deprecated and will shut down as early
as June 2026. As a replacement, you can
[migrate your apps to use Gemini Image models (the "Nano Banana" models)](https://firebase.google.com/docs/ai-logic/imagen-models-migration).
- Firebase AI: Add support for the JsonSchema formatting.

### 13.9.0
- Changes
Expand Down
35 changes: 33 additions & 2 deletions firebaseai/src/FunctionCalling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public readonly struct FunctionDeclaration
private string Name { get; }
private string Description { get; }
private Schema Parameters { get; }
private JsonSchema JsonParameters { get; }

/// <summary>
/// Constructs a new `FunctionDeclaration`.
Expand All @@ -57,6 +58,26 @@ public FunctionDeclaration(string name, string description,
Name = name;
Description = description;
Parameters = Schema.Object(parameters, optionalParameters);
JsonParameters = null;
Comment thread
a-maurice marked this conversation as resolved.
}

/// <summary>
/// Constructs a new `FunctionDeclaration`.
/// </summary>
/// <param name="name">The name of the function; must be a-z, A-Z, 0-9, or contain
/// underscores and dashes, with a maximum length of 63.</param>
/// <param name="description">A brief description of the function.</param>
/// <param name="parameters">Describes the parameters to this function.</param>
/// <param name="optionalParameters">The names of parameters that may be omitted by the model
/// in function calls; by default, all parameters are considered required.</param>
public FunctionDeclaration(string name, string description,
IDictionary<string, JsonSchema> parameters,
IEnumerable<string> optionalParameters = null)
{
Name = name;
Description = description;
Parameters = null;
JsonParameters = JsonSchema.Object(parameters, optionalParameters);
}

/// <summary>
Expand All @@ -65,11 +86,21 @@ public FunctionDeclaration(string name, string description,
/// </summary>
internal Dictionary<string, object> ToJson()
{
return new() {
var json = new Dictionary<string, object>() {
{ "name", Name },
{ "description", Description },
{ "parameters", Parameters.ToJson() },
};
// Only one of these will likely be set, but just check
Comment thread
a-maurice marked this conversation as resolved.
if (JsonParameters != null)
{
json["parametersJsonSchema"] = JsonParameters.ToJson();
}
if (Parameters != null)
{
json["parameters"] = Parameters.ToJson();
}

return json;
}
}

Expand Down
16 changes: 15 additions & 1 deletion firebaseai/src/GenerationConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public readonly struct GenerationConfig
private readonly string[] _stopSequences;
private readonly string _responseMimeType;
private readonly Schema _responseSchema;
private readonly JsonSchema _responseJsonSchema;
private readonly List<ResponseModality> _responseModalities;
private readonly ThinkingConfig? _thinkingConfig;

Expand Down Expand Up @@ -133,7 +134,7 @@ public readonly struct GenerationConfig
/// `responseSchema`.</param>
///
/// <param name="responseSchema">Output schema of the generated candidate text. If set, a compatible
/// `responseMimeType` must also be set.
/// `responseMimeType` must also be set. Note that if this is set, responseJsonSchema should be left null.
///
/// Compatible MIME types:
/// - `application/json`: Schema for JSON response.
Expand All @@ -142,6 +143,16 @@ public readonly struct GenerationConfig
/// [Control generated output](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/control-generated-output)
/// guide for more details.</param>
///
/// <param name="responseJsonSchema">Output JSON schema of the generated candidate text. If set, a compatible
/// `responseMimeType` must also be set. Note that if this is set, responseSchema should be left null.
///
/// Compatible MIME types:
/// - `application/json`: JsonSchema for JSON response.
///
/// Refer to the
/// [Control generated output](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/control-generated-output)
/// guide for more details.</param>
///
/// <param name="responseModalities">
/// The data types (modalities) that may be returned in model responses.
///
Expand All @@ -168,6 +179,7 @@ public GenerationConfig(
string[] stopSequences = null,
string responseMimeType = null,
Schema responseSchema = null,
JsonSchema responseJsonSchema = null,
Comment thread
a-maurice marked this conversation as resolved.
IEnumerable<ResponseModality> responseModalities = null,
ThinkingConfig? thinkingConfig = null)
{
Expand All @@ -181,6 +193,7 @@ public GenerationConfig(
_stopSequences = stopSequences;
_responseMimeType = responseMimeType;
_responseSchema = responseSchema;
_responseJsonSchema = responseJsonSchema;
_responseModalities = responseModalities != null ?
new List<ResponseModality>(responseModalities) : null;
_thinkingConfig = thinkingConfig;
Expand All @@ -203,6 +216,7 @@ internal Dictionary<string, object> ToJson()
if (_stopSequences != null && _stopSequences.Length > 0) jsonDict["stopSequences"] = _stopSequences;
if (!string.IsNullOrWhiteSpace(_responseMimeType)) jsonDict["responseMimeType"] = _responseMimeType;
if (_responseSchema != null) jsonDict["responseSchema"] = _responseSchema.ToJson();
if (_responseJsonSchema != null) jsonDict["responseJsonSchema"] = _responseJsonSchema.ToJson();
if (_responseModalities != null && _responseModalities.Count > 0)
{
jsonDict["responseModalities"] =
Expand Down
Loading
Loading