Skip to content
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

[Vertex AI] Fix CountTokensResponse decoding #14447

Merged
merged 2 commits into from
Feb 11, 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
17 changes: 16 additions & 1 deletion FirebaseVertexAI/Sources/CountTokensRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,19 @@ extension CountTokensRequest: Encodable {
}

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
extension CountTokensResponse: Decodable {}
extension CountTokensResponse: Decodable {
enum CodingKeys: CodingKey {
case totalTokens
case totalBillableCharacters
case promptTokensDetails
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
totalTokens = try container.decodeIfPresent(Int.self, forKey: .totalTokens) ?? 0
totalBillableCharacters =
try container.decodeIfPresent(Int.self, forKey: .totalBillableCharacters)
promptTokensDetails =
try container.decodeIfPresent([ModalityTokenCount].self, forKey: .promptTokensDetails) ?? []
}
}
16 changes: 8 additions & 8 deletions FirebaseVertexAI/Sources/GenerateContentResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,15 @@ extension GenerateContentResponse.UsageMetadata: Decodable {
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
promptTokenCount = try container.decodeIfPresent(Int.self, forKey: .promptTokenCount) ?? 0
candidatesTokenCount = try container
.decodeIfPresent(Int.self, forKey: .candidatesTokenCount) ?? 0
candidatesTokenCount =
try container.decodeIfPresent(Int.self, forKey: .candidatesTokenCount) ?? 0
totalTokenCount = try container.decodeIfPresent(Int.self, forKey: .totalTokenCount) ?? 0
promptTokensDetails = try container
.decodeIfPresent([ModalityTokenCount].self, forKey: .promptTokensDetails) ??
[ModalityTokenCount]()
candidatesTokensDetails = try container
.decodeIfPresent([ModalityTokenCount].self, forKey: .candidatesTokensDetails) ??
[ModalityTokenCount]()
promptTokensDetails =
try container.decodeIfPresent([ModalityTokenCount].self, forKey: .promptTokensDetails) ?? []
candidatesTokensDetails = try container.decodeIfPresent(
[ModalityTokenCount].self,
forKey: .candidatesTokensDetails
) ?? []
}
}

Expand Down
Loading