Skip to content

Commit

Permalink
Merge pull request #84 from ysak-y/send_property_even_if_content_is_nil
Browse files Browse the repository at this point in the history
[BUG FIX] Encode content property with nil value when responding to function calling
  • Loading branch information
Krivoblotsky authored Jul 10, 2023
2 parents a9965f0 + a6e1b2f commit edfa1e6
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/OpenAI/Public/Models/ChatQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ public struct Chat: Codable, Equatable {
self.name = name
self.functionCall = functionCall
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(role, forKey: .role)

if let name = name {
try container.encode(name, forKey: .name)
}

if let functionCall = functionCall {
try container.encode(functionCall, forKey: .functionCall)
}

// Should add 'nil' to 'content' property for function calling response
// See https://openai.com/blog/function-calling-and-other-api-updates
if content != nil || (role == .assistant && functionCall != nil) {
try container.encode(content, forKey: .content)
}
}
}

public struct ChatFunctionCall: Codable, Equatable {
Expand Down

0 comments on commit edfa1e6

Please sign in to comment.