Skip to content

🫙 fix: Repeat the Code Execution Container on Follow-up Anthropic Requests - #732

Open
roman-redl wants to merge 1 commit into
LibreChat-AI:mainfrom
roman-redl:fix/anthropic-code-execution-container
Open

roman-redl wants to merge 1 commit into
LibreChat-AI:mainfrom
roman-redl:fix/anthropic-code-execution-container

Conversation

@roman-redl

Copy link
Copy Markdown
Contributor

Summary

Repeat the Anthropic code execution container on 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 with web_search_20260209 / web_search_20260318) and a client tool call in the same response, the next request of the run fails:

400 invalid_request_error: container_id is required when there are pending tool uses generated by code execution with tools.

Root cause

CustomAnthropic.invocationParams already forwards callOptions.container, but nothing sets it. The container id does arrive: message_start (and message_delta) put it into additional_kwargs.container of 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

  • New src/llm/anthropic/utils/container.ts: withCurrentTurnContainer(messages, options) returns the options with container set from the current turn when options.container is not set; an explicit options.container always wins.
  • Only the current turn is searched: the AI messages after the last human message that starts a turn, latest one first (additional_kwargs.container.id, then response_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).
  • Human messages added inside a turn do not end it: additional_kwargs.injected === true, or source one of the InjectedMessage sources (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.
  • Defensive: an id that is a back-to-back repetition of a shorter container_... id (a merge that appends strings) is collapsed to the shorter one. With the current @langchain/core merge id is not doubled.
  • _streamResponseChunks and _streamChatModelEvents (the two methods that build params from messages) pass their options through the helper. The non-streaming _generate belongs to @langchain/anthropic; it goes through _streamResponseChunks when streaming, which is what LibreChat uses.

Tests

New src/llm/anthropic/container.spec.ts (stubs createStreamWithRetry, asserts the request container), run against both _streamResponseChunks and _streamChatModelEvents:

  • container of the current turn is sent, and the latest one wins;
  • a new human turn: container from the earlier turn is not sent;
  • a human message injected inside the turn (injected + source: 'hook', or a routing cue): container is still sent;
  • explicit options.container wins;
  • no container on any message: nothing sent.

Plus unit tests of getCurrentTurnContainerId (response_metadata fallback, 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.ts does not run on main either (suite fails to run in this environment). Full suite: identical set of 22 failing suites as on main (not related to this change). npx tsc diagnostics and npx eslint on the touched files: no new findings.

Verification

Found and verified live on LibreChat v0.8.9 with web_search_20260318, claude-opus-5-5 and 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).

…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.
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).
danny-avila pushed a commit that referenced this pull request Nov 23, 2026
…#736)

Retain native results and caller linkage through both stream paths and replay. Scope automatic container reuse to the current user turn.

Design credits: tang-ontheway (#720) and roman-redl (#731, #732; reported #690).

Co-authored-by: Lia <lia@librechat.ai>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant