Skip to content

Commit edfa1e6

Browse files
authored
Merge pull request #84 from ysak-y/send_property_even_if_content_is_nil
[BUG FIX] Encode content property with nil value when responding to function calling
2 parents a9965f0 + a6e1b2f commit edfa1e6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Sources/OpenAI/Public/Models/ChatQuery.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ public struct Chat: Codable, Equatable {
3535
self.name = name
3636
self.functionCall = functionCall
3737
}
38+
39+
public func encode(to encoder: Encoder) throws {
40+
var container = encoder.container(keyedBy: CodingKeys.self)
41+
try container.encode(role, forKey: .role)
42+
43+
if let name = name {
44+
try container.encode(name, forKey: .name)
45+
}
46+
47+
if let functionCall = functionCall {
48+
try container.encode(functionCall, forKey: .functionCall)
49+
}
50+
51+
// Should add 'nil' to 'content' property for function calling response
52+
// See https://openai.com/blog/function-calling-and-other-api-updates
53+
if content != nil || (role == .assistant && functionCall != nil) {
54+
try container.encode(content, forKey: .content)
55+
}
56+
}
3857
}
3958

4059
public struct ChatFunctionCall: Codable, Equatable {

0 commit comments

Comments
 (0)