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

[BUG FIX] Encode content property with nil value when responding to function calling #84

Merged
Merged
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
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