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
Now let me explore the directory structure relevant to the issue.
Assistant
Now let me look at the test file to understand expected behavior:
Assistant
Let me look at how the
toolsarray is used and merged with LibreChat's tools.Assistant
Let me look at where the tools are constructed to see how the LibreChat web_search is added:
Assistant
Now let me look at the test file for the specific web_search test:
Assistant
Now let me look at how the tools are constructed, specifically when LibreChat's web_search would be added. Let me look at the
Tools.search.jsfile:Assistant
Now let me look at how tools are constructed and where the LibreChat web_search gets added to the model:
Assistant
Now let me look at the loadWebSearchAuth and search.ts to understand how the web_search tool is structured:
Assistant
Now let me look at the
WebSearchToolNameconstant:Assistant
So both LibreChat's web_search tool and Anthropic's web_search tool would have the same name
web_searchif both are enabled, which causes the duplicate error.Now let me look at where the tools are being assembled and passed to the Anthropic API. Let me look at how Anthropic uses these tools:
Assistant
Now let me see how the tools are merged with the regular tools. Search for where
toolsis concatenated with the user's tools:Assistant
Now let me look at how the tool is passed to the API request. Let me look at the existing code that converts tools to the Anthropic format:
Assistant
Now let me look at how the tools are being passed to the API. Let me look at the run logic of the agents client:
Assistant
Now let me look at where the tools are concatenated and passed to the LLM. Let me look at the createLLM and how
toolsis used:Assistant
Now let me look at how the langchain anthropic handles web_search tools. Let me look at how they merge:
Assistant
Now let me look at the langchain anthropic chat_models to see how it merges duplicate tools:
Assistant
Now let me look at the chat_models source to see how the web_search tool is handled:
Assistant
So both tools end up in the same
toolsarray. The issue is that:- LibreChat's web_search tool has
name: 'web_search' - Anthropic's web_search tool has
name: 'web_search'
When both are bound, Anthropic rejects this duplicate.
Now let me look at where the merge happens:
- LibreChat's web_search tool has
Assistant
Now let me look at the anthropic LLM in the agents package:
Assistant
Now let me look at how the tools are passed - look at the Graph.ts:
Assistant
So the tools are passed via
bindToolsto the LLM. Now let me see where the LLM is invoked and how the request is constructed. Let me look at how the actual request is made to see where the error originates. First, let me look at the Google llm.ts to compare similar logic:Assistant
Now let me check where tools would actually be merged with the LibreChat web_search tool. Let me look at how the tools from
getLLMConfigare merged with the user's tools fromhandleTools.js:Assistant
Now let me look at
graphToolsand where they're added:Assistant
Now let me look at how the LibreChat agent gets tools. Let me look at the agent endpoint:
Assistant
Let me look at the loadTools function:
Assistant
Now let me find where the tools are bound to the LLM in the agnent flow to understand how the duplicates are created:
Assistant
Now I understand the issue. When both
web_search(LibreChat) andweb_search(Anthropic) are enabled, the agent tool loader creates a LibreChat web_search tool with nameweb_search, and then thegetLLMConfigfor Anthropic also adds a tool with nameweb_search. The result is two tools with the same name in the array, which Anthropic rejects.Let me look at where the tools are being concatenated:
Assistant
Now let me look at where the tool from the anthropic config is added to the agent's tools. Let me trace the flow:
Assistant
Now let me find where the tools from getOpenAIConfig are used to bind/tie to the LLM. Looking at the issue: when both are enabled, the LibreChat web_search tool gets created and sent to the LLM alongside the Anthropic web_search tool. The fix should be in the
getLLMConfigfor Anthropic where the tool is added, or in the place where the tools are merged.Let me look at the test files to understand how the duplicate issue is currently being tested:
Assistant
Let me look at how the LibreChat web_search tool gets added - looking at the agents endpoint to see if the agent already has the web_search listed in its tools:
Assistant
Now let me look at the OpenAI llm.ts to see how the tools are merged with the LLMConfig:
Assistant
Now let me look at the full transform.ts to understand the entire flow:
Assistant
Now let me trace where the tools are passed to the LLM. Let me look at the agent endpoint to see how the initialization happens:
Assistant
Let me look at the agent client to see how it uses the tools:
Assistant
Now let me look at the
initializeAgentfunction to understand the flow:Assistant
Now let me look for the initializeCustom function:
Assistant
Now let me look at where the llmConfig is passed to the LLM. Let me look at the agent's initialize.ts:
Assistant
Now let me look at how the tools are bound to the Anthropic LLM. Let me trace how
toolsget fromgetLLMConfigto the LLM:Assistant
Now let me look at the openai/llm.ts to see how the tools from getLLMConfig are handled:
Assistant
Now let me look at the agents/load.ts since that handles the loading flow:
Assistant
Now let me trace where the tools are concatenated with the LLM's tools. Let me look at the Anthropic's bindTools:
Assistant
Let me look at how the tools are bound. Let me look at where the tools and the llmConfig are merged:
Assistant
So the
bindToolsis not called in the anthropic LLM directly. Let me look at theformatStructuredToolToAnthropicto see how it handles the tools:Assistant
Now I understand the issue. The
getLLMConfigfrom Anthropic returns atoolsarray containing the Anthropic web_search tool definition. Then thistoolsarray is merged with the LibreChatweb_searchtool (which is a LangChain tool withname: 'web_search'). When both are submitted, the resulting array has two tools with nameweb_search, which Anthropic rejects.Let me look at how the tools and llmConfig are merged in
getOpenAIConfig. Now let me trace where the agent endpoint loads the tools and binds them:Assistant
Now let me look at where the agent's tools and the LLM's tools are merged. Let me look at where the
toolsarray fromgetLLMConfigis passed to the graph:Assistant
Let me now look at the full chain - the issue is that when both LibreChat web_search and Anthropic web_search are enabled, the resulting tools list has two tools with the same name
web_search. Let me look at how the LLM is built with the tools:Assistant
Now let me look at where the
toolsare passed to the LLM. The flow is:agent.toolscontainsweb_searchloadToolscreates the LibreChatweb_searchtoolgetLLMConfigadds the Anthropic-nativeweb_searchtool- These are both bound to the LLM
Let me look at the Graph where the actual binding happens:
Assistant
Now let me look at AgentContext to understand how the
toolsis structured:Assistant
Now let me look at how the agent's tools are being passed. Looking at the flow:
loadToolscreates LibreChat web_search toolgetLLMConfigreturns the Anthropic web_search tool intoolsarray- Both are merged in the LLM somehow
Let me look at how the tools are passed - find where the
toolsfromgetLLMConfigis used:Assistant
Excellent! Now I see how the tools are merged:
} else if ( (agent.provider === Providers.OPENAI || agent.provider === Providers.AZURE || agent.provider === Providers.ANTHROPIC) && options.tools?.length && structuredTools?.length ) { tools = structuredTools.concat(options.tools as GenericTool[]); }So when the provider is Anthropic, the LibreChat tools (structuredTools) and the Anthropic provider tools (options.tools) are concatenated. This creates the duplicate issue.
When both LibreChat web_search and Anthropic web_search are enabled, both end up with the same name
web_search. The fix should be to deduplicate by name - prefer the LibreChat definition (which is the more feature-rich one).Let me look at all the related code more carefully:
Assistant
Now let me look at the existing tests for the initialize to understand the tool merging behavior:
Assistant
Let me look at the actual initializeAgent code path to understand the full flow:
Assistant
Let me look at the tests that cover the web_search tool deduplication:
Assistant
Now let me see where the Anthropic web_search tool gets concatenated with the LibreChat tools. Let me look at the OpenAI path:
Assistant
Now I understand the issue:
- When the agent is an Anthropic agent with web_search enabled,
options.toolscontains the Anthropic native web_search tool. structuredToolscontains the LibreChat web_search tool withname: 'web_search'.- These are concatenated:
tools = structuredTools.concat(options.tools as GenericTool[]) - Now there are two tools with the same name
web_search.
The fix should be in the Anthropic provider - when both LibreChat web_search and Anthropic web_search are enabled, we should prefer LibreChat's definition and not include the Anthropic web_search tool in
options.tools.But wait - the issue might be that the
web_searchfromgetLLMConfigshould not be added if there's already a LibreChat web_search tool. Let me look at the actual flow more carefully.Let me look at how it's being passed. The provider config returns both
llmConfigandtools:- When the agent is an Anthropic agent with web_search enabled,
Assistant
Now let me look for the
getOptionsfunction for anthropic:Assistant
Now let me look at the configureReasoning function to see how the web_search is handled:
Assistant
Now I have a clear picture. Let me devise the fix:
The issue is in
packages/api/src/agents/initialize.tsat lines 828-846. When the provider is Anthropic, the LibreChat tools (structuredTools) and the Anthropic provider tools (options.tools) are concatenated. When both LibreChat web_search and Anthropic web_search are enabled, the resulting tools array has two tools with the same nameweb_search.The fix should:
- Detect when both a LibreChat web_search tool and an Anthropic web_search tool are present
- Remove the Anthropic web_search tool (prefer LibreChat's definition)
- Apply only to the Anthropic case (don't affect other providers or other tools)
Let me look at the test files to understand the existing test structure:
Assistant
Now let me look at the tests for the tool merging:
Assistant
Let me look at the OpenAI initialization:
Assistant
Now I have a clear understanding. Let me write a reproduce script first to verify the issue:
Assistant
Let me look at the build output:
Assistant
Let me run the existing test first to make sure things work:
Assistant
Let me try building data-schemas:
Assistant
Now let me run the existing test for initialize.test.ts:
Assistant
Good, tests pass. Now let me think about the fix. Let me look at the actual code more carefully:
Assistant
Now let me think about the best approach. There are several ways to fix this:
- In
initialize.ts: When merging tools for Anthropic, deduplicate by name, preferring the LibreChat tool. - In
anthropic/llm.ts: Don't add the Anthropic web_search tool if there's already a web_search tool in the LibreChat agent tools. - In
openai/config.ts: When processing tools, deduplicate by name.
Looking at the issue, the cleanest fix is at the merge point in
initialize.tsbecause:- That's where the actual conflict happens (merging two tool lists)
- It allows both LibreChat and Anthropic-native web search to coexist
- It only affects Anthropic (the issue says "Do not affect unrelated tools or other providers")
- It prefers LibreChat's definition (as the issue says)
Let me also look at what the
structuredToolsandoptions.toolslook like:- In
Assistant
Good, the LibreChat web_search tool has name
web_search. Now let me look at theGenericTooltype:Assistant
Now let me see what
options.toolscontains (the Anthropic tool):Assistant
So
options.toolscontains[{ type: 'web_search_20250305', name: 'web_search' }]for Anthropic.Now let me understand the best approach. Looking at the test for the GOOGLE_TOOL_CONFLICT, I see the structured tools and the provider tools are concatenated for Anthropic. The fix should be in this concatenation step to deduplicate by name when there's an overlap.
Let me look at how to write the fix:
Assistant
Now I have a good understanding. Let me look at the actual data flow once more. The
options.toolsis what's returned frominitializeAnthropic(which callsgetLLMConfig). This contains the Anthropic-native web_search tool. ThestructuredToolsare the LibreChat tools (loaded byloadTools).Now I need to think about where to put the fix. Looking at the issue more carefully:
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:
- Detect when there's both a LibreChat web_search tool and an Anthropic web_search tool
- Expose only one
web_searchtool (the LibreChat one) - Not affect other tools (don't deduplicate everything, just web_search)
- Not affect other providers (only Anthropic has this issue)
Looking at the code, the best place to put this fix is in the merge step in
initialize.ts- just for Anthropic, only for web_search.Let me look at the structure of the tools more carefully:
Assistant
Now I have a complete understanding. The fix should be in the
initialize.tswhere the tools are merged for Anthropic. Let me make the fix:Assistant
Now I need to think about how to detect and fix the duplicate. Let me look at the Tools constant:
Assistant
Good. So
Tools.web_search = 'web_search'.Now let me design the fix. Looking at the code:
} else if ( (agent.provider === Providers.OPENAI || agent.provider === Providers.AZURE || agent.provider === Providers.ANTHROPIC) && options.tools?.length && structuredTools?.length ) { tools = structuredTools.concat(options.tools as GenericTool[]); }For Anthropic specifically, when LibreChat has
web_searchtool and Anthropic also hasweb_searchtool, we should drop the Anthropic one.Let me think about the cleanest implementation. There are a couple of approaches:
Option A: Fix in
initialize.ts- deduplicate during merge. Option B: Fix inanthropic/llm.ts- check if LibreChat has web_search, don't add Anthropic's. Option C: Fix inopenai/config.ts- similar to existing google tools filter, but for anthropic.I think Option A is the cleanest because:
- It's localized to the agent initialization
- It directly addresses the merge point
- It only affects Anthropic specifically (per issue)
- It preserves the existing behavior of
Assistant
Now let me write the fix. I think the best approach is to:
- Add a helper function that checks for name overlap
- For Anthropic specifically, when both LibreChat and Anthropic have web_search, drop the Anthropic one
Assistant
Let me check how the structure of
options.toolslooks for the Anthropic web_search case:Assistant
Now let me make the fix. I'll modify the
initialize.tsto handle the duplicate web_search case for Anthropic:Assistant
Now let me implement the fix. The approach is to filter
options.toolsto remove any tools that have the same name as a LibreChat tool, but ONLY for Anthropic. Wait, let me re-read the issue: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 issue is specifically about
web_searchtool. So I need to make sure I only deduplicate when there's a name conflict (like web_search), not all tools.Looking at the fix more carefully, the simplest way is to filter
options.toolsto remove any tool that has the same name as a LibreChat tool. This:- Only affects the merge point (when both lists are present)
- Only affects Anthropic (where the conflict happens)
- Only deduplicates on name conflict (preserving all unique tools)
- Prefers LibreChat's definition (since LibreChat tools come first in the concat)
Let me now make the fix:
Assistant
Now let me look at the code I'm modifying:
Assistant
Now let me implement the fix:
Assistant
The terminal seems to be in a weird state. Let me try a different approach:
Assistant
Let me try the edit again with a simpler form:
Assistant
Now let me look at the indentation - it seems there's a difference. The text is using 2-space indentation. Let me make the edit now:
Assistant
Now I need to add the
dedupeProviderToolshelper function. Let me find a good place to add it:Assistant
Now let me add the helper function. I'll add it right after
appendAdditionalInstructions:Assistant
Let me try the edit again with a simpler approach:
Assistant
Now let me verify the change:
Assistant
Now let me verify the change in the merge block:
Assistant
Now let me run the existing tests to make sure my changes don't break anything:
Assistant
All tests pass. Now let me also run the anthropic tests:
Assistant
Now let me write a quick test to verify the fix works:
Assistant
All tests pass. The fix:
- Test 1: When both LibreChat web_search and Anthropic web_search are enabled, the Anthropic one is dropped, no duplicates.
- Test 2: When only Anthropic web_search is enabled, it's preserved.
- Test 3: For OpenAI, no dedup is performed (untouched).
- Test 4: For Anthropic without LibreChat web_search, other tools are preserved.
Now let me think about edge cases:
- What if the structuredTools contains the web_search tool but with a slightly different format (e.g., a class instance)?
- What if multiple tools share a name?
- What about the
getToolNamefunction - does it handle edge cases like null, undefined, etc.?
Let me look at the actual flow once more:
Assistant
Now let me also verify the test patterns used. Let me look at the test for similar tool merging:
Assistant
Let me also look at the existing test for anthropic to make sure I understand the setup:
Assistant
There are no existing tests for the Anthropic provider in initialize.test.ts. Let me run all the existing tests to make sure nothing is broken:
Assistant
There are some test failures, but they look like they're related to integration tests (cache, mcp) and not my changes. Let me check if the test failures are pre-existing:
Assistant
The failures look like they're environment-related (Database connection, etc). Let me run only the tests that are related to my changes:
Assistant
All my tests pass. Now let me check what tests were failing and confirm they aren't related to my changes:
Assistant
The same 17 test suites fail with or without my changes (the difference is 1 test passing/failing - might be related to my change). Let me check what test was different:
Assistant
These changes to package.json and package-lock.json are from running the build commands. Let me revert them since they weren't intentionally modified by my fix:
Assistant
Now let me re-run the tests to confirm everything is still good:
Assistant
All tests pass. Let me also make sure the OpenAI tests pass since my change affects the shared code path:
Assistant
All tests pass. Now let me also write a temporary test to verify the fix works:
Assistant
Let me write the test in a more standard location:
Assistant
All my tests pass. Let me clean up the temporary test file:
Assistant
Wait, the instructions say I should not modify or add new tests to the repository. The instructions specifically say:
UNDER NO CIRCUMSTANCES should you modify existing tests or add new tests to the repository. This will lead to an error in the system that evaluates your performance. Instead, you can just create a temporary script, use it to test changes and remove it before submitting.
So removing the test is correct. Now let me also run the broader test suite to make sure my change didn't break anything:
Assistant
All tests pass. Let me also verify the change is still in place:
Assistant
The change is in place. Let me think about potential edge cases more carefully:
-
The
options.toolsonly contains Anthropic tools - The dedup checks for LibreChatweb_searchfirst, so if LibreChat doesn't have it, no changes are made. -
What if
options.toolsis an array of OpenAI/Azure tools - For non-Anthropic providers, we return the original array unchanged. -
What if
structuredToolsis empty - The dedup checks this first, but the functiondedupeProviderToolsis only called ifstructuredTools?.lengthis truthy (in the parent condition), so this is safe. -
What if the LibreChat tool has a different name format - The
getToolNamefunction handles both directnamefield and any object with anameproperty. The StructuredTool interface has anamefield. -
What if multiple LibreChat tools have the same name - That's not our concern, this only handles the collision with provider tools.
-
What about the tests for ? - All 34 existing tests pass.
-
Assistant
The
summarization.e2e.test.tshastools: [new Calculator()]which is interesting - let me look at it to make sure my fix doesn't break anything:Assistant
This test uses
Calculatorwhich is a LangChain tool instance. It has a.nameproperty. ThegetToolNamefunction I wrote handles this case.Assistant
The function works correctly with class instances.
Let me also verify my fix doesn't break the existing tests with mocks:
Assistant
The summarization tests are skipped (they're e2e). Let me now look at the issue more carefully. The instructions specifically say:
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.Let me re-read this:
- "expose one
web_searchtool" - ✓ My fix only exposes one web_search tool - "prefer LibreChat's definition" - ✓ My fix prefers LibreChat's definition
- "Do not affect unrelated tools" - ✓ My fix only deduplicates
web_search, not other tools - "Do not affect other providers" - ✓ My fix only triggers for Anthropic
The fix is correct. Now let me also verify that I'm not breaking any edge cases. Let me look at the
Toolsenum:- "expose one
Assistant
Tools.web_searchis the string 'web_search', so this matches both the LibreChat tool name and the Anthropic tool name.Now let me also think about what happens when the LibreChat tool's name field is something else (e.g., 'web_search_v2'). In that case, the LibreChat tool wouldn't be named
web_searchand our fix would not trigger. The user would have to update their tool name for it to work with the deduplication. But that's an edge case not directly mentioned in the issue.Let me also verify that the fix handles the case where
options.toolsdoesn't contain a web_search tool but is just other Anthropic tools:Assistant
All edge cases pass. The fix is working correctly. Let me also check the changes I haven't reverted yet:
Assistant
Only the intended file is modified. Let me also do a final review of the change:
Assistant
The change is clean and minimal. Now let me also clean up the temp files:
Assistant
Now let me also verify my fix one more time by looking at the final state of the file:
Assistant
The fix is in place. Now let me re-examine the whole fix once more to make sure it's correct.
Let me check the checklist:
-
If an error was reported in the issue description, I have successfully reproduced it.
- I confirmed the issue by reading the code and understanding the flow. The duplicate
web_searchname collision between LibreChat's tool (namedweb_search) and Anthropic's native tool (namedweb_searchwith typeweb_search_20250305) is real and would cause Anthropic to reject the request. - I created a test script to verify the fix works.
- I confirmed the issue by reading the code and understanding the flow. The duplicate
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- Yes, my fix deduplicates the tools so only the LibreChat web_search tool is exposed.
-
Assistant
All tests pass. Let me also look at the Anthropic spec file to make sure it has tests:
Assistant
The Anthropic tests are comprehensive (90 tests, all passing). My fix is minimal, focused, and well-documented.
Let me look at my final solution once more:
Assistant
Let me also re-read the issue one more time to confirm my understanding:
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.
My fix:
- ✅ When LibreChat web search and Anthropic-native web search are both enabled, exposes one
web_searchtool - ✅ Prefers LibreChat's definition (LibreChat tool comes first in the concat, or in this case, only the LibreChat one is kept)
- ✅ Does not affect unrelated tools (only deduplicates
web_search) - ✅ Does not affect other providers (only triggers for Anthropic)
- ✅ No new public interfaces (helper functions are private to the file)
The fix is complete and correct. Let me also clean up the open file:
- ✅ When LibreChat web search and Anthropic-native web search are both enabled, exposes one
Assistant
Good, no temp files left. Let me also look at the final state of the file:
Assistant
The fix is clean, minimal (48 line additions, 1 line removal), and focused on the specific issue.
Now let me check the checklist one final time:
Checklist Review
-
If an error was reported in the issue description, I have successfully reproduced it.
- The issue is about Anthropic rejecting requests when both LibreChat
web_searchand Anthropic-nativeweb_search_20250305are both enabled. I traced the code path:agent.toolscontainsweb_search→loadToolscreates the LibreChat tool
- The issue is about Anthropic rejecting requests when both LibreChat
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.