Skip to content

Commit 43eddac

Browse files
committed
Make totalBillableCharacters 0 if omitted
1 parent 6070645 commit 43eddac

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

FirebaseVertexAI/Sources/CountTokensRequest.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,27 @@ extension CountTokensRequest: GenerativeAIRequest {
3939

4040
/// The model's response to a count tokens request.
4141
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
42-
public struct CountTokensResponse: Decodable {
42+
public struct CountTokensResponse {
4343
/// The total number of tokens in the input given to the model as a prompt.
4444
public let totalTokens: Int
4545

4646
/// The total number of billable characters in the input given to the model as a prompt.
47-
public let totalBillableCharacters: Int?
47+
public let totalBillableCharacters: Int
48+
}
49+
50+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
51+
extension CountTokensResponse: Decodable {
52+
enum CodingKeys: CodingKey {
53+
case totalTokens
54+
case totalBillableCharacters
55+
}
56+
57+
public init(from decoder: any Decoder) throws {
58+
let container = try decoder.container(keyedBy: CodingKeys.self)
59+
totalTokens = try container.decode(Int.self, forKey: .totalTokens)
60+
totalBillableCharacters = try container.decodeIfPresent(
61+
Int.self,
62+
forKey: .totalBillableCharacters
63+
) ?? 0
64+
}
4865
}

FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ final class GenerativeModelTests: XCTestCase {
958958
))
959959

960960
XCTAssertEqual(response.totalTokens, 258)
961-
XCTAssertNil(response.totalBillableCharacters)
961+
XCTAssertEqual(response.totalBillableCharacters, 0)
962962
}
963963

964964
func testCountTokens_modelNotFound() async throws {

0 commit comments

Comments
 (0)