Skip to content

Commit 72bd4c7

Browse files
authored
Merge pull request #388 from MacPaw/make-index-required
Make index of ChoiceDeltaToolCall required
2 parents 7e0a6ad + 08c3838 commit 72bd4c7

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

Sources/OpenAI/Private/KeyedDecodingContainer+ParsingOptions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ extension KeyedDecodingContainer {
1616
try self.decode(TimeInterval.self, forKey: key, parsingOptions: parsingOptions, defaultValue: 0)
1717
}
1818

19+
func decodeInt(forKey key: KeyedDecodingContainer<K>.Key, parsingOptions: ParsingOptions) throws -> Int {
20+
try self.decode(Int.self, forKey: key, parsingOptions: parsingOptions, defaultValue: 0)
21+
}
22+
1923
func decode<T: Decodable>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key, parsingOptions: ParsingOptions, defaultValue: T) throws -> T {
2024
do {
2125
return try decode(T.self, forKey: key)

Sources/OpenAI/Public/Models/ChatStreamResult.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public struct ChatStreamResult: Codable, Equatable, Sendable {
7171

7272
public struct ChoiceDeltaToolCall: Codable, Equatable, Sendable {
7373

74-
public let index: Int?
74+
public let index: Int
7575
/// The ID of the tool call.
7676
public let id: String?
7777
/// The function that the model called.
@@ -89,6 +89,15 @@ public struct ChatStreamResult: Codable, Equatable, Sendable {
8989
self.function = function
9090
self.type = "function"
9191
}
92+
93+
public init(from decoder: any Decoder) throws {
94+
let container: KeyedDecodingContainer<ChoiceDeltaToolCall.CodingKeys> = try decoder.container(keyedBy: ChoiceDeltaToolCall.CodingKeys.self)
95+
let parsingOptions = decoder.userInfo[.parsingOptions] as? ParsingOptions ?? []
96+
self.index = try container.decodeInt(forKey: ChoiceDeltaToolCall.CodingKeys.index, parsingOptions: parsingOptions)
97+
self.id = try container.decodeIfPresent(String.self, forKey: ChoiceDeltaToolCall.CodingKeys.id)
98+
self.function = try container.decodeIfPresent(ChoiceDeltaToolCallFunction.self, forKey: ChoiceDeltaToolCall.CodingKeys.function)
99+
self.type = try container.decodeIfPresent(String.self, forKey: ChoiceDeltaToolCall.CodingKeys.type)
100+
}
92101

93102
public struct ChoiceDeltaToolCallFunction: Codable, Equatable, Sendable {
94103

0 commit comments

Comments
 (0)