@@ -26,6 +26,9 @@ public struct GenerateContentResponse: Sendable {
26
26
/// The total number of tokens across the generated response candidates.
27
27
public let candidatesTokenCount : Int
28
28
29
+ /// The number of tokens used by tools.
30
+ public let toolUsePromptTokenCount : Int
31
+
29
32
/// The number of tokens used by the model's internal "thinking" process.
30
33
///
31
34
/// For models that support thinking (like Gemini 2.5 Pro and Flash), this represents the actual
@@ -39,11 +42,15 @@ public struct GenerateContentResponse: Sendable {
39
42
/// The total number of tokens in both the request and response.
40
43
public let totalTokenCount : Int
41
44
42
- /// The breakdown, by modality, of how many tokens are consumed by the prompt
45
+ /// The breakdown, by modality, of how many tokens are consumed by the prompt.
43
46
public let promptTokensDetails : [ ModalityTokenCount ]
44
47
45
48
/// The breakdown, by modality, of how many tokens are consumed by the candidates
46
49
public let candidatesTokensDetails : [ ModalityTokenCount ]
50
+
51
+ /// The breakdown, by modality, of how many tokens were consumed by the tools used to process
52
+ /// the request.
53
+ public let toolUsePromptTokensDetails : [ ModalityTokenCount ]
47
54
}
48
55
49
56
/// A list of candidate response content, ordered from best to worst.
@@ -154,14 +161,19 @@ public struct Candidate: Sendable {
154
161
155
162
public let groundingMetadata : GroundingMetadata ?
156
163
164
+ /// Metadata related to the ``URLContext`` tool.
165
+ public let urlContextMetadata : URLContextMetadata ?
166
+
157
167
/// Initializer for SwiftUI previews or tests.
158
168
public init ( content: ModelContent , safetyRatings: [ SafetyRating ] , finishReason: FinishReason ? ,
159
- citationMetadata: CitationMetadata ? , groundingMetadata: GroundingMetadata ? = nil ) {
169
+ citationMetadata: CitationMetadata ? , groundingMetadata: GroundingMetadata ? = nil ,
170
+ urlContextMetadata: URLContextMetadata ? = nil ) {
160
171
self . content = content
161
172
self . safetyRatings = safetyRatings
162
173
self . finishReason = finishReason
163
174
self . citationMetadata = citationMetadata
164
175
self . groundingMetadata = groundingMetadata
176
+ self . urlContextMetadata = urlContextMetadata
165
177
}
166
178
167
179
// Returns `true` if the candidate contains no information that a developer could use.
@@ -469,17 +481,21 @@ extension GenerateContentResponse.UsageMetadata: Decodable {
469
481
enum CodingKeys : CodingKey {
470
482
case promptTokenCount
471
483
case candidatesTokenCount
484
+ case toolUsePromptTokenCount
472
485
case thoughtsTokenCount
473
486
case totalTokenCount
474
487
case promptTokensDetails
475
488
case candidatesTokensDetails
489
+ case toolUsePromptTokensDetails
476
490
}
477
491
478
492
public init ( from decoder: any Decoder ) throws {
479
493
let container = try decoder. container ( keyedBy: CodingKeys . self)
480
494
promptTokenCount = try container. decodeIfPresent ( Int . self, forKey: . promptTokenCount) ?? 0
481
495
candidatesTokenCount =
482
496
try container. decodeIfPresent ( Int . self, forKey: . candidatesTokenCount) ?? 0
497
+ toolUsePromptTokenCount =
498
+ try container. decodeIfPresent ( Int . self, forKey: . toolUsePromptTokenCount) ?? 0
483
499
thoughtsTokenCount = try container. decodeIfPresent ( Int . self, forKey: . thoughtsTokenCount) ?? 0
484
500
totalTokenCount = try container. decodeIfPresent ( Int . self, forKey: . totalTokenCount) ?? 0
485
501
promptTokensDetails =
@@ -488,6 +504,9 @@ extension GenerateContentResponse.UsageMetadata: Decodable {
488
504
[ ModalityTokenCount ] . self,
489
505
forKey: . candidatesTokensDetails
490
506
) ?? [ ]
507
+ toolUsePromptTokensDetails = try container. decodeIfPresent (
508
+ [ ModalityTokenCount ] . self, forKey: . toolUsePromptTokensDetails
509
+ ) ?? [ ]
491
510
}
492
511
}
493
512
@@ -499,6 +518,7 @@ extension Candidate: Decodable {
499
518
case finishReason
500
519
case citationMetadata
501
520
case groundingMetadata
521
+ case urlContextMetadata
502
522
}
503
523
504
524
/// Initializes a response from a decoder. Used for decoding server responses; not for public
@@ -540,6 +560,14 @@ extension Candidate: Decodable {
540
560
GroundingMetadata . self,
541
561
forKey: . groundingMetadata
542
562
)
563
+
564
+ if let urlContextMetadata =
565
+ try container. decodeIfPresent ( URLContextMetadata . self, forKey: . urlContextMetadata) ,
566
+ !urlContextMetadata. urlMetadata. isEmpty {
567
+ self . urlContextMetadata = urlContextMetadata
568
+ } else {
569
+ urlContextMetadata = nil
570
+ }
543
571
}
544
572
}
545
573
0 commit comments