Skip to content

safely merge empty tool call name case(streaming mode)#4588

Open
yxfengforever wants to merge 2 commits intospring-projects:mainfrom
yxfengforever:main_dev
Open

safely merge empty tool call name case(streaming mode)#4588
yxfengforever wants to merge 2 commits intospring-projects:mainfrom
yxfengforever:main_dev

Conversation

@yxfengforever
Copy link

@yxfengforever yxfengforever commented Oct 10, 2025

Signed-off-by: Feng Xieyxloveforever@gmail.com

Overview

The PR is to add a private method safelyMergeAssistantMessageIfEmptyToolCallPresent processes and merges tool calls in an AssistantMessage, particularly handling cases where tool calls might have empty name or fragmented arguments due to streaming or other processing issues.

Key Logic of safelyMergeAssistantMessageIfEmptyToolCallPresent

  • Reverses tool calls to process from last to first
  • Accumulates arguments in StringBuilder
  • Critical: StringUtils.hasText(toolCall.name()) check prevents invalid tool call exceptions
  • Only creates ToolCall objects when name has valid text content
  • Reverses processed tool calls back to original order

Safety Mechanism

StringUtils.hasText(toolCall.name()) serves as a guard to:

  • Prevent creating tool calls with null/empty names
  • Avoid downstream execution exceptions
  • Filter out malformed streaming data

Result

Returns new AssistantMessage with merged tool calls, preserving original content, media, and metadata.

Signed-off-by: Feng Xie<yxloveforever@gmail.com>

Signed-off-by: yxloveforever <yxloveforever@gmail.com>
Signed-off-by: Feng Xie<yxloveforever@gmail.com>

Signed-off-by: yxloveforever <yxloveforever@gmail.com>
@tzolov
Copy link
Contributor

tzolov commented Dec 11, 2025

Hi @yxfengforever , can you elaborate about the issue? What models/providers are generating tool call chunks without name?

@ilayaperumalg ilayaperumalg modified the milestones: 2.0.0.M1, 2.0.0.M2 Dec 11, 2025
@yxfengforever
Copy link
Author

The model i use is deepseek.
The issue is:
when i use toolcall in the stream mode, the toolcall name might be empty. One of the place in the spring AI framework requires to check if toolcall name presents,StringUtils.hasText(toolCall.name()). If the name is empty, then it throws exception.
Actually, i ran a testing with one tool like "weatherReport". Here is the code.

 Flux<ChatResponse> response = chatClient.prompt(ref.prompt)
                        Map.of("qam-user-id", headers.get("qam-user-id"))))
                .stream().chatResponse();

When it uses stream mode, then there are 2 tool calls return, of which one name is empty string. The expected response should be one tool call "weatherReport".

@ilayaperumalg ilayaperumalg modified the milestones: 2.0.0.M2, 2.0.0.RC1 Jan 20, 2026
@ilayaperumalg ilayaperumalg modified the milestones: 2.0.0-RC1, 2.0.0-M3 Feb 3, 2026
@tzolov tzolov modified the milestones: 2.0.0-M3, 2.0.0-M4 Mar 13, 2026
@tzolov tzolov assigned sdeleuze and unassigned markpollack and tzolov Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants