Skip to content
50 changes: 50 additions & 0 deletions firebaseai/src/Candidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@ public enum FinishReason
/// Token generation was stopped because the function call generated by the model was invalid.
/// </summary>
MalformedFunctionCall,
/// <summary>
/// Token generation stopped because generated images contain safety violations.
/// </summary>
ImageSafety,
/// <summary>
/// Image generation stopped because generated images have other prohibited content.
/// </summary>
ImageProhibitedContent,
/// <summary>
/// Image generation stopped because of other miscellaneous issue.
/// </summary>
ImageOther,
/// <summary>
/// The model was expected to generate an image, but none was generated.
/// </summary>
NoImage,
/// <summary>
/// Image generation stopped due to recitation.
/// </summary>
ImageRecitation,
/// <summary>
/// The response candidate content was flagged for using an unsupported language.
/// </summary>
Language,
/// <summary>
/// Model generated a tool call but no tools were enabled in the request.
/// </summary>
UnexpectedToolCall,
/// <summary>
/// Model called too many tools consecutively, thus the system exited execution.
/// </summary>
TooManyToolCalls,
/// <summary>
/// Request has at least one thought signature missing.
/// </summary>
MissingThoughtSignature,
/// <summary>
/// Finished due to malformed response.
/// </summary>
MalformedResponse,
}

/// <summary>
Expand Down Expand Up @@ -137,6 +177,16 @@ private static FinishReason ParseFinishReason(string str)
"PROHIBITED_CONTENT" => Firebase.AI.FinishReason.ProhibitedContent,
"SPII" => Firebase.AI.FinishReason.SPII,
"MALFORMED_FUNCTION_CALL" => Firebase.AI.FinishReason.MalformedFunctionCall,
"IMAGE_SAFETY" => Firebase.AI.FinishReason.ImageSafety,
"IMAGE_PROHIBITED_CONTENT" => Firebase.AI.FinishReason.ImageProhibitedContent,
"IMAGE_OTHER" => Firebase.AI.FinishReason.ImageOther,
"NO_IMAGE" => Firebase.AI.FinishReason.NoImage,
"IMAGE_RECITATION" => Firebase.AI.FinishReason.ImageRecitation,
"LANGUAGE" => Firebase.AI.FinishReason.Language,
"UNEXPECTED_TOOL_CALL" => Firebase.AI.FinishReason.UnexpectedToolCall,
"TOO_MANY_TOOL_CALLS" => Firebase.AI.FinishReason.TooManyToolCalls,
"MISSING_THOUGHT_SIGNATURE" => Firebase.AI.FinishReason.MissingThoughtSignature,
"MALFORMED_RESPONSE" => Firebase.AI.FinishReason.MalformedResponse,
_ => Firebase.AI.FinishReason.Unknown,
};
}
Expand Down
9 changes: 8 additions & 1 deletion firebaseai/src/GenerationConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public readonly struct GenerationConfig
private readonly JsonSchema _responseJsonSchema;
private readonly List<ResponseModality> _responseModalities;
private readonly ThinkingConfig? _thinkingConfig;
private readonly ImageConfig? _imageConfig;

/// <summary>
/// Creates a new `GenerationConfig` value.
Expand Down Expand Up @@ -168,6 +169,9 @@ public readonly struct GenerationConfig
/// An error will be returned if this field is set for models that don't
/// support thinking.
/// </param>
/// <param name="imageConfig">
/// Configuration for the aspect ratio and size of generated images.
/// </param>
public GenerationConfig(
float? temperature = null,
float? topP = null,
Expand All @@ -181,7 +185,8 @@ public GenerationConfig(
Schema responseSchema = null,
JsonSchema responseJsonSchema = null,
IEnumerable<ResponseModality> responseModalities = null,
ThinkingConfig? thinkingConfig = null)
ThinkingConfig? thinkingConfig = null,
ImageConfig? imageConfig = null)
{
_temperature = temperature;
_topP = topP;
Expand All @@ -197,6 +202,7 @@ public GenerationConfig(
_responseModalities = responseModalities != null ?
new List<ResponseModality>(responseModalities) : null;
_thinkingConfig = thinkingConfig;
_imageConfig = imageConfig;
}

/// <summary>
Expand All @@ -223,6 +229,7 @@ internal Dictionary<string, object> ToJson()
_responseModalities.Select(EnumConverters.ResponseModalityToString).ToList();
}
if (_thinkingConfig != null) jsonDict["thinkingConfig"] = _thinkingConfig?.ToJson();
if (_imageConfig != null) jsonDict["imageConfig"] = _imageConfig?.ToJson();
Comment thread
paulb777 marked this conversation as resolved.
Outdated

return jsonDict;
}
Expand Down
109 changes: 109 additions & 0 deletions firebaseai/src/ImageConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Collections.Generic;

namespace Firebase.AI
{
/// <summary>
/// Configuration options for generating images with Gemini models.
/// </summary>
public readonly struct ImageConfig
{
/// <summary>
/// The aspect ratio of generated images.
/// </summary>
public enum AspectRatio
{
Square1x1,
Portrait9x16,
Landscape16x9,
Portrait3x4,
Landscape4x3,
Portrait2x3,
Landscape3x2,
Portrait4x5,
Landscape5x4,
Portrait1x4,
Landscape4x1,
Portrait1x8,
Landscape8x1,
Ultrawide21x9
}

/// <summary>
/// The size of images to generate.
/// </summary>
public enum ImageSize
{
Size512,
Size1K,
Size2K,
Size4K
}

public AspectRatio? Ratio { get; }
public ImageSize? Size { get; }

public ImageConfig(AspectRatio? aspectRatio = null, ImageSize? imageSize = null)
{
Ratio = aspectRatio;
Size = imageSize;
}

internal static string ConvertAspectRatio(AspectRatio aspectRatio)
{
return aspectRatio switch
{
AspectRatio.Square1x1 => "1:1",
AspectRatio.Portrait9x16 => "9:16",
AspectRatio.Landscape16x9 => "16:9",
AspectRatio.Portrait3x4 => "3:4",
AspectRatio.Landscape4x3 => "4:3",
AspectRatio.Portrait2x3 => "2:3",
AspectRatio.Landscape3x2 => "3:2",
AspectRatio.Portrait4x5 => "4:5",
AspectRatio.Landscape5x4 => "5:4",
AspectRatio.Portrait1x4 => "1:4",
AspectRatio.Landscape4x1 => "4:1",
AspectRatio.Portrait1x8 => "1:8",
AspectRatio.Landscape8x1 => "8:1",
AspectRatio.Ultrawide21x9 => "21:9",
_ => aspectRatio.ToString(),
Comment thread
paulb777 marked this conversation as resolved.
Outdated
};
}

internal static string ConvertImageSize(ImageSize imageSize)
{
return imageSize switch
{
ImageSize.Size512 => "512",
ImageSize.Size1K => "1K",
ImageSize.Size2K => "2K",
ImageSize.Size4K => "4K",
_ => imageSize.ToString(),
Comment thread
paulb777 marked this conversation as resolved.
Outdated
};
}

internal Dictionary<string, object> ToJson()
{
Dictionary<string, object> jsonDict = new();
if (Ratio.HasValue) jsonDict["aspectRatio"] = ConvertAspectRatio(Ratio.Value);
if (Size.HasValue) jsonDict["imageSize"] = ConvertImageSize(Size.Value);
return jsonDict;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ protected override void Start()
TestReadSecureFile,
// Internal tests for Json parsing, requires using a source library.
InternalTestBasicReplyShort,
InternalTestFinishReasonExpanded,
InternalTestImageConfigSerialization,
InternalTestCitations,
InternalTestBlockedSafetyWithMessage,
InternalTestFinishReasonSafetyNoContent,
Expand Down Expand Up @@ -1380,6 +1382,59 @@ async Task InternalTestBasicReplyShort()
ValidateUsageMetadata(response.UsageMetadata, 6, 7, 0, 0, 13);
}

// Test that parsing a response with expanded FinishReason works.
async Task InternalTestFinishReasonExpanded()
{
string jsonStr = @"{
""candidates"": [{
""content"": {
""parts"": [{""text"": ""Hello""}],
""role"": ""model""
},
""finishReason"": ""IMAGE_SAFETY""
}]
}";
Dictionary<string, object> json = (Dictionary<string, object>)Json.Deserialize(jsonStr);
GenerateContentResponse response = GenerateContentResponse.FromJson(json, FirebaseAI.Backend.InternalProvider.VertexAI);

Assert(""Response missing candidates."", response.Candidates.Any());
Candidate candidate = response.Candidates.First();
AssertEq(""FinishReason"", candidate.FinishReason, FinishReason.ImageSafety);

// Test another one
jsonStr = @"{
""candidates"": [{
""content"": {""parts"": []},
""finishReason"": ""MALFORMED_RESPONSE""
}]
}";
json = (Dictionary<string, object>)Json.Deserialize(jsonStr);
response = GenerateContentResponse.FromJson(json, FirebaseAI.Backend.InternalProvider.VertexAI);
candidate = response.Candidates.First();
AssertEq(""FinishReason"", candidate.FinishReason, FinishReason.MalformedResponse);
}

// Test that ImageConfig serialization works as expected.
Task InternalTestImageConfigSerialization()
{
var imageConfig = new ImageConfig(ImageConfig.AspectRatio.Landscape16x9, ImageConfig.ImageSize.Size1K);
var json = imageConfig.ToJson();

AssertEq(""ImageConfig.aspectRatio"", json[""aspectRatio""], ""16:9"");
AssertEq(""ImageConfig.imageSize"", json[""imageSize""], ""1K"");

var genConfig = new GenerationConfig(imageConfig: imageConfig);
var genJson = genConfig.ToJson();

Assert(""GenerationConfig missing imageConfig"", genJson.ContainsKey(""imageConfig""));
var imageConfigJson = genJson[""imageConfig""] as Dictionary<string, object>;
Assert(""imageConfig is not a dictionary"", imageConfigJson != null);
AssertEq(""imageConfigJson.aspectRatio"", imageConfigJson[""aspectRatio""], ""16:9"");
AssertEq(""imageConfigJson.imageSize"", imageConfigJson[""imageSize""], ""1K"");

return Task.CompletedTask;
}

// Test that parsing a response including Citations works.
// https://github.com/FirebaseExtended/vertexai-sdk-test-data/blob/main/mock-responses/unary-success-citations.json
async Task InternalTestCitations()
Expand Down
Loading