Skip to content

Commit 988e4ef

Browse files
committed
[Vertex AI] Fix decoding ModalityTokenCount when tokenCount is 0
1 parent fb400f8 commit 988e4ef

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

FirebaseVertexAI/Sources/ModalityTokenCount.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,18 @@ public struct ContentModality: DecodableProtoEnum, Hashable, Sendable {
5757
VertexLog.MessageCode.generateContentResponseUnrecognizedContentModality
5858
}
5959

60+
// MARK: Codable Conformances
61+
6062
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
61-
extension ModalityTokenCount: Decodable {}
63+
extension ModalityTokenCount: Decodable {
64+
enum CodingKeys: CodingKey {
65+
case modality
66+
case tokenCount
67+
}
68+
69+
public init(from decoder: any Decoder) throws {
70+
let container = try decoder.container(keyedBy: CodingKeys.self)
71+
self.modality = try container.decode(ContentModality.self, forKey: .modality)
72+
self.tokenCount = try container.decodeIfPresent(Int.self, forKey: .tokenCount) ?? 0
73+
}
74+
}

0 commit comments

Comments
 (0)