fix(core): deduplicate tool_calls in content_blocks with OpenAI Responses API streaming#36986
Closed
Anmol Mishra (anmolxlight) wants to merge 1 commit intolangchain-ai:masterfrom
Conversation
…nses API streaming When using the OpenAI Responses API, streaming produces partial tool_call blocks in content (fc_ prefix, empty args) while tool_calls holds the finalized blocks (call_ prefix, full args). The previous ID-based dedup logic did not recognize these as the same calls, resulting in duplicate entries in content_blocks. Match tool_call blocks by name and position instead of ID, replacing partial blocks with their finalized counterparts from tool_calls. Closes langchain-ai#36985
|
This PR has been automatically closed because you are not assigned to the linked issue. External contributors must be assigned to an issue before opening a PR for it. Please:
Maintainers: reopen this PR or remove the |
29 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #36985
Problem
When using the OpenAI Responses API, streaming produces partial tool_call blocks in
content(fc_prefix, empty args) whiletool_callsholds the finalized blocks (call_prefix, full args). The previous ID-based dedup logic inAIMessage.content_blocksdid not recognize these as the same calls, resulting in duplicate entries.Root cause
The old logic at
AIMessage.content_blockscompared tool_call IDs between content andself.tool_calls:Since
fc_12345!=call_abc123, both the partial and finalized blocks ended up incontent_blocks.Fix
Match tool_call blocks by name and position instead of ID. When a tool_call block from content matches a tool_call from
self.tool_callsat the same position (same name), replace the partial with the finalized version. Any remaining tool_calls not represented in content are appended.This preserves the existing behavior for:
self.tool_calls(appended as before)Changes
libs/core/langchain_core/messages/ai.py-- rewritten dedup logic inAIMessage.content_blockslibs/core/tests/unit_tests/messages/test_ai.py-- two new regression tests:test_content_blocks_no_duplicate_tool_calls_openai_responses_streaming-- verifies the fc_/call_ deduptest_content_blocks_no_duplicate_tool_calls_mixed_content-- verifies matching-ID dedup still worksAll 200 existing message unit tests continue to pass.
This contribution was developed with the assistance of an AI coding agent.