Skip to content

Incompatible Tool Result Handling with Anthropic Server Tools in LangChain #8546

@Diluka

Description

@Diluka

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • 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.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import 'dotenv/config';
import { ChatAnthropic } from '@langchain/anthropic';
import { createReactAgent } from '@langchain/langgraph/prebuilt';

const llm = new ChatAnthropic({
  model: 'claude-sonnet-4-20250514',
});

const agent = createReactAgent({
  llm,
  tools: [
    {
      type: 'web_search_20250305',
      name: 'web_search',
      max_uses: 5,
    },
  ],
});

const stream = await agent.stream({ messages: `search today's news` });

for await (const element of stream) {
  console.log(element);
}

Error Message and Stack Trace (if applicable)

[Running] deno run -A "e:\Sources\langchain-demo\main.ts"
{
  agent: {
    messages: [
      AIMessage {
        "id": "msg_01JLSycfL4c8D2ebwMaj4vDc",
        "content": [
          {
            "type": "server_tool_use",
            "id": "srvtoolu_01NUzp5NVn6BnASdy7yAav7U",
            "name": "web_search",
            "input": {
              "query": "news today July 21 2025"
            }
          },
          {
            "type": "web_search_tool_result",
            "tool_use_id": "srvtoolu_01NUzp5NVn6BnASdy7yAav7U",
            "content": [
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]"
            ]
          },
          {
            "type": "text",
            "text": "Let me search for more current breaking news from today:"
          },
          {
            "type": "server_tool_use",
            "id": "srvtoolu_01FQQFoazxkSmhBtKGf1oxBa",
            "name": "web_search",
            "input": {
              "query": "breaking news today July 21"
            }
          },
          {
            "type": "web_search_tool_result",
            "tool_use_id": "srvtoolu_01FQQFoazxkSmhBtKGf1oxBa",
            "content": [
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]"
            ]
          },
          {
            "type": "text",
            "text": "Let me search for more comprehensive current news from major news sources:"
          },
          {
            "type": "server_tool_use",
            "id": "srvtoolu_01SY5wigi3ByD7toHSGJcTtD",
            "name": "web_search",
            "input": {
              "query": "major news headlines July 21 2025"
            }
          },
          {
            "type": "web_search_tool_result",
            "tool_use_id": "srvtoolu_01SY5wigi3ByD7toHSGJcTtD",
            "content": [
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]",
              "[Object]"
            ]
          },
          {
            "type": "text",
            "text": "Based on my search results, here are the key news stories from today (July 21, 2025):\n\n## Major International News\n\n"
          },
          {
            "citations": [
              "[Object]"
            ],
            "type": "text",
            "text": "**Earthquake and Tsunami Alert**: A powerful 7.4 magnitude earthquake struck near Petropavlovsk-Kamchatsky, Kamchatka, Russia's Pacific Coast. With a shallow depth of 20 km, the quake raised tsunami concerns and authorities are monitoring risks of aftershocks and waves."
          },
          {
            "type": "text",
            "text": "\n\n"
          },
          {
            "citations": [
              "[Object]"
            ],
            "type": "text",
            "text": "**Japan Election**: Japanese voters are heading to polls today in a critical upper house election that could end PM Shigeru Ishiba's leadership. Inflation, U.S. tariffs, and populist challenges are reshaping Japan's domestic and international direction."
          },
          {
            "type": "text",
            "text": "\n\n"
          },
          {
            "citations": [
              "[Object]"
            ],
            "type": "text",
            "text": "**Gaza Conflict**: Israeli military has issued new evacuation warnings in central Gaza near Deir al-Balah, cutting access to Rafah and Khan Younis, as ceasefire talks with Hamas continue without breakthrough."
          },
          {
            "type": "text",
            "text": "\n\n"
          },
          {
            "citations": [
              "[Object]",
              "[Object]"
            ],
            "type": "text",
            "text": "**Ukraine Conflict**: European Commissioner for Defense and Space Andrius Kubilius warned of escalating Russian attacks on Ukraine and called for more Patriots. The Kremlin has stated it's open to talks but demands remain unchanged."
          },
          {
            "type": "text",
            "text": "\n\n## U.S. News\n\n"
          },
          {
            "citations": [
              "[Object]"
            ],
            "type": "text",
            "text": "A parliamentary panel is scheduled to table a report on the Income Tax Bill 2025, aiming to replace the six-decade-old tax act with reforms."
          },
          {
            "type": "text",
            "text": "\n\n"
          },
          {
            "citations": [
              "[Object]"
            ],
            "type": "text",
            "text": "**Development News**: NITI Aayog highlighted India's SDG progress, noting that 240 million people have been lifted from poverty and social protection coverage has doubled since 2013."
          },
          {
            "type": "text",
            "text": "\n\n"
          },
          {
            "citations": [
              "[Object]"
            ],
            "type": "text",
            "text": "**School Holiday**: Several districts and states have announced July 21 as a school holiday, with the Telangana government releasing an official notification for schools across Hyderabad, Secunderabad, and surrounding districts to observe a general holiday for the celebration of Bonalu 2025."
          },
          {
            "type": "text",
            "text": "\n\n## Weather Alerts\n\n"
          },
          {
            "citations": [
              "[Object]"
            ],
            "type": "text",
            "text": "**Severe Weather**: Uttarakhand's Met Department has forecast heavy to very heavy rainfall in several districts today and tomorrow, with an orange warning covering multiple affected regions and district administrations directed to stay alert."
          },
          {
            "type": "text",
            "text": "\n\nThe news today reflects ongoing global tensions, natural disasters, political developments, and regional celebrations, with particular focus on the major earthquake in Russia, Japan's crucial election, and continued Middle East conflicts."
          }
        ],
        "additional_kwargs": {
          "id": "msg_01JLSycfL4c8D2ebwMaj4vDc",
          "type": "message",
          "role": "assistant",
          "model": "claude-sonnet-4-20250514",
          "stop_reason": "end_turn",
          "stop_sequence": null,
          "usage": {
            "input_tokens": 61312,
            "cache_creation_input_tokens": 0,
            "cache_read_input_tokens": 0,
            "output_tokens": 859,
            "service_tier": "standard",
            "server_tool_use": {
              "web_search_requests": 3
            }
          }
        },
        "response_metadata": {
          "id": "msg_01JLSycfL4c8D2ebwMaj4vDc",
          "model": "claude-sonnet-4-20250514",
          "stop_reason": "end_turn",
          "stop_sequence": null,
          "usage": {
            "input_tokens": 61312,
            "cache_creation_input_tokens": 0,
            "cache_read_input_tokens": 0,
            "output_tokens": 859,
            "service_tier": "standard",
            "server_tool_use": {
              "web_search_requests": 3
            }
          },
          "type": "message",
          "role": "assistant"
        },
        "tool_calls": [
          {
            "name": "web_search",
            "args": {
              "query": "news today July 21 2025"
            },
            "id": "srvtoolu_01NUzp5NVn6BnASdy7yAav7U",
            "type": "tool_call"
          },
          {
            "name": "web_search",
            "args": {
              "query": "breaking news today July 21"
            },
            "id": "srvtoolu_01FQQFoazxkSmhBtKGf1oxBa",
            "type": "tool_call"
          },
          {
            "name": "web_search",
            "args": {
              "query": "major news headlines July 21 2025"
            },
            "id": "srvtoolu_01SY5wigi3ByD7toHSGJcTtD",
            "type": "tool_call"
          }
        ],
        "invalid_tool_calls": [],
        "usage_metadata": {
          "input_tokens": 61312,
          "output_tokens": 859,
          "total_tokens": 62171,
          "input_token_details": {
            "cache_creation": 0,
            "cache_read": 0
          }
        }
      }
    ]
  }
}
{
  tools: {
    messages: [
      ToolMessage {
        "id": "6b3eb7ec-f129-4513-b0fe-04225e16e652",
        "content": "Error: Tool \"web_search\" not found.\n Please fix your mistakes.",
        "name": "web_search",
        "additional_kwargs": {},
        "response_metadata": {},
        "tool_call_id": "srvtoolu_01NUzp5NVn6BnASdy7yAav7U"
      },
      ToolMessage {
        "id": "4c62a9d5-ae44-49ac-b491-284e63c08f63",
        "content": "Error: Tool \"web_search\" not found.\n Please fix your mistakes.",
        "name": "web_search",
        "additional_kwargs": {},
        "response_metadata": {},
        "tool_call_id": "srvtoolu_01FQQFoazxkSmhBtKGf1oxBa"
      },
      ToolMessage {
        "id": "0f9c5c37-0046-490b-b533-529c586dd4bb",
        "content": "Error: Tool \"web_search\" not found.\n Please fix your mistakes.",
        "name": "web_search",
        "additional_kwargs": {},
        "response_metadata": {},
        "tool_call_id": "srvtoolu_01SY5wigi3ByD7toHSGJcTtD"
      }
    ]
  }
}
�[0m�[1m�[31merror�[0m: Uncaught (in promise) Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.2.content.0: unexpected `tool_use_id` found in `tool_result` blocks: srvtoolu_01NUzp5NVn6BnASdy7yAav7U. Each `tool_result` block must have a corresponding `tool_use` block in the previous message."}}

Troubleshooting URL: https://js.langchain.com/docs/troubleshooting/errors/INVALID_TOOL_RESULTS/

Description

I tried to use the latest @langchain/anthropic and @langchain/langgraph to create an agent to call server tools. However, I saw that the intermediate process was successfully processed, but the last step went wrong.

System Info

latest version

Related

#8302

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions