Skip to content

Commit

Permalink
Add extra fields to countToken response
Browse files Browse the repository at this point in the history
  • Loading branch information
cynthiajoan committed May 7, 2024
1 parent b600400 commit a14697a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkgs/google_generative_ai/lib/src/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ final class CountTokensResponse {
/// Always non-negative.
final int totalTokens;

CountTokensResponse(this.totalTokens);
/// Optional extra fields that returned from count token response
final Map<String, dynamic>? extraFields;

CountTokensResponse(this.totalTokens, {this.extraFields});
}

/// Response from the model; supports multiple candidates.
Expand Down Expand Up @@ -543,7 +546,8 @@ GenerateContentResponse parseGenerateContentResponse(Object jsonObject) {

CountTokensResponse parseCountTokensResponse(Object jsonObject) {
return switch (jsonObject) {
{'totalTokens': final int totalTokens} => CountTokensResponse(totalTokens),
{'totalTokens': final int totalTokens} =>
CountTokensResponse(totalTokens, extraFields: Map.from(jsonObject)),
{'error': final Object error} => throw parseError(error),
_ =>
throw FormatException('Unhandled CountTokensResponse format', jsonObject)
Expand Down

0 comments on commit a14697a

Please sign in to comment.