Skip to content

Commit 6922e92

Browse files
committed
fix: make ChatStreamResult fields optional for compatibility
1 parent 52e0ffc commit 6922e92

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

Sources/OpenAI/Private/KeyedDecodingContainer+ParsingOptions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ extension KeyedDecodingContainer {
1111
func decodeString(forKey key: KeyedDecodingContainer<K>.Key, parsingOptions: ParsingOptions) throws -> String {
1212
try self.decode(String.self, forKey: key, parsingOptions: parsingOptions, defaultValue: "")
1313
}
14+
15+
func decodeStringIfPresent(forKey key: KeyedDecodingContainer<K>.Key, parsingOptions: ParsingOptions) throws -> String? {
16+
try self.decodeIfPresent(String.self, forKey: key)
17+
}
1418

1519
func decodeTimeInterval(forKey key: KeyedDecodingContainer<K>.Key, parsingOptions: ParsingOptions) throws -> TimeInterval {
1620
try self.decode(TimeInterval.self, forKey: key, parsingOptions: parsingOptions, defaultValue: 0)

Sources/OpenAI/Public/Models/ChatStreamResult.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,20 @@ public struct ChatStreamResult: Codable, Equatable, Sendable {
168168
}
169169

170170
/// A unique identifier for the chat completion. Each chunk has the same ID.
171-
public let id: String
171+
public let id: String?
172172

173173
/// The object type, which is always `chat.completion.chunk`.
174-
public let object: String
174+
public let object: String?
175175

176176
/// The Unix timestamp (in seconds) of when the chat completion was created. Each chunk has the same timestamp.
177-
public let created: TimeInterval
177+
public let created: TimeInterval?
178178

179179
/// The model to generate the completion.
180-
public let model: String
181-
180+
public let model: String?
181+
182182
/// A list of chat completion choices. Can contain more than one element if `n` is greater than 1.
183183
/// Can also be empty for the last chunk if you set `stream_options: {"include_usage": true}`.
184-
public let choices: [Choice]
184+
public let choices: [Choice]?
185185

186186
/// This fingerprint represents the backend configuration that the model runs with.
187187
/// Can be used in conjunction with the `seed` request parameter to understand when backend changes
@@ -228,12 +228,12 @@ public struct ChatStreamResult: Codable, Equatable, Sendable {
228228
let container = try decoder.container(keyedBy: CodingKeys.self)
229229
let parsingOptions = decoder.userInfo[.parsingOptions] as? ParsingOptions ?? []
230230

231-
self.id = try container.decodeString(forKey: .id, parsingOptions: parsingOptions)
232-
self.object = try container.decodeString(forKey: .object, parsingOptions: parsingOptions)
233-
self.created = try container.decode(TimeInterval.self, forKey: .created)
234-
self.model = try container.decodeString(forKey: .model, parsingOptions: parsingOptions)
231+
self.id = try container.decodeStringIfPresent(forKey: .id, parsingOptions: parsingOptions)
232+
self.object = try container.decodeStringIfPresent(forKey: .object, parsingOptions: parsingOptions)
233+
self.created = try container.decodeIfPresent(TimeInterval.self, forKey: .created)
234+
self.model = try container.decodeStringIfPresent(forKey: .model, parsingOptions: parsingOptions)
235235
self.citations = try container.decodeIfPresent([String].self, forKey: .citations)
236-
self.choices = try container.decode([ChatStreamResult.Choice].self, forKey: .choices)
236+
self.choices = try container.decodeIfPresent([ChatStreamResult.Choice].self, forKey: .choices)
237237
self.systemFingerprint = try container.decodeIfPresent(String.self, forKey: .systemFingerprint)
238238

239239
// Even though API Reference declares that usage field should be either informative or null: https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream_options

0 commit comments

Comments
 (0)