Agentic run with dynamic filtering (web_search_20260209 inside code_execution) rejected with "code_execution tool use ... without a corresponding code_execution_tool_result block
Submission checklist
- This is a bug, not a usage question.
- I added a clear and descriptive title that summarizes this issue.
- I used the GitHub search to find a similar question and didn't find it.
- I am sure that this is a bug in LangChain rather than my code.
- The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
- This is not related to the langchain-community package.
- I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.
Package (Required)
- langchain
- langchain-openai
- langchain-anthropic
- langchain-classic
- langchain-core
- langchain-model-profiles
- langchain-tests
- langchain-text-splitters
- langchain-chroma
- langchain-deepseek
- langchain-exa
- langchain-fireworks
- langchain-groq
- langchain-huggingface
- langchain-mistralai
- langchain-nomic
- langchain-ollama
- langchain-openrouter
- langchain-perplexity
- langchain-qdrant
- langchain-typesafe
- langchain-xai
- Other / not sure / general
Related Issues / PRs
https://github.com/vercel/ai/pull/13046 https://github.com/vercel/ai/issues/13040 https://github.com/vercel/ai/issues/18785
Reproduction Steps / Example Code (Python)
from __future__ import annotations
import os
from collections.abc import Sequence
from dotenv import load_dotenv
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
from langchain_anthropic import ChatAnthropic
from langchain.agents import create_agent
load_dotenv()
# Official upgrade / Cloud Exchange documentation root (search is constrained to this host).
ALLOWED_DOMAIN = "" # some domain for web-search to traverse
# Built-in Anthropic web search tool (passed through to the API unchanged by ChatAnthropic).
# See: https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/web-search-tool
WEB_SEARCH_TOOL: dict = {
"type": "web_search_20260209",
"name": "web_search",
"max_uses": 5,
"allowed_domains": [ALLOWED_DOMAIN],
}
SYSTEM_PROMPT = """""" # system prompt for the agent to when to go and hit the web search.
def build_agent():
if not os.environ.get("ANTHROPIC_API_KEY"):
raise RuntimeError("Set ANTHROPIC_API_KEY in your environment.")
llm = ChatAnthropic(
model=os.environ.get("ANTHROPIC_MODEL", "claude-sonnet-5"),
)
return create_agent(
llm,
tools=[WEB_SEARCH_TOOL],
system_prompt=SYSTEM_PROMPT,
debug=True,
)
def _message_visible_text(message: BaseMessage) -> str:
"""Plain text parts only (skips tool_use, web_search_tool_result, encrypted blocks, etc.)."""
content = message.content
if isinstance(content, str):
return content
if not isinstance(content, list):
return ""
parts: list[str] = []
for block in content:
if isinstance(block, dict) and block.get("type") == "text":
t = block.get("text")
if isinstance(t, str):
parts.append(t)
return "".join(parts)
def _final_assistant_answer(messages: Sequence[BaseMessage]) -> str:
for m in reversed(messages):
if isinstance(m, AIMessage):
text = _message_visible_text(m)
if text.strip():
return text
return ""
if __name__ == "__main__":
query = "some nested web search request"
agent = build_agent()
result = agent.invoke({"messages": [HumanMessage(content=query)]})
messages = result["messages"]
for m in messages:
m.pretty_print()
answer = _final_assistant_answer(messages)
print(answer)Error Message and Stack Trace (if applicable)
While using the web-search with dynamic filtering enabled from anthropic I'm facing below mentioned error:
code_execution` tool use with id `srvtoolu_015m5Yth4v9NRFzSXkaY1dCA` was found without a corresponding `code_execution_tool_result` block
Full traceback:
Traceback (most recent call last):
File "<path>/utils/llm_invoke.py", line 994, in invoke_agent_with_tracking
async for event in graph.astream_events(
File "/opt/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 1658, in _astream_events_v1_v2
async for event in event_stream:
File "/opt/venv/lib/python3.12/site-packages/langchain_core/tracers/event_stream.py", line 1105, in _astream_events_implementation_v2
await task
File "/opt/venv/lib/python3.12/site-packages/langchain_core/tracers/event_stream.py", line 1060, in consume_astream
async for _ in event_streamer.tap_output_aiter(run_id, stream):
File "/opt/venv/lib/python3.12/site-packages/langchain_core/tracers/event_stream.py", line 224, in tap_output_aiter
async for chunk in output:
File "/opt/venv/lib/python3.12/site-packages/langgraph/pregel/main.py", line 3440, in astream
async for _ in runner.atick(
File "/opt/venv/lib/python3.12/site-packages/langgraph/pregel/_runner.py", line 396, in atick
await arun_with_retry(
File "/opt/venv/lib/python3.12/site-packages/langgraph/pregel/_retry.py", line 741, in arun_with_retry
async for _ in task.proc.astream(task.input, config):
File "/opt/venv/lib/python3.12/site-packages/langgraph/_internal/_runnable.py", line 867, in astream
output = await asyncio.create_task(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langgraph/_internal/_runnable.py", line 932, in _consume_aiter
async for chunk in it:
File "/opt/venv/lib/python3.12/site-packages/langchain_core/tracers/event_stream.py", line 201, in tap_output_aiter
first = await anext(output, sentinel)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 1818, in atransform
async for ichunk in input:
File "/opt/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 1818, in atransform
async for ichunk in input:
File "/opt/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 1222, in astream
yield await self.ainvoke(input, config, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langgraph/_internal/_runnable.py", line 501, in ainvoke
ret = await self.afunc(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain/agents/factory.py", line 1498, in amodel_node
result = await awrap_model_call_handler(request, _execute_model_async)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain/agents/factory.py", line 402, in composed
outer_result = await outer(request, inner_handler)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<path>/utils/llm_invoke.py", line 123, in awrap_model_call
return await handler(self._apply(request))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain/agents/factory.py", line 392, in inner_handler
inner_result = await inner(req, handler)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain/agents/middleware/model_retry.py", line 291, in awrap_model_call
return self._handle_failure(exc, attempts_made)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain/agents/middleware/model_retry.py", line 204, in _handle_failure
raise exc
File "/opt/venv/lib/python3.12/site-packages/langchain/agents/middleware/model_retry.py", line 284, in awrap_model_call
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain/agents/factory.py", line 1467, in _execute_model_async
output = await model_.ainvoke(messages)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 6015, in ainvoke
return await self.bound.ainvoke(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 499, in ainvoke
llm_result = await self.agenerate_prompt(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 1860, in agenerate_prompt
return await self.agenerate(
^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 1818, in agenerate
raise exceptions[0]
File "/opt/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 2151, in _agenerate_with_cache
result = await self._agenerate(
^^^^^^^^^^^^^^^^^^^^^^
File "<some-other-path>/lib/langchain_anthropic/chat_models.py", line 1984, in _agenerate
_handle_anthropic_bad_request(e)
File "<some-other-path>/lib/langchain_anthropic/chat_models.py", line 1982, in _agenerate
data = await self._acreate(payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<some-other-path>/lib/langchain_anthropic/chat_models.py", line 1603, in _acreate
return await self._async_client.messages.create(**payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<some-other-path>/lib/anthropic/resources/messages/messages.py", line 2545, in create
return await self._post(
^^^^^^^^^^^^^^^^^
File "<some-other-path>/lib/anthropic/_base_client.py", line 2276, in post
return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<some-other-path>/lib/anthropic/_base_client.py", line 1941, in request
raise self._make_status_error_from_response(response) from None
anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.1: `code_execution` tool use with id `srvtoolu_015m5Yth4v9NRFzSXkaY1dCA` was found without a corresponding `code_execution_tool_result` block'}, 'request_id': 'req_011CfLKWqV8XDxQ8xWLzZioT'}
During task with name 'model' and id '3428ad98-a3a8-040a-aaf2-d05e0f31f8ad'Description
I'm using the below mentioned versions of the langchain libs: langchain==1.3.14 langchain-core==1.5.3 langgraph==1.2.10 langsmith==0.10.16 anthropic==0.120.2 langchain-anthropic==1.5.4
The web-search tool version: web_search_20260209
The issue is intermittent, it usually occurs when there are deep web-search performed by anthropic using its code_execution tool which automatically gets enabled when web_search_20260209 tool is used.
System Info
System Information
OS: Linux OS Version: #189-Ubuntu SMP Tue May 5 18:20:56 UTC 2026 Python Version: 3.12.13 (main, Mar 4 2026, 09:23:07) [GCC 11.4.0]
Package Information
langchain_core: 1.5.3 langchain: 1.3.14 langsmith: 0.10.16 langchain_protocol: 0.0.19 langgraph_sdk: 0.4.5
Optional packages not installed
deepagents deepagents-cli
Other Dependencies
anyio: 4.9.0 cachetools: 5.5.2 distro: 1.9.0 httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.2.10 opentelemetry-api: 1.34.1 opentelemetry-exporter-otlp-proto-http: 1.34.1 opentelemetry-sdk: 1.34.1 orjson: 3.11.5 packaging: 25.0 pydantic: 2.11.3 pytest: 8.3.4 pyyaml: 6.0.2 requests: 2.33.0 requests-toolbelt: 1.0.0 rich: 14.0.0 sniffio: 1.3.1 tenacity: 9.1.4 typing-extensions: 4.13.2 uuid-utils: 0.17.1 websockets: 15.0.1 wrapt: 1.17.2 xxhash: 4.0.1 zstandard: 0.25.0
Social handles (optional)
LinkedIn: https://www.linkedin.com/in/neel004/
Source: langchain-ai/langchain