Skip to content

Merge streaming tool call deltas by index in OpenAiChatModel#6381

Open
jewoodev wants to merge 1 commit into
spring-projects:mainfrom
jewoodev:fix-stream-toolcall-merge-by-index
Open

Merge streaming tool call deltas by index in OpenAiChatModel#6381
jewoodev wants to merge 1 commit into
spring-projects:mainfrom
jewoodev:fix-stream-toolcall-merge-by-index

Conversation

@jewoodev

@jewoodev jewoodev commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Streaming tool calls through OpenAiChatModel against OpenAI-compatible endpoints such as DeepSeek are broken since 2.0.0-RC1: every argument fragment surfaces as a separate ToolCall with an empty id and name, so tool execution receives null or empty arguments.

ChunkMerger.mergeDeltas treats a delta as the start of a new tool call when toolCall.id().isPresent(). OpenAI omits id on continuation deltas, but DeepSeek-style endpoints send "id": "", which is present, so each fragment is appended as a new tool call instead of being concatenated into the previous one. Up to 2.0.0-M8 the aggregation keyed tool call builders by choice.index() + "-" + toolCall.index() and was not affected; the behavior changed when 12f5cbe (#6288) replaced that aggregation, and d72057c later moved the logic into ChunkMerger unchanged.

This change keys the merge on the delta's required index field, which is what the streaming API defines for correlating tool call deltas. It also lifts the previous one-tool-call-per-chunk Assert and merges parallel tool call deltas correctly when fragments interleave across indexes.

Test

ChunkMerger is now package-private (was private) for direct unit testing. The new tests cover OpenAI-style absent continuation ids, DeepSeek-style empty-string ids (the gh-6374 reproduction), parallel tool calls, and multiple tool call deltas in a single chunk. Verified with ./mvnw -pl models/spring-ai-openai package.

Relation to existing work

#6369 fixes the empty-id continuation case narrowly for #4790. This PR takes the index-keyed path in the current 2.0 OpenAiChatModel.ChunkMerger, which also covers omitted continuation ids, empty-string continuation ids, parallel tool-call indexes, and multiple tool-call deltas in one chunk. Older PRs such as #5973/#4794 target the previous helper-layer code path, while this PR is scoped to the current ChunkMerger implementation.

Fixes gh-6374

The streaming chunk aggregation detects the start of a new tool call by
the presence of the delta id field. OpenAI omits id on continuation
deltas, but some OpenAI-compatible endpoints (e.g. DeepSeek) send an
empty string instead, so every continuation delta surfaced as a
separate tool call with fragmented arguments and tool execution failed.

Key the merge on the delta's required index field instead, which the
streaming API defines for correlating tool call deltas and which the
aggregation used until 2.0.0-M8. This also lifts the one-tool-call-per-
chunk limitation and merges parallel tool call deltas correctly.

Fixes spring-projectsgh-6374

Signed-off-by: jewoodev <jewoos15@naver.com>
@jewoodev jewoodev force-pushed the fix-stream-toolcall-merge-by-index branch from d1d69a0 to 6c04458 Compare June 11, 2026 03:42
@jewoodev

jewoodev commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main to resolve a conflict with #6365.

#6365 changed mergeDeltas to preserve tool-call additional properties. This PR replaces that merge logic with by-index merging, so I carried the preservation over: ChunkMerger.mergeToolCalls now copies each delta's additional properties via putAllAdditionalProperties.

The streaming test added in #6365 (preserveUnmappedToolCallAdditionalProperties) verifies this. It fails without that call and passes with it, along with the rest of the chat model tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant