Skip to content

Conversation

kmw10693
Copy link

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

  • In streaming mode, tool call JSON fragments can arrive incomplete.
  • This results in Jackson throwing errors such as: com.fasterxml.jackson.core.JsonParseException: Unexpected close marker ']': expected '}'
  • The issue only occurs in streaming (stream()) calls.
  • Synchronous calls (call()) are not affected.
  • This prevents users from reliably using tool callbacks with streaming responses.

What Changed

  • Added a new ToolExecutionEligibilityPredicate implementation: ToolOnFinishPredicate.
  • Ensures tool execution only runs after the assistant signals completion (finishReason = "tool_calls" or "stop").
  • Prevents premature execution and malformed JSON during streaming tool calls.

How to Use

Example usage:

ChatClient client = ChatClient.create(openAiChatModel);

String result = client.prompt()
  .user("Migrate node cluster xx")
  .toolCallbacks(ToolCallbacks.from(new MyTool()))
  .options(OpenAiChatOptions.builder()
      .toolExecutionEligibilityPredicate(new ToolOnFinishPredicate())
      .build())
  .stream()
  .content();

Signed-off-by: Minu Kim <kmw106933@naver.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant