Skip to content

Commit 3e2c79d

Browse files
authored
[Vertex AI] Add Decodable conformance for FunctionResponse (firebase#13606)
1 parent 424516b commit 3e2c79d

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

FirebaseVertexAI/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Unreleased
2+
- [added] Added `Decodable` conformance for `FunctionResponse`. (#13606)
3+
14
# 11.2.0
25
- [fixed] Resolved a decoding error for citations without a `uri` and added
36
support for decoding `title` fields, which were previously ignored. (#13518)

FirebaseVertexAI/Sources/FunctionCalling.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,4 @@ extension FunctionCallingConfig.Mode: Encodable {}
193193

194194
extension ToolConfig: Encodable {}
195195

196-
extension FunctionResponse: Encodable {}
196+
extension FunctionResponse: Codable {}

FirebaseVertexAI/Sources/ModelContent.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,13 @@ extension ModelContent.Part: Codable {
179179
self = .data(mimetype: mimetype, bytes)
180180
} else if values.contains(.functionCall) {
181181
self = try .functionCall(values.decode(FunctionCall.self, forKey: .functionCall))
182+
} else if values.contains(.functionResponse) {
183+
self = try .functionResponse(values.decode(FunctionResponse.self, forKey: .functionResponse))
182184
} else {
183-
throw DecodingError.dataCorrupted(.init(
184-
codingPath: [CodingKeys.text, CodingKeys.inlineData],
185-
debugDescription: "No text, inline data or function call was found."
185+
let unexpectedKeys = values.allKeys.map { $0.stringValue }
186+
throw DecodingError.dataCorrupted(DecodingError.Context(
187+
codingPath: values.codingPath,
188+
debugDescription: "Unexpected ModelContent.Part type(s): \(unexpectedKeys)"
186189
))
187190
}
188191
}

FirebaseVertexAI/Tests/Unit/ModelContentTests.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import XCTest
1919

2020
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
2121
final class ModelContentTests: XCTestCase {
22+
let decoder = JSONDecoder()
2223
let encoder = JSONEncoder()
2324

2425
override func setUp() {
@@ -27,7 +28,35 @@ final class ModelContentTests: XCTestCase {
2728
)
2829
}
2930

30-
// MARK: ModelContent.Part Encoding
31+
// MARK: - ModelContent.Part Decoding
32+
33+
func testDecodeFunctionResponsePart() throws {
34+
let functionName = "test-function-name"
35+
let resultParameter = "test-result-parameter"
36+
let resultValue = "test-result-value"
37+
let json = """
38+
{
39+
"functionResponse" : {
40+
"name" : "\(functionName)",
41+
"response" : {
42+
"\(resultParameter)" : "\(resultValue)"
43+
}
44+
}
45+
}
46+
"""
47+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
48+
49+
let part = try decoder.decode(ModelContent.Part.self, from: jsonData)
50+
51+
guard case let .functionResponse(functionResponse) = part else {
52+
XCTFail("Decoded Part was not a FunctionResponse.")
53+
return
54+
}
55+
XCTAssertEqual(functionResponse.name, functionName)
56+
XCTAssertEqual(functionResponse.response, [resultParameter: .string(resultValue)])
57+
}
58+
59+
// MARK: - ModelContent.Part Encoding
3160

3261
func testEncodeFileDataPart() throws {
3362
let mimeType = "image/jpeg"

0 commit comments

Comments
 (0)