Skip to content

Commit 7e03a15

Browse files
committed
Mistral AI streaming function API change fix
1 parent 773b7bd commit 7e03a15

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

models/spring-ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/MistralAiChatClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,13 @@ protected List<ChatCompletionMessage> doGetUserMessages(ChatCompletionRequest re
285285
@SuppressWarnings("null")
286286
@Override
287287
protected ChatCompletionMessage doGetToolResponseMessage(ResponseEntity<ChatCompletion> chatCompletion) {
288-
return chatCompletion.getBody().choices().iterator().next().message();
288+
ChatCompletionMessage msg = chatCompletion.getBody().choices().iterator().next().message();
289+
if (msg.role() == null) {
290+
// add missing role
291+
msg = new ChatCompletionMessage(msg.content(), ChatCompletionMessage.Role.ASSISTANT, msg.name(),
292+
msg.toolCalls());
293+
}
294+
return msg;
289295
}
290296

291297
@Override

models/spring-ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/api/MistralAiApi.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,12 @@ public enum ChatCompletionFinishReason {
535535
*/
536536
@JsonProperty("model_length") MODEL_LENGTH,
537537
/**
538-
* The model called a tool.
538+
*
539539
*/
540-
@JsonProperty("tool_call") TOOL_CALL,
541-
542-
// anticipation of future changes. Based on:
543-
// https://github.com/mistralai/client-python/blob/main/src/mistralai/models/chat_completion.py
544540
@JsonProperty("error") ERROR,
545-
541+
/**
542+
* The model requested a tool call.
543+
*/
546544
@JsonProperty("tool_calls") TOOL_CALLS
547545
// @formatter:on
548546

models/spring-ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/api/MistralAiStreamFunctionCallingHelper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ public boolean isStreamingToolFunctionCallFinish(ChatCompletionChunk chatComplet
190190
}
191191

192192
var choice = choices.get(0);
193-
return choice.finishReason() == ChatCompletionFinishReason.TOOL_CALL
194-
|| choice.finishReason() == ChatCompletionFinishReason.TOOL_CALLS;
193+
return choice.finishReason() == ChatCompletionFinishReason.TOOL_CALLS;
195194
}
196195

197196
}

0 commit comments

Comments
 (0)