-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix: handle JSON parse error in streaming tool calls #4274
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Minu Kim <kmw106933@naver.com>
| import java.util.List; | ||
| import java.util.function.BiPredicate; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't use this annotation for not null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@markpollack I’ve removed ToolOnFinishPredicate.java and applied the fix directly in OpenAiChatModel.
when the LLM returns malformed or invalid JSON during streaming, the code explicitly throws an exception.
|
The PR looks incomplete. I've commented on the issue thread for further clarification. #4274 |
Signed-off-by: Minu Kim <kmw106933@naver.com>
@markpollack However, the OpenAIChatModel was not properly detected when the LLM returned an invalid response. |
| return new OpenAiApi.ChatCompletion(chunk.id(), choices, chunk.created(), chunk.model(), | ||
| chunk.serviceTier(), chunk.systemFingerprint(), "chat.completion", chunk.usage()); | ||
| } | ||
| catch (Exception e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see what is going to throw an exception in either one of these calls. records are being created, what json marshalling is going on?
| void shouldThrowExceptionOnInvalidJsonChunk() { | ||
| OpenAiApi mockApi = Mockito.mock(OpenAiApi.class); | ||
|
|
||
| OpenAiApi.ChatCompletionChunk invalidChunk = new OpenAiApi.ChatCompletionChunk("invalid-id", null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain what qualifies this as an invalid chunk here? I don't understand how this could fail with JSON parser error.
Overview
This PR fixes a JSON parsing error that occurs when handling streaming tool calls in Spring AI.
Related issue: spring-projects/spring-ai#4250
Why
com.fasterxml.jackson.core.JsonParseException: Unexpected close marker ']': expected '}'stream()) calls.call()) are not affected.What Changed
ToolExecutionEligibilityPredicateimplementation:ToolOnFinishPredicate.finishReason = "tool_calls"or"stop").How to Use
Example usage: