Skip to content

Anthropic web_search_20260209 (dynamic filtering): code_execution_tool_result is dropped and caller is stripped, so the next request fails with HTTP 400 #690

Closed

Description

@roman-redl

Summary

Anthropic's newer web_search_20260209 tool filters search results with code execution before they enter the context. It cuts input tokens a lot, but cannot be used with @librechat/agents today: after a turn that used it, the next request that continues the same assistant message (for example after a client/MCP tool call) fails with HTTP 400, because the code_execution_tool_result block is dropped and the caller field is stripped.

What we tried and why

Same 6-search question sent straight to the Anthropic API, claude-opus-5-5:

Tool version Input tokens
web_search_20250305 224,120
web_search_20260209 36,987 - 40,701

That is roughly 5.5x fewer input tokens, so we tried to switch LibreChat to the new version. LibreChat hardcodes { type: 'web_search_20250305', name: 'web_search' } (see Environment), so we patched the type to web_search_20260209 locally. The search itself works. The first request after a server search turn plus a client (MCP) tool call fails:

400 messages.1: `code_execution` tool use with id `srvtoolu_...` was found without a corresponding `code_execution_tool_result` block

Root cause

With web_search_20260209 the model calls code_execution (server tool, srvtoolu_ id) and the nested web_search calls carry caller: { type: 'code_execution_20260120', tool_id: '<srvtoolu_ id of the code_execution call>' }. The assistant message then contains server_tool_use (code_execution), server_tool_use (web_search, with caller), web_search_tool_result (with caller) and code_execution_tool_result. All of them have to be replayed unchanged.

Checked on main at b3387e6 (v4.2.5). All three problems were also reproduced with a throwaway unit test calling the functions below directly.

  1. The stream parser drops code_execution_tool_result. _makeMessageChunkFromAnthropicEvent only turns tool_use, document, server_tool_use and web_search_tool_result block starts into chunks; any other block type reaches the final return null, so the block never enters the aggregated message. This is the path used by _streamResponseChunks. The newer convertAnthropicStream maps unknown types to non_standard, so it keeps them.

    Probe: a content_block_start with type: 'code_execution_tool_result' returns null. A nested server_tool_use block keeps its caller at this stage.

  2. _formatContent rebuilds every srvtoolu_ block as { type: 'server_tool_use', id, name, input }, which drops caller.

    • * Normalize server_tool_use blocks into a clean shape the API accepts.
      * These blocks may arrive with the correct type (server_tool_use), as a
      * standardized server_tool_call, or mislabeled after state serialization.
      * Regardless of current type, if the id starts with 'srvtoolu_' we rebuild
      * a clean block with only the properties the API expects.
      */
      if (
      'id' in contentPart &&
      typeof (contentPart as Record<string, unknown>).id === 'string' &&
      ((contentPart as Record<string, unknown>).id as string).startsWith(
      Constants.ANTHROPIC_SERVER_TOOL_PREFIX
      ) &&
      'name' in contentPart
      ) {
      const rawPart = contentPart as Record<string, unknown>;
      const corrected: AnthropicServerToolUseBlockParam = {
      type: 'server_tool_use',
      id: rawPart.id as string,
      name: (rawPart.name ?? 'web_search') as 'web_search',
      input: coerceAnthropicToolUseInput(rawPart.input ?? rawPart.args),
      };
      return corrected;
      }

    web_search_tool_result is rebuilt the same way with only type, tool_use_id and content, so its caller is dropped too:

    • /**
      * Normalize web_search_tool_result blocks into a clean shape.
      * Same rationale as above — the block may carry extra properties from
      * streaming (input, index, etc.) that the API rejects. Rebuild cleanly.
      */
      if (
      'tool_use_id' in contentPart &&
      typeof (contentPart as Record<string, unknown>).tool_use_id ===
      'string' &&
      (
      (contentPart as Record<string, unknown>).tool_use_id as string
      ).startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) &&
      'content' in contentPart
      ) {
      const rawPart = contentPart as Record<string, unknown>;
      const content = rawPart.content;
      const isValidContent =
      Array.isArray(content) ||
      (content != null &&
      typeof content === 'object' &&
      'type' in content &&
      (content as Record<string, unknown>).type ===
      'web_search_tool_result_error');
      if (isValidContent) {
      const corrected: AnthropicWebSearchToolResultBlockParam = {
      type: 'web_search_tool_result',
      tool_use_id: rawPart.tool_use_id as string,
      content:
      content as AnthropicWebSearchToolResultBlockParam['content'],
      };
      return corrected;
      }
      return null;
      }
  3. Any srvtoolu_ result whose content is neither an array nor a web_search_tool_result_error is replaced by null, which removes code_execution_tool_result (its content is an object of type code_execution_result). The type is also missing from the toolTypes list at the top of the function.

    • const toolTypes = [
      'tool_use',
      'tool_result',
      'input_json_delta',
      'server_tool_use',
      'web_search_tool_result',
      'web_search_result',
      ];
    • const isValidContent =
      Array.isArray(content) ||
      (content != null &&
      typeof content === 'object' &&
      'type' in content &&
      (content as Record<string, unknown>).type ===
      'web_search_tool_result_error');
      if (isValidContent) {
      const corrected: AnthropicWebSearchToolResultBlockParam = {
      type: 'web_search_tool_result',
      tool_use_id: rawPart.tool_use_id as string,
      content:
      content as AnthropicWebSearchToolResultBlockParam['content'],
      };
      return corrected;
      }
      return null;

Probe on the replay side: an assistant message with a nested server_tool_use (with caller) followed by a code_execution_tool_result is formatted as [server_tool_use (no caller), text]; the code_execution_tool_result is gone, which is exactly what the 400 reports. A code_execution server_tool_use followed by a nested web_search and its web_search_tool_result is formatted without any caller.

Even when the stream keeps the block (path 1 fixed), step 3 would drop it again, and a type that is not recognised at all would reach the Unsupported message content format throw at the end of _formatContent. So both sides need to change.

UI side effect

code_execution and the nested web_search calls show up in the LibreChat UI as failed tool calls with empty args and output parts. This is related to the server-tool run-step handling (see #575) and, for streams with several searches, to the eager-execution defect fixed separately in #689 (server srvtoolu_ calls dispatched as on_tool_execute, "Tool web_search not found").

Suggested fix

In @librechat/agents:

  1. Stream: keep code_execution_tool_result (and the other Anthropic server tool result types: web_fetch_tool_result, bash_code_execution_tool_result, text_editor_code_execution_tool_result, tool_search_tool_result) when aggregating chunks in _makeMessageChunkFromAnthropicEvent, as convertAnthropicStream already does.
  2. Replay: preserve caller on rebuilt server_tool_use and web_search_tool_result blocks when present.
  3. Replay: pass through server tool result blocks whose tool_use_id starts with srvtoolu_ for every server result type, not only web_search_tool_result; rebuild them without stream-only properties (index) but keep content and caller.
  4. Add replay tests for a code_execution + nested web_search message.

In LibreChat: make the web search tool version configurable (for example an endpoint-level option such as webSearchToolVersion, defaulting to web_search_20250305) so deployments can opt in to web_search_20260209 once replay is fixed. The version is currently hardcoded.

Environment

  • LibreChat v0.8.8
  • @librechat/agents 4.0.1
  • Anthropic provider, claude-opus-5-5, built-in web search enabled
  • Confirmed the relevant code is unchanged on LibreChat-AI/agents main at b3387e6 (v4.2.5), and LibreChat-AI/LibreChat main at 9529c99 (v0.8.9-rc1, depends on @librechat/agents ^4.2.4) still hardcodes web_search_20250305.

Activity

  1. roman-redl commented on Nov 22, 2026

    @roman-redl
    ContributorAuthor

    To make dynamic filtering (web_search_20260209 and later) work end to end:

    Test details and numbers: #720 (comment)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions