Skip to content

Commit 58de48f

Browse files
authored
Merge pull request #294 from MacPaw/293-keynotfound-errors-since-v037
Make toolCalls and annotations optional
2 parents 1a7a5ba + a60c1e6 commit 58de48f

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

Demo/DemoChat/Sources/ChatStore.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public final class ChatStore: ObservableObject {
6666
func sendMessage(
6767
_ message: Message,
6868
conversationId: Conversation.ID,
69-
model: Model
69+
model: Model,
70+
isStreamEnabled: Bool
7071
) async {
7172
guard let conversationIndex = conversations.firstIndex(where: { $0.id == conversationId }) else {
7273
return
@@ -79,7 +80,7 @@ public final class ChatStore: ObservableObject {
7980
await completeChat(
8081
conversationId: conversationId,
8182
model: model,
82-
stream: true
83+
stream: isStreamEnabled
8384
)
8485
// For assistant case we send chats to thread and then poll, polling will receive sent chat + new assistant messages.
8586
case .assistant:

Demo/DemoChat/Sources/UI/ChatView.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public struct ChatView: View {
5353
DetailView(
5454
availableAssistants: assistantStore.availableAssistants, conversation: conversation,
5555
error: store.conversationErrors[conversation.id],
56-
sendMessage: { message, selectedModel in
56+
sendMessage: { message, selectedModel, streamEnabled in
5757
self.sendMessageTask = Task {
5858
await store.sendMessage(
5959
Message(
@@ -63,7 +63,8 @@ public struct ChatView: View {
6363
createdAt: dateProvider()
6464
),
6565
conversationId: conversation.id,
66-
model: selectedModel
66+
model: selectedModel,
67+
isStreamEnabled: streamEnabled
6768
)
6869
}
6970
}, isSendingMessage: $store.isSendingMessage

Demo/DemoChat/Sources/UI/DetailView.swift

+11-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ struct DetailView: View {
1818
@FocusState private var isFocused: Bool
1919
@State private var showsModelSelectionSheet = false
2020
@State private var selectedChatModel: Model = .gpt4_o_mini
21+
@State private var streamEnabled = true
2122
var availableAssistants: [Assistant]
2223

2324
private static let availableChatModels: [Model] = [.gpt4_o_mini]
2425

2526
let conversation: Conversation
2627
let error: Error?
27-
let sendMessage: (String, Model) -> Void
28+
let sendMessage: (String, Model, Bool) -> Void
2829

2930
@Binding var isSendingMessage: Bool
3031

@@ -76,7 +77,7 @@ struct DetailView: View {
7677
.safeAreaInset(edge: .top) {
7778
HStack {
7879
Text(
79-
"Model: \(conversation.type == .assistant ? Model.gpt4_o_mini : selectedChatModel)"
80+
"Model: \(conversation.type == .assistant ? Model.gpt4_o_mini : selectedChatModel), stream: \(streamEnabled)"
8081
)
8182
.font(.caption)
8283
.foregroundColor(.secondary)
@@ -123,6 +124,12 @@ struct DetailView: View {
123124
Text(model)
124125
}
125126
}
127+
128+
Button {
129+
streamEnabled.toggle()
130+
} label: {
131+
Text(streamEnabled ? "Disable streaming" : "Enable streaming")
132+
}
126133

127134
Button("Cancel", role: .cancel) {
128135
showsModelSelectionSheet = false
@@ -219,7 +226,7 @@ struct DetailView: View {
219226
return
220227
}
221228

222-
sendMessage(message, selectedChatModel)
229+
sendMessage(message, selectedChatModel, streamEnabled)
223230
inputText = ""
224231

225232
// if let lastMessage = conversation.messages.last {
@@ -317,7 +324,7 @@ struct DetailView_Previews: PreviewProvider {
317324
]
318325
),
319326
error: nil,
320-
sendMessage: { _, _ in }, isSendingMessage: Binding.constant(false)
327+
sendMessage: { _, _, _ in }, isSendingMessage: Binding.constant(false)
321328
)
322329
}
323330
}

Demo/DemoChat/Sources/UI/ModerationChatView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public struct ModerationChatView: View {
2121
DetailView(
2222
availableAssistants: [], conversation: store.moderationConversation,
2323
error: store.moderationConversationError,
24-
sendMessage: { message, _ in
24+
sendMessage: { message, _, _ in
2525
Task {
2626
await store.sendModerationMessage(
2727
Message(

Sources/OpenAI/Public/Models/ChatResult.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,22 @@ public struct ChatResult: Codable, Equatable, Sendable {
2626
public let refusal: String?
2727
/// The role of the author of this message.
2828
public let role: String
29+
2930
/// Annotations for the message, when applicable, as when using the web search tool.
3031
/// Web search tool: https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat
31-
public let annotations: [Annotation]
32+
///
33+
/// This field is declared non-optional in OpenAI API reference, but to not make breaking changes for our users that use this SDK for other providers like DeepSeek, made it optional for now
34+
/// See https://github.com/MacPaw/OpenAI/issues/293
35+
public let annotations: [Annotation]?
3236
/// If the audio output modality is requested, this object contains data about the audio response from the model.
3337
/// Learn more: https://platform.openai.com/docs/guides/audio
3438
public let audio: Audio?
39+
3540
/// The tool calls generated by the model, such as function calls.
36-
public let toolCalls: [ChatQuery.ChatCompletionMessageParam.AssistantMessageParam.ToolCallParam]
41+
///
42+
/// This field is declared not-optional in OpenAI API reference. But it seems that under some conditions it may not be present in a response.
43+
/// See https://github.com/MacPaw/OpenAI/issues/293
44+
public let toolCalls: [ChatQuery.ChatCompletionMessageParam.AssistantMessageParam.ToolCallParam]?
3745

3846
/// Following are fields that are not part of OpenAI but are present in responses from other providers
3947

0 commit comments

Comments
 (0)