ISSUE DESCRIPTION Anthropic rejects duplicate websearch tool names When LibreChat web…
- User
ISSUE DESCRIPTION
Anthropic rejects duplicate web_search tool names
When LibreChat web search and Anthropic-native web search are both enabled, expose one
web_searchtool and prefer LibreChat's definition. Do not affect unrelated tools or other providers.Interface
No new public interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /LibreChat, current file: none) bash-$
Assistant
I'll start by exploring the repository structure to understand the codebase.
Assistant
Let me search for Anthropic-related web search handling:
Assistant
Let me look for where Anthropic tools are defined/added:
Assistant
Now let me find where the web_search tool is added to a request for Anthropic:
Assistant
Found it! Let me look at the relevant section in the anthropic LLM:
Assistant
Now let me look at the relevant tests to understand the existing functionality:
Assistant
Now let me look at where
toolsis consumed and how the Anthropic request is constructed:Assistant
Now let's find where these tools are merged with other tools (like the LibreChat ones) before being sent to the Anthropic API:
Assistant
Now I see the architecture. The Anthropic tools (with
name: 'web_search') come fromgetAnthropicLLMConfig, and there are also tools from LibreChat itself. Let me find where these are merged:Assistant
Let me look at where the tools get sent to the LLM and find the actual merge logic:
Assistant
Now let's look at how the LibreChat web_search tool gets transformed for Anthropic:
Assistant
Now let me find where the actual tools from LibreChat (and the LLM provider) are merged:
Assistant
Let me search for where anthropic tools are bound:
Assistant
Now let me see how the
result.toolsfrom getOpenAIConfig is merged with the LibreChat native web_search tools. Let me look at where the actual chat completion request happens:Assistant
Now I understand! In the
initialize.tsfile, whenagent.providerisANTHROPICand bothoptions.tools(fromgetOpenAIConfig) andstructuredToolsare present, they are concatenated. Theoptions.toolswould include the Anthropic-nativeweb_searchtool, andstructuredToolswould include the LibreChat web_search tool. This causes the duplicate.Let me verify by looking at
structuredTools:Assistant
Now let me trace where the tools are turned into Anthropic format. Let me search for how these tools are sent:
Assistant
Let me look at the broader flow of when the request actually goes to Anthropic:
Assistant
Let me look at the createPayload or similar flow:
Assistant
Now let me understand where these tools get translated to Anthropic format. Let me check where
structuredToolsis converted into tool format for the actual LLM call:Assistant
Let me look at how the OpenAI/Anthropic client processes tools at runtime:
Assistant
Now let me look at the actual issue. The flow is:
- The LLM config generates a
web_searchtool withtype: 'web_search_20250305'(Anthropic-native format) - The user enables LibreChat web_search through the UI / agents config
- The LibreChat web_search tool is in the agent's
toolsarray - When
loadToolsruns, the LibreChat web_search tool is loaded intostructuredTools(aDynamicStructuredTool) - When
initialize.tsruns for Anthropic provider, it concatenatesstructuredTools.concat(options.tools) - So we'd end up with both the LibreChat web_search tool AND the Anthropic-native web_search tool sent to the Anthropic API
Now let's look at where this happens to understand the order and trace where the duplicate would arise:
- The LLM config generates a
Assistant
Now I see this is the problem. Let me look more carefully:
Assistant
Let me check the structure of
options.tools:Assistant
Now let me trace where
toolsfromoptions(theoptions.toolsfield) gets to. Let me look at howoptionsare constructed:Assistant
Now let me look at where the tools from
getLLMConfigare returned and processed:Assistant
Let me check the InitializeResultBase type:
Assistant
Now let me check where the duplicate issue actually manifests. Let me look at the
formatStructuredToolToAnthropicfunction more carefully:Assistant
Now I understand the picture. The
toolsfromgetLLMConfigare raw objects withtypeandname, like{type: 'web_search_20250305', name: 'web_search'}. They will be passed inagent.tools(oroptions.tools) array. ThenloadToolsloads LibreChat'sweb_searchtool. The finaltoolsarray then has both, which gets sent to the LLM.Let me look at
bindToolsto understand exactly what gets sent to Anthropic API:Assistant
Now let me look at
isBuiltinToolmore carefully - it checks if the tool has type starting withweb_search_. The Anthropic-native tool will pass this check. The LibreChatweb_searchtool when loaded asDynamicStructuredToolwill get formatted byisLangChainTooltoname: 'web_search'. So we'd end up with two tools having nameweb_search- one from LibreChat and one from Anthropic - causing the duplicate.Now let me write a reproduction script to confirm:
Assistant
Now let me look at the request flow more comprehensively. Let me see how this happens at a high level - what happens with agent.tools:
Assistant
Now I have a clear picture. Let me look at the exact flow more carefully:
- The agent config has
tools: ['web_search'] - The
loadToolsfunction loads the LibreChat web_search tool (DynamicStructuredTool) intostructuredTools - The
getLLMConfigreturnstools: [{type: 'web_search_20250305', name: 'web_search'}]from Anthropic - In
initialize.ts, line 846:tools = structuredTools.concat(options.tools as GenericTool[]) - This produces
[librechat_websearchTool, anthropic_websearchTool]both with nameweb_search - When sent to Anthropic, the Anthropic API rejects duplicate tool names
Let me look at where the agent.tools get bound to langchain. Let me trace it further:
- The agent config has
Assistant
Now let me look at where the request actually gets sent to the LLM. Let me trace where tools flow:
Assistant
So BaseClient.js doesn't have any tool handling. Let me look at OpenAIClient or any custom client:
Assistant
Now let me look at where the actual
toolsare translated for the OpenAI/Anthropic call. Let me look atopenai.js:Assistant
Now let me look at the test file for
initialize.tsto understand the expectations:Assistant
Now I understand. The issue is in
initialize.tsline 829-846. Let me write a test to reproduce:Assistant
Let me search for tests that test the existing behavior of
options.toolsbeing concatenated withstructuredTools:Assistant
Now let me look at how the function calls the
loadToolsto actually load the LibreChat web_search tool:Assistant
Now let me think about the actual issue more carefully. Let me look at the existing tests for
initialize.tsfor tools:Assistant
Now I understand the structure. Let me write a script to reproduce the bug:
Assistant
Now let me also explore how the actual flow to Anthropic happens. Let me search for the request creation path:
Assistant
Now let me look at the langchain anthropic SDK to confirm how duplicate tool names cause errors:
Assistant
The langchain SDK doesn't have explicit duplicate validation, but the Anthropic API will reject it. Let me look at how the actual Anthropic request is made:
Assistant
Now let me trace exactly what gets sent. Let me look at the structure of the final tools array in initialize.ts:
Assistant
Now let me look at how the request flow uses the result of initializeAgent. I need to confirm this gets bound to langchain and sent. Let me search for where the LLM is created and tools are bound:
Assistant
Now I confirm that
formatStructuredToolToAnthropicis called onoptions?.tools, which is the final tools array frominitialize.ts. If it contains both the LibreChat web_search tool AND the Anthropic-native web_search tool, both will get formatted toname: 'web_search'which the Anthropic API will reject.Let me write a reproduction script:
Assistant
I've successfully reproduced the issue. Now I need to find the right place to fix this.
Looking at the issue description again:
When LibreChat web search and Anthropic-native web search are both enabled, expose one
web_searchtool and prefer LibreChat's definition. Do not affect unrelated tools or other providers.The fix should:
- Expose only ONE
web_searchtool - Prefer LibreChat's definition
- Not affect other tools or providers
The cleanest place to fix this is in
initialize.tswhere the concatenation happens. Or we could do it in the Anthropicllm.tswhere it generates the tool list. Let me think about both options:Option 1: Fix in
initialize.ts: When the agent provider is Anthropic, filter out the Anthropic-native web_search tool fromoptions.toolsif the LibreChatstructuredToolscontains a web_search tool.Option 2: Fix in
anthropic/llm.ts: Don't add the web_search tool totoolsifweb_searchis in the model parameters. But this won't help because we can't tell whether LibreChat's web_search is loaded.Option 3: Fix in
initialize.ts: Use a single tool — the LibreChat one when available — for bothoptions.tools(LLM-level) andstructuredTools.The best approach is Option 1. When the agent uses Anthropic and both LibreChat web_search and Anthropic-native web_search are enabled, we should:
- Skip the Anthropic-native web_search tool from
options.tools - Keep LibreChat's web_search tool
Let me look at the current structure to implement the fix:
- Expose only ONE
Assistant
Now let me look at the existing imports in initialize.ts:
Assistant
Good. Now let me check the imports for Tools.web_search:
Assistant
Let me check what
Tools.web_searchevaluates to:Assistant
So
Tools.web_search === 'web_search'. Now let me look at theTools.web_searchimport and what name does it have for the LibreChat tool:Assistant
Good,
WebSearchToolName === 'web_search'. Now I need to determine where to make the fix. Let me think about the design carefully.The issue is that the tools in
options.tools(returned fromgetLLMConfig) and the LibreChat-native tools instructuredToolsboth end up with the name'web_search', and when concatenated, they become duplicates.The fix should:
- When the agent provider is Anthropic AND LibreChat's web_search tool is loaded, exclude the Anthropic-native
web_searchtool fromoptions.tools. - Or alternatively, when concatenating tools for Anthropic provider, deduplicate by tool name.
The simplest and cleanest approach is option 1: filter out the Anthropic-native
web_searchtool fromoptions.toolswhen the LibreChat web_search tool is present instructuredTools.Let me look more carefully at the structure to ensure the right approach:
- When the agent provider is Anthropic AND LibreChat's web_search tool is loaded, exclude the Anthropic-native
Assistant
Now let me also confirm my understanding of
isBuiltinTool. Theweb_search_20250305tool hastypeandnamefields, so it should be detected as a builtin tool.Let me also look at
isLangChainToolto confirm aDynamicStructuredToolfrom LangChain would match it:Assistant
Now I have a clear understanding. Let me look at how the tools from the dynamicStructuredTool look - they would be
StructuredToolinstances withlc_namespace. The Anthropic-native web_search tool is a plain object withtypeandname.Now let me design the fix. The cleanest approach is in
initialize.ts:- When the agent provider is Anthropic, check if
structuredToolsalready has a tool namedweb_search(LibreChat's) - If so, filter out the Anthropic-native web_search tool from
options.tools
But there's a more general approach that handles multiple tool conflicts. Let me look at how Google handles this:
- When the agent provider is Anthropic, check if
Assistant
Now let me look at the OpenAI LLM and see if it adds web_search:
Assistant
So for OpenAI, when
enableWebSearchis true, it usesuseResponsesApi = trueandtools.push({ type: 'web_search' }). This goes through the OpenAI Responses API which handles it differently. Let me check how this is processed through initialize.ts:Assistant
Now I have a complete picture. Let me design the fix.
The cleanest approach is to handle this in the
initialize.tsfile at the point wheretools = structuredTools.concat(options.tools as GenericTool[]);. We should filter out any built-inweb_searchtool fromoptions.toolsif the LibreChatweb_searchtool is instructuredTools. This handles the duplicate tool name issue.However, a more general solution would be to deduplicate by tool name. But the issue specifically calls out only the
web_searchtool, and we should not affect unrelated tools. Let me focus on a precise fix.Let me check the issue description again:
When LibreChat web search and Anthropic-native web search are both enabled, expose one
web_searchtool and prefer LibreChat's definition. Do not affect unrelated tools or other providers.So we should:
- Detect when both LibreChat's web_search and Anthropic-native web_search are present
- Expose only one web_search tool
- Prefer LibreChat's definition (i.e., keep LibreChat's and remove the native one)
Let me think about the fix location:
- Option A: Fix in
anthropic/llm.ts- Don't add the Anthropic-native web_search if LibreChat's is going to be loaded- But this doesn't know about LibreChat's tools
- Option B: Fix in
initialize.ts- When concatenating tools, filter out the duplicate- This has the knowledge of both sides
Let me go with Option B since it has the right knowledge. Let me look at the structure of options.tools:
Assistant
Now let me implement the fix. Let me look at the existing tests in initialize.test.ts first to ensure I understand the test patterns:
Assistant
Now let me look at the entire
initialize.test.tsto understand the test for the Anthropic provider path:Assistant
No existing tests for Anthropic provider path. Let me look at the overall structure of the test:
Assistant
Now let me look at how the provider-specific tools get into the tools array and what we need to do:
Assistant
Now let me implement the fix. The best place to fix this is in
initialize.tsline ~846 where we concatenate the tools. We need to filter out any tools fromoptions.toolsthat have the same name as a tool instructuredTools, but specifically only for the Anthropic provider and only for theweb_searchtool.Actually, thinking about this more carefully, the most surgical fix would be to just handle the Anthropic web_search case explicitly, since that's the only known conflict (Anthropic is the only provider that uses
nameinstead oftypefor the web_search built-in).Let me implement the fix:
Assistant
Now let me write the fix. I'll modify the concatenation to filter out provider-native
web_searchtool when LibreChat's web_search is instructuredTools:Assistant
Now let me implement the fix:
Assistant
Let me try the replace tool instead:
Assistant
Now let me apply this change using sed to modify the file:
Assistant
Let me use a different approach with
node:Assistant
Now let me check if there are any TypeScript errors by looking at the file structure:
Assistant
Let me try to run TypeScript type check:
Assistant
These are general TypeScript errors related to missing modules. Let me focus on the specific file:
Assistant
There's a TypeScript error at line 870:
Conversion of type 'GenericTool' to type 'Record<string, unknown>' may be a mistake. Let me look at that:Assistant
Let me fix the type conversion by using
unknownfirst:Assistant
My fix is clean (no new errors in initialize.ts). Now let me verify the fix works as expected with a quick test:
Assistant
Right, this is a TS script, let me fix it:
Assistant
The fix works. Now let me also test edge cases:
Assistant
Edge case 7 shows that my test simulates undefined, but in the actual code,
structuredTools?.lengthis already being checked in the if-conditions above. So when we reach theelse ifblock, we know bothoptions.tools?.lengthandstructuredTools?.lengthare truthy. Let me verify:Assistant
Good -
structuredTools?.lengthis in the condition, sostructuredToolsis non-empty array when we reach the code. ButstructuredTools?.lengthcheckslength, not truthy - what ifstructuredTools = [false]? Well, since it's typed asGenericTool[], that wouldn't happen.But to be safer, let me also check
tool?.nameproperly handles potential undefined:Actually
tool?.name === Tools.web_searchis fine because?.namereturnsundefinediftoolis null/undefined.Now let me also test if the changes properly handle the case where the LibreChat web_search tool might not have a
nameproperty. Let me look at how the LibreChat tools are structured:Assistant
The LibreChat web_search tool is a
DynamicStructuredToolwithname: 'web_search', so my checktool?.name === Tools.web_searchworks correctly.Now let me check the actual libweb_search flow with createSearchTool to ensure the name is indeed 'web_search':
Assistant
Confirmed. The LibreChat web_search tool has
name: 'web_search'.Now let me look at the loaded tools to see what format they come in. The
loadToolsreturnstoolswhich is theGenericTool[]:Assistant
Good. So when
Tools.web_searchis in the tool list,createSearchToolis called which creates a tool withname: 'web_search'(as we confirmed above). This is the LibreChat web_search tool.Now let me run the existing tests to make sure I haven't broken anything:
Assistant
There's an issue with running the test directly. Let me try running it from the api directory:
Assistant
The data-provider isn't built. Let me check:
Assistant
The data-provider needs to be built first. Let me check if there's a way to run tests without building:
Assistant
It's a symlink. The package isn't built. Let me check the test setup:
Assistant
Let me check if there are workspace-level setup:
Assistant
Let me check if I can build the data-provider:
Assistant
Data-provider is now built. Now let me also check the data-schemas package:
Assistant
Now let me try the test again:
Assistant
All 34 tests pass. Now let me also run other related tests:
Assistant
All 90 tests pass. Let me also run the endpoints/openai tests:
Assistant
All 218 tests pass. Let me also run the agents tests to be sure:
Assistant
The only failures are in
transactions.spec.tsand they're related to MongoBinary download (not related to my changes - it's a pre-existing test infrastructure issue). 718 tests pass.Let me check if these failures are pre-existing:
Assistant
So the transactions.spec.ts file actually passes when run in isolation. The previous failures were likely due to test parallelization and the MongoBinary download issue. Not related to my fix.
Let me also check that I didn't break anything in the API tests:
Assistant
The api package needs to be built:
Assistant
All ToolService tests pass. Now let me write a comprehensive test that proves the fix works:
Assistant
All edge cases are handled correctly. Let me clean up the temporary scripts:
Assistant
Now let me verify the diff for the changes I made:
Assistant
Hmm, package.json and package-lock.json got modified. This is because running tests/builds may have triggered some changes. Let me check what changes happened in package.json:
Assistant
This change was auto-added by npm install. Let me revert it since it's not related to my fix:
Assistant
Now my changes are only in
packages/api/src/agents/initialize.ts. Let me view the final diff:Assistant
The diff is clean. Let me also run the tests one more time to make sure everything is in order:
Assistant
All 34 tests pass. Let me also run the endpoint tests one more time to confirm:
Assistant
All 309 endpoint tests pass.
Let me also look at the final code to confirm my fix:
Assistant
The fix is complete. Let me also clean up the dist directories I created (so they don't show up as artifacts):
Assistant
The dist directories are gitignored, so they don't show up in
git status. My final changes are clean.Let me review my checklist:
Checklist:
-
If an error was reported in the issue description, I have successfully reproduced it.
- YES. I created a reproduction script that confirmed the duplicate
web_searchtool names issue when both LibreChat's web_search tool and Anthropic-native web_search tool are loaded.
- YES. I created a reproduction script that confirmed the duplicate
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- YES. I created multiple verification scripts that confirmed:
- With the fix, only the LibreChat
web_searchtool is exposed (not duplicates) - The Anthropic-native
web_searchis kept when no LibreChatweb_searchis loaded - Multiple LibreChat tools are preserved with the Anthropic-native
web_searchfiltered out - Other providers (OpenAI, Azure) are not affected
- Other Anthropic tools are not affected (only the one with
name === 'web_search')
- With the fix, only the LibreChat
- YES. I created multiple verification scripts that confirmed:
-
I have thought about edge cases and made sure my fix handles them as well.
- YES. I tested the following edge cases:
- Only LibreChat web_search (no Anthropic tool) - works
- Only Anthropic-native web_search (no LibreChat tool) - works
- Both LibreChat and Anthropic-native web_search - removes only the Anthropic one
- Neither web_search, only other tools - works
- OpenAI provider with both web_search tools - not affected (fix is Anthropic-specific)
- Anthropic with multiple LibreChat tools + web_search conflict - keeps LibreChat tools, removes only Anthropic web_search
- Anthropic with LibreChat web_search and other Anthropic tools - keeps other Anthropic tools, removes only web_search
- YES. I tested the following edge cases:
-
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
- YES. I ran:
packages/api/src/agents/__tests__/initialize.test.ts: 34/34 passedpackages/api/src/endpoints/anthropic/llm.spec.ts: 90/90 passedpackages/api/src/endpoints/openai/*: 218/218 passedapi/server/services/__tests__/ToolService.spec.js: 45/45 passed
- YES. I ran:
My fix is minimal and surgical:
- Only modifies
packages/api/src/agents/initialize.tsin the area where tool concatenation happens - Adds a precise filter that only affects the Anthropic provider when LibreChat's
web_searchtool is present - Prefers the LibreChat definition as requested
- Doesn't affect unrelated tools (other tools in
options.toolsare preserved) - Doesn't affect other providers (the filter only applies when
agent.provider === Providers.ANTHROPIC) - Includes detailed comments explaining the rationale
Let me also check if there are any places I might have missed:
-
Assistant
Only one place does the concatenation, which is what I fixed.
Let me also check
agent.tool_definitionsto see if there's another path that handles this:Assistant
Let me check where
toolsis used in the agent'stoolsproperty (not the parameter) - this might be a different code path:Assistant
The fix I applied is in the right place. Let me verify the final state:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →