Skip to content

Fix: Tool Call chunk merging fails for APIs returning empty string IDs #4629

@Yuumi-zoomie

Description

@Yuumi-zoomie

Bug description
Inside org.springframework.ai.openai.api.OpenAiStreamFunctionCallingHelper at line 114:

			if (currentToolCall.id() != null) {   //HERE
				if (lastPreviousTooCall != null) {
					toolCalls.add(lastPreviousTooCall);
				}
				toolCalls.add(currentToolCall);
			}
			else {
				toolCalls.add(merge(lastPreviousTooCall, currentToolCall));
			}

The current logic for merging ToolCallChunk streams assumes that the tool call ID will be null. However, some API implementations, such as Aliyun Model Studio, return an empty string ("") instead.

This discrepancy causes the merge logic to fail, preventing partial tool call chunks from being correctly assembled into a single function call.

A simple fix is to change the null check to a more robust blank string check (e.g., StringUtils.isBlank()). This will resolve the issue with Aliyun models and improve the overall compatibility of Spring AI with different API providers.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions