Skip to content

Commit b1a19b4

Browse files
authored
[Vertex AI] Optional CountTokensResponse.totalBillableCharacters (#13721)
1 parent 5822291 commit b1a19b4

File tree

3 files changed

+7
-17
lines changed

3 files changed

+7
-17
lines changed

FirebaseVertexAI/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
accepts an array of *optional* parameters instead of a list of *required*
1919
parameters; if a parameter is not listed as optional it is assumed to be
2020
required. (#13616)
21+
- [changed] **Breaking Change**: `CountTokensResponse.totalBillableCharacters`
22+
is now optional (`Int?`); it may be `null` in cases such as when a
23+
`GenerateContentRequest` contains only images or other non-text content.
24+
(#13721)
2125

2226
# 11.3.0
2327
- [added] Added `Decodable` conformance for `FunctionResponse`. (#13606)

FirebaseVertexAI/Sources/CountTokensRequest.swift

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public struct CountTokensResponse {
4040
///
4141
/// > Important: This does not include billable image, video or other non-text input. See
4242
/// [Vertex AI pricing](https://cloud.google.com/vertex-ai/generative-ai/pricing) for details.
43-
public let totalBillableCharacters: Int
43+
public let totalBillableCharacters: Int?
4444
}
4545

4646
// MARK: - Codable Conformances
@@ -53,18 +53,4 @@ extension CountTokensRequest: Encodable {
5353
}
5454

5555
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
56-
extension CountTokensResponse: Decodable {
57-
enum CodingKeys: CodingKey {
58-
case totalTokens
59-
case totalBillableCharacters
60-
}
61-
62-
public init(from decoder: any Decoder) throws {
63-
let container = try decoder.container(keyedBy: CodingKeys.self)
64-
totalTokens = try container.decode(Int.self, forKey: .totalTokens)
65-
totalBillableCharacters = try container.decodeIfPresent(
66-
Int.self,
67-
forKey: .totalBillableCharacters
68-
) ?? 0
69-
}
70-
}
56+
extension CountTokensResponse: Decodable {}

FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ final class GenerativeModelTests: XCTestCase {
11941194
))
11951195

11961196
XCTAssertEqual(response.totalTokens, 258)
1197-
XCTAssertEqual(response.totalBillableCharacters, 0)
1197+
XCTAssertNil(response.totalBillableCharacters)
11981198
}
11991199

12001200
func testCountTokens_modelNotFound() async throws {

0 commit comments

Comments
 (0)