fix(aws): accept tool_call content blocks in convertAIMessageToConverseMessage - #11633
Open
Lukas Buck (L4XB) wants to merge 1 commit into
Open
Lukas Buck (L4XB) wants to merge 1 commit into
Lukas Buck (L4XB) wants to merge 1 commit into
Conversation
…seMessage `convertAIMessageToConverseMessage` threw `Unsupported content block type: tool_call` on any assistant message whose `content` array carried a standard `tool_call` block, while serializing the same call from `msg.tool_calls` a few lines below. An AIMessage holds both views, so the converter rejected a shape this class produces itself. Skip the block when `msg.tool_calls` already carries the same id and name. When it does not, convert the block to a `toolUse` rather than skipping it: core only keeps `content` and `tool_calls` in sync for a message built with `contentBlocks`, so one assembled through `content` can hold the block alone, and a blanket skip would drop that call silently — the failure the throw was there to prevent. Scope is the standard `tool_call` block. Anthropic's provider-shaped `tool_use` is not produced by this class and is left to the existing throw. Fixes langchain-ai#11476
🦋 Changeset detectedLatest commit: 8c1ff18 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Fixes #11476.
The bug
convertAIMessageToConverseMessagewalks an assistant message'scontentarray and throws on any block type it does not recognise:A
tool_callblock is one of those, even though the same call is serialized a few lines below frommsg.tool_calls. AnAIMessagecarries both views —contentblocks andtool_calls— so the converter rejected a message shape this class produces itself. Any agent loop that replays assistant turns as standard content blocks hits it.The fix
Handle
tool_callbefore the throw. Whenmsg.tool_callsalready carries the same id and name, the block is the duplicate and is skipped, so thetoolUseis emitted exactly once, from the existingtool_callspass.When it does not, the block is converted to a
toolUsehere instead of being skipped. That branch matters: core only syncscontentandtool_callswhen anAIMessageis constructed withcontentBlocks(libs/langchain-core/src/messages/ai.ts), so a message assembled throughcontentcan carry the block withtool_callsempty. A blanket skip would drop that call silently — which is the failure the throw was there to prevent in the first place.Scope is the standard
tool_callblock. Anthropic's provider-shapedtool_useis not produced by this class, so it stays with the existing throw rather than being mapped on a guess about which ofargs/inputit carries.Tests
Three cases in
libs/providers/langchain-aws/src/tests/chat_models.test.ts:tool_callblock and intool_callsconverts without throwing, and yields onetoolUse, in the block's position relative to the text;Checked against mutants rather than only against the fix: on
mainthe first two fail and the third passes; skipping the block unconditionally (the shape the issue suggests) fails the second; emitting it without the duplicate check fails the first, with twotoolUseblocks for one call.oxlintandoxfmt --checkare clean, and a changeset is included.