Skip to content

[Firebase AI] Change public fields to easier types #1290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2025
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
2 changes: 2 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ Release Notes
- Firebase AI: Add support for defining a Thinking budget.
- Firebase AI: Deprecated `CountTokensResponse.TotalBillableCharacters`, use
`CountTokensResponse.TotalTokens` instead.
- Firebase AI: Changed public field types for ReadOnlyMemory<byte> to byte[],
and IEnumerable to IReadOnlyList.
- Messaging: Removed deprecated `FirebaseMessage.Dispose`,
`FirebaseNotification.Dispose`, and `MessagingOptions.Dispose` methods.

Expand Down
11 changes: 4 additions & 7 deletions firebaseai/src/Candidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Firebase.AI.Internal;

namespace Firebase.AI {
Expand Down Expand Up @@ -73,7 +70,7 @@ public enum FinishReason {
/// Each content generation prompt may produce multiple candidate responses.
/// </summary>
public readonly struct Candidate {
private readonly ReadOnlyCollection<SafetyRating> _safetyRatings;
private readonly IReadOnlyList<SafetyRating> _safetyRatings;

/// <summary>
/// The response’s content.
Expand All @@ -83,9 +80,9 @@ public readonly struct Candidate {
/// <summary>
/// The safety rating of the response content.
/// </summary>
public IEnumerable<SafetyRating> SafetyRatings {
public IReadOnlyList<SafetyRating> SafetyRatings {
get {
return _safetyRatings ?? new ReadOnlyCollection<SafetyRating>(new List<SafetyRating>());
return _safetyRatings ?? new List<SafetyRating>();
}
}

Expand All @@ -110,7 +107,7 @@ private Candidate(ModelContent content, List<SafetyRating> safetyRatings,
FinishReason? finishReason, CitationMetadata? citationMetadata,
GroundingMetadata? groundingMetadata) {
Content = content;
_safetyRatings = new ReadOnlyCollection<SafetyRating>(safetyRatings ?? new List<SafetyRating>());
_safetyRatings = safetyRatings ?? new List<SafetyRating>();
FinishReason = finishReason;
CitationMetadata = citationMetadata;
GroundingMetadata = groundingMetadata;
Expand Down
7 changes: 1 addition & 6 deletions firebaseai/src/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
Expand All @@ -36,11 +35,7 @@ public class Chat {
/// The previous content from the chat that has been successfully sent and received from the
/// model. This will be provided to the model for each message sent as context for the discussion.
/// </summary>
public IEnumerable<ModelContent> History {
get {
return new ReadOnlyCollection<ModelContent>(chatHistory);
}
}
public IReadOnlyList<ModelContent> History => chatHistory;

// Note: No public constructor, get one through GenerativeModel.StartChat
private Chat(GenerativeModel model, IEnumerable<ModelContent> initialHistory) {
Expand Down
9 changes: 4 additions & 5 deletions firebaseai/src/Citation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Firebase.AI.Internal;

namespace Firebase.AI {
Expand All @@ -25,20 +24,20 @@ namespace Firebase.AI {
/// A collection of source attributions for a piece of content.
/// </summary>
public readonly struct CitationMetadata {
private readonly ReadOnlyCollection<Citation> _citations;
private readonly IReadOnlyList<Citation> _citations;

/// <summary>
/// A list of individual cited sources and the parts of the content to which they apply.
/// </summary>
public IEnumerable<Citation> Citations {
public IReadOnlyList<Citation> Citations {
get {
return _citations ?? new ReadOnlyCollection<Citation>(new List<Citation>());
return _citations ?? new List<Citation>();
}
}

// Hidden constructor, users don't need to make this.
private CitationMetadata(List<Citation> citations) {
_citations = new ReadOnlyCollection<Citation>(citations ?? new List<Citation>());
_citations = citations;
}

/// <summary>
Expand Down
10 changes: 4 additions & 6 deletions firebaseai/src/CountTokensResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Google.MiniJSON;
using Firebase.AI.Internal;

Expand Down Expand Up @@ -44,13 +43,13 @@ public readonly struct CountTokensResponse {
[Obsolete("Use TotalTokens instead; Gemini 2.0 series models and newer are always billed by token count.")]
public int? TotalBillableCharacters { get; }

private readonly ReadOnlyCollection<ModalityTokenCount> _promptTokensDetails;
private readonly IReadOnlyList<ModalityTokenCount> _promptTokensDetails;
/// <summary>
/// The breakdown, by modality, of how many tokens are consumed by the prompt.
/// </summary>
public IEnumerable<ModalityTokenCount> PromptTokensDetails {
public IReadOnlyList<ModalityTokenCount> PromptTokensDetails {
get {
return _promptTokensDetails ?? new ReadOnlyCollection<ModalityTokenCount>(new List<ModalityTokenCount>());
return _promptTokensDetails ?? new List<ModalityTokenCount>();
}
}

Expand All @@ -62,8 +61,7 @@ private CountTokensResponse(int totalTokens,
#pragma warning disable CS0618
TotalBillableCharacters = totalBillableCharacters;
#pragma warning restore CS0618
_promptTokensDetails =
new ReadOnlyCollection<ModalityTokenCount>(promptTokensDetails ?? new List<ModalityTokenCount>());
_promptTokensDetails = promptTokensDetails;
}

/// <summary>
Expand Down
72 changes: 34 additions & 38 deletions firebaseai/src/GenerateContentResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Google.MiniJSON;
using Firebase.AI.Internal;
Expand All @@ -27,14 +26,14 @@ namespace Firebase.AI {
/// The model's response to a generate content request.
/// </summary>
public readonly struct GenerateContentResponse {
private readonly ReadOnlyCollection<Candidate> _candidates;
private readonly IReadOnlyList<Candidate> _candidates;

/// <summary>
/// A list of candidate response content, ordered from best to worst.
/// </summary>
public IEnumerable<Candidate> Candidates {
public IReadOnlyList<Candidate> Candidates {
get {
return _candidates ?? new ReadOnlyCollection<Candidate>(new List<Candidate>());
return _candidates ?? new List<Candidate>();
}
}

Expand Down Expand Up @@ -64,16 +63,16 @@ public string Text {
/// <summary>
/// Returns function calls found in any `Part`s of the first candidate of the response, if any.
/// </summary>
public IEnumerable<ModelContent.FunctionCallPart> FunctionCalls {
public IReadOnlyList<ModelContent.FunctionCallPart> FunctionCalls {
get {
return Candidates.FirstOrDefault().Content.Parts.OfType<ModelContent.FunctionCallPart>();
return Candidates.FirstOrDefault().Content.Parts.OfType<ModelContent.FunctionCallPart>().ToList();
}
}

// Hidden constructor, users don't need to make this.
private GenerateContentResponse(List<Candidate> candidates, PromptFeedback? promptFeedback,
UsageMetadata? usageMetadata) {
_candidates = new ReadOnlyCollection<Candidate>(candidates ?? new List<Candidate>());
_candidates = candidates;
PromptFeedback = promptFeedback;
UsageMetadata = usageMetadata;
}
Expand Down Expand Up @@ -132,7 +131,7 @@ public enum BlockReason {
/// A metadata struct containing any feedback the model had on the prompt it was provided.
/// </summary>
public readonly struct PromptFeedback {
private readonly ReadOnlyCollection<SafetyRating> _safetyRatings;
private readonly IReadOnlyList<SafetyRating> _safetyRatings;

/// <summary>
/// The reason a prompt was blocked, if it was blocked.
Expand All @@ -145,9 +144,9 @@ public readonly struct PromptFeedback {
/// <summary>
/// The safety ratings of the prompt.
/// </summary>
public IEnumerable<SafetyRating> SafetyRatings {
public IReadOnlyList<SafetyRating> SafetyRatings {
get {
return _safetyRatings ?? new ReadOnlyCollection<SafetyRating>(new List<SafetyRating>());
return _safetyRatings ?? new List<SafetyRating>();
}
}

Expand All @@ -156,7 +155,7 @@ private PromptFeedback(BlockReason? blockReason, string blockReasonMessage,
List<SafetyRating> safetyRatings) {
BlockReason = blockReason;
BlockReasonMessage = blockReasonMessage;
_safetyRatings = new ReadOnlyCollection<SafetyRating>(safetyRatings ?? new List<SafetyRating>());
_safetyRatings = safetyRatings;
}

private static BlockReason ParseBlockReason(string str) {
Expand Down Expand Up @@ -191,37 +190,37 @@ internal static PromptFeedback FromJson(Dictionary<string, object> jsonDict) {
/// section within the Service Specific Terms).
/// </summary>
public readonly struct GroundingMetadata {
private readonly ReadOnlyCollection<string> _webSearchQueries;
private readonly ReadOnlyCollection<GroundingChunk> _groundingChunks;
private readonly ReadOnlyCollection<GroundingSupport> _groundingSupports;
private readonly IReadOnlyList<string> _webSearchQueries;
private readonly IReadOnlyList<GroundingChunk> _groundingChunks;
private readonly IReadOnlyList<GroundingSupport> _groundingSupports;

/// <summary>
/// A list of web search queries that the model performed to gather the grounding information.
/// These can be used to allow users to explore the search results themselves.
/// </summary>
public IEnumerable<string> WebSearchQueries {
public IReadOnlyList<string> WebSearchQueries {
get {
return _webSearchQueries ?? new ReadOnlyCollection<string>(new List<string>());
return _webSearchQueries ?? new List<string>();
}
}

/// <summary>
/// A list of `GroundingChunk` structs. Each chunk represents a piece of retrieved content
/// (e.g., from a web page) that the model used to ground its response.
/// </summary>
public IEnumerable<GroundingChunk> GroundingChunks {
public IReadOnlyList<GroundingChunk> GroundingChunks {
get {
return _groundingChunks ?? new ReadOnlyCollection<GroundingChunk>(new List<GroundingChunk>());
return _groundingChunks ?? new List<GroundingChunk>();
}
}

/// <summary>
/// A list of `GroundingSupport` structs. Each object details how specific segments of the
/// model's response are supported by the `groundingChunks`.
/// </summary>
public IEnumerable<GroundingSupport> GroundingSupports {
public IReadOnlyList<GroundingSupport> GroundingSupports {
get {
return _groundingSupports ?? new ReadOnlyCollection<GroundingSupport>(new List<GroundingSupport>());
return _groundingSupports ?? new List<GroundingSupport>();
}
}

Expand All @@ -234,9 +233,9 @@ public IEnumerable<GroundingSupport> GroundingSupports {

private GroundingMetadata(List<string> webSearchQueries, List<GroundingChunk> groundingChunks,
List<GroundingSupport> groundingSupports, SearchEntryPoint? searchEntryPoint) {
_webSearchQueries = new ReadOnlyCollection<string>(webSearchQueries ?? new List<string>());
_groundingChunks = new ReadOnlyCollection<GroundingChunk>(groundingChunks ?? new List<GroundingChunk>());
_groundingSupports = new ReadOnlyCollection<GroundingSupport>(groundingSupports ?? new List<GroundingSupport>());
_webSearchQueries = webSearchQueries;
_groundingChunks = groundingChunks;
_groundingSupports = groundingSupports;
SearchEntryPoint = searchEntryPoint;
}

Expand Down Expand Up @@ -347,7 +346,7 @@ internal static WebGroundingChunk FromJson(Dictionary<string, object> jsonDict)
/// retrieved grounding chunks.
/// </summary>
public readonly struct GroundingSupport {
private readonly ReadOnlyCollection<int> _groundingChunkIndices;
private readonly IReadOnlyList<int> _groundingChunkIndices;

/// <summary>
/// Specifies the segment of the model's response content that this grounding support pertains
Expand All @@ -363,15 +362,15 @@ public readonly struct GroundingSupport {
/// means that `groundingChunks[1]`, `groundingChunks[3]`, `groundingChunks[4]` are the
/// retrieved content supporting this part of the response.
/// </summary>
public IEnumerable<int> GroundingChunkIndices {
public IReadOnlyList<int> GroundingChunkIndices {
get {
return _groundingChunkIndices ?? new ReadOnlyCollection<int>(new List<int>());
return _groundingChunkIndices ?? new List<int>();
}
}

private GroundingSupport(Segment segment, List<int> groundingChunkIndices) {
Segment = segment;
_groundingChunkIndices = new ReadOnlyCollection<int>(groundingChunkIndices ?? new List<int>());
_groundingChunkIndices = groundingChunkIndices;
}

internal static GroundingSupport FromJson(Dictionary<string, object> jsonDict) {
Expand Down Expand Up @@ -459,17 +458,17 @@ public readonly struct UsageMetadata {
/// </summary>
public int TotalTokenCount { get; }

private readonly ReadOnlyCollection<ModalityTokenCount> _promptTokensDetails;
public IEnumerable<ModalityTokenCount> PromptTokensDetails {
private readonly IReadOnlyList<ModalityTokenCount> _promptTokensDetails;
public IReadOnlyList<ModalityTokenCount> PromptTokensDetails {
get {
return _promptTokensDetails ?? new ReadOnlyCollection<ModalityTokenCount>(new List<ModalityTokenCount>());
return _promptTokensDetails ?? new List<ModalityTokenCount>();
}
}

private readonly ReadOnlyCollection<ModalityTokenCount> _candidatesTokensDetails;
public IEnumerable<ModalityTokenCount> CandidatesTokensDetails {
private readonly IReadOnlyList<ModalityTokenCount> _candidatesTokensDetails;
public IReadOnlyList<ModalityTokenCount> CandidatesTokensDetails {
get {
return _candidatesTokensDetails ?? new ReadOnlyCollection<ModalityTokenCount>(new List<ModalityTokenCount>());
return _candidatesTokensDetails ?? new List<ModalityTokenCount>();
}
}

Expand All @@ -480,11 +479,8 @@ private UsageMetadata(int promptTC, int candidatesTC, int thoughtsTC, int totalT
CandidatesTokenCount = candidatesTC;
ThoughtsTokenCount = thoughtsTC;
TotalTokenCount = totalTC;
_promptTokensDetails =
new ReadOnlyCollection<ModalityTokenCount>(promptDetails ?? new List<ModalityTokenCount>());
_candidatesTokensDetails =
new ReadOnlyCollection<ModalityTokenCount>(candidateDetails ?? new List<ModalityTokenCount>());

_promptTokensDetails = promptDetails;
_candidatesTokensDetails = candidateDetails;
}

/// <summary>
Expand Down
Loading