Skip to content

Unable to use anthropic's web search tool #8691

@lzj960515

Description

@lzj960515

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 'npm:dotenv/config';
import { ChatAnthropic } from 'npm:@langchain/anthropic';
import { createReactAgent } from 'npm:@langchain/langgraph/prebuilt';
import { MemorySaver } from 'npm:@langchain/langgraph-checkpoint';

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

const agent = createReactAgent({
  llm,
  tools: [
    {
      type: 'web_search_20250305',
      name: 'web_search',
      max_uses: 3,
    },
  ],
  name: 'Web Search Agent',
  checkpointer: new MemorySaver(),
});

const stream1 = await agent.stream(
  { messages: 'What is the capital of France?' },
  { streamMode: ['messages'], configurable: { thread_id: '1' } },
);
for await (const it of stream1) {
  console.log(it);
}

const stream2 = await agent.stream(
  { messages: 'How many people live in France?' },
  { streamMode: ['messages'], configurable: { thread_id: '1' } },
);
for await (const it of stream2) {
  console.log(it);
}

Error Message and Stack Trace (if applicable)

400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.4.content.0: unexpected tool_use_id found in tool_result blocks: fallback-0. Each tool_result block must have a corresponding tool_use block in the previous message."}}

Description

This is because when Claude uses web search, it returns a message like this

{
type: 'content_block_delta',
index: 1,
delta: { type: 'input_json_delta', partial_json: '"}' }
}

In langchain, this will result in a tool call:

} else if (
data.type === "content_block_delta" &&
data.delta.type === "input_json_delta"
) {
return {
chunk: new AIMessageChunk({
content: fields.coerceContentToString
? ""
: [
{
index: data.index,
input: data.delta.partial_json,
type: data.delta.type,
},
],
additional_kwargs: {},
tool_call_chunks: [
{
index: data.index,
args: data.delta.partial_json,
},
],
}),
};
} else if (

const groupedToolCallChunk = fields.tool_call_chunks.reduce(
(acc, chunk) => {
// Assign a fallback ID if the chunk doesn't have one
// This can happen with tools that have empty schemas
const chunkId = chunk.id || `fallback-${chunk.index || 0}`;
acc[chunkId] = acc[chunkId] ?? [];
acc[chunkId].push(chunk);
return acc;
},
{} as Record<string, ToolCallChunk[]>
);

Finally, langgraphql thinks that tool call exists:

libs/langgraph/src/prebuilt/react_agent_executor.ts

const { messages } = state;
        const lastMessage = messages[messages.length - 1];

        // if there's no function call, we finish
        if (!isAIMessage(lastMessage) || !lastMessage.tool_calls?.length) {
          if (responseFormat != null) return "generate_structured_response";
          return END;
        }

        // there are function calls, we continue
        if (version === "v2") {
          return lastMessage.tool_calls.map(
            (toolCall) =>
              new Send("tools", { ...state, lg_tool_call: toolCall })
          );
        }

        return "tools";

This finally led to the error

System Info

.

Metadata

Metadata

Assignees

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