Repository navigation
🫙 fix: Repeat the Code Execution Container on Follow-up Anthropic Requests - #732
Open
roman-redl wants to merge 1 commit into
Open
roman-redl wants to merge 1 commit into
roman-redl wants to merge 1 commit into
Conversation
…uests When the model calls code_execution (dynamic filtering) and a client tool in the same response, the next request of the run must repeat the container, otherwise Anthropic answers 400 'container_id is required when there are pending tool uses generated by code execution with tools'. invocationParams forwards callOptions.container, but nothing set it. _streamResponseChunks and _streamChatModelEvents now take the container id from the latest AI message of the current turn when the caller did not pass one. Only AI messages after the last human message that starts a turn are searched; human messages injected inside a turn (injected flag, or hook/skill/system/steer/routing/handoff source) do not end it, and a container from an earlier turn is never reused.
This was referenced Nov 22, 2026
roman-redl
added a commit
to roman-redl/parley
that referenced
this pull request
Nov 22, 2026
… next request When the model emits code_execution and a client tool call in the same response, Anthropic requires the container id on the follow-up request (400 container_id is required ...). patch-anthropic-container.sh takes it from the latest AI message of the current turn (proposed upstream as LibreChat-AI/agents#732).
This branch has not been deployed
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.
Summary
Repeat the Anthropic code execution
containeron the follow-up requests of a turn, taking the id from the latest AI message of the current turn when the caller did not pass one.Refs #690 (same live test scenario). Independent of #720 and #731 (server tool result types); they can land in any order.
Problem
When the model emits
code_execution(dynamic filtering withweb_search_20260209/web_search_20260318) and a client tool call in the same response, the next request of the run fails:Root cause
CustomAnthropic.invocationParamsalready forwardscallOptions.container, but nothing sets it. The container id does arrive:message_start(andmessage_delta) put it intoadditional_kwargs.containerof the streamed chunks, which are aggregated into the AI message that goes back to the model on the next request of the run. It is just never read back.Fix
src/llm/anthropic/utils/container.ts:withCurrentTurnContainer(messages, options)returns the options withcontainerset from the current turn whenoptions.containeris not set; an explicitoptions.containeralways wins.additional_kwargs.container.id, thenresponse_metadata.container.id). A container from an earlier turn is never reused (it may have expired and nothing in an earlier turn is waiting on it).additional_kwargs.injected === true, orsourceone of theInjectedMessagesources (skill,hook,system,steer) or the handoff cue sources (routing,handoff). Any other human message starts a new turn, so an unknown kind of message falls back to the previous behavior (no container) rather than to a stale one.container_...id (a merge that appends strings) is collapsed to the shorter one. With the current@langchain/coremergeidis not doubled._streamResponseChunksand_streamChatModelEvents(the two methods that build params from messages) pass their options through the helper. The non-streaming_generatebelongs to@langchain/anthropic; it goes through_streamResponseChunkswhen streaming, which is what LibreChat uses.Tests
New
src/llm/anthropic/container.spec.ts(stubscreateStreamWithRetry, asserts the request container), run against both_streamResponseChunksand_streamChatModelEvents:injected+source: 'hook', or aroutingcue): container is still sent;options.containerwins;Plus unit tests of
getCurrentTurnContainerId(response_metadatafallback, merged stream chunks keep the id, repeated id collapsed, empty id ignored, unmarked meta human message starts a turn).Without the change 8 of 19 tests fail.
npx jest src/llm/anthropic src/messages: 1251 passed, 1 skipped;llm.spec.tsdoes not run onmaineither (suite fails to run in this environment). Full suite: identical set of 22 failing suites as onmain(not related to this change).npx tscdiagnostics andnpx eslinton the touched files: no new findings.Verification
Found and verified live on LibreChat v0.8.9 with
web_search_20260318,claude-opus-5-5and MCP client tools, using the same change applied as a build-time patch to the compiled 4.2.7 dist: 6/6 runs without errors including 6-request turns, plus a 4-turn session with 4 researcher subagents and 70 searches with 0 errors (together with the #720 changes and #731).