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
Let me start by exploring the repository structure to understand the codebase and find relevant files related to the issue.
The issue is about:
- When LibreChat web search and Anthropic-native web search are both enabled, it exposes one
web_searchtool and prefers LibreChat's definition. - Don't affect unrelated tools or other providers.
Let me explore the repository structure first.
- When LibreChat web search and Anthropic-native web search are both enabled, it exposes one
Assistant
Let me search for relevant files related to web_search, anthropic, and tool handling
Assistant
Let me look at the key files involved. The issue is about duplicate
web_searchtool names when both LibreChat and Anthropic web search are enabled. Let me check the coreAssistant
Let me look at the handleTools.js file first
Assistant
Now let me look at the Anthropic web search tool and how tools are loaded for Anthropic specifically
Assistant
Let me look at the tools schema and handlers for Anthropic
Assistant
Let me look at the agent load and the anthropic-specific handling
Assistant
Let me look at the agents handlers and the tools.spec.ts for anthrop
Assistant
Let me look at the tools.ts in
Assistant
Let me look at the agents handlers spec and the handlers.ts for the web_search deduplication
Assistant
Let me look at the handlers.ts file
Assistant
Let me look at agents/added.ts which seems relevant
Assistant
Now let me look at the specific Anthropic llm.ts and where the native web_search is added
Assistant
Now I understand the flow. Let me see how the Anthropic LLM config tools are merged with loaded tools. Let me look at the llm.spec.ts for Anthropic:
Assistant
Let me find where
getLLMConfigis called to see how its tool results are mergedAssistant
Let me look at the Anthropic initialize.ts to understand how tools are merged
Assistant
Let me search for where
initializeAnthropicis called to see how tools getAssistant
Let me look at
providers.ts:Assistant
Now let me find where the
toolsfromgetLLMConfigare used/m<|DSML|tool_calls> <|DSML|invoke name="bash_terminal"> <|DSML|parameter name="command" string="true">cd /LibreChat && grep -rn ".tools\b" --include=".ts" --include=".js" packages/api/src/endpoints/anthropic/ | grep -v node_modules | grep -v ".spec."</|DSML|parameter> </|DSML|invoke> </|DSML|tool_calls>
Assistant
Let me find where the Anthropic tools are used/
Assistant
The grep might not match anything. Let me search more broadly.
Assistant
Let me look at the types to see what
InitializeResultBasereturnsAssistant
Let me look at the
LLMConfigResulttype in openai.ts:Assistant
Let me see the full
LLMConfigResulttype:Assistant
Now let me find where the
toolsfromInitializeResultBaseare combined with loaded agent tools:Assistant
Let me look at the
initialize.tsin agents to understand the tool merging:Assistant
Let me look at the part of
initialize.tsaround lines 800-900 where tools are merged:Assistant
Let me look at the full
initializeAgentfunction:Assistant
Let me look at the function from line 355
Assistant
Let me search for where
getOptionsandoptions.toolscome from in initialize.ts:Assistant
Let me look at lines 724-750
Assistant
Now I have a clear understanding of the issue. Let me look at the key area where the fix needs to be applied. The issue is in
initialize.tswherestructuredTools(LibreChat tools) andoptions.tools(provider-native tools fromgetLLMConfig) are concatenated. For Anthropic, when both LibreChatweb_searchand Anthropic-nativeweb_searchare enabled, duplicates occur.Let me look more at the structuredTools loading to understand the full
Assistant
Let me see where
structuredToolsis defined:Assistant
Let me look at line 675-685
Assistant
Now I have a clear understanding. Let me create a reproduction script and then implement the fix.
First, let me understand the existing test structure:
Assistant
Let me look at how tools are tested in initialize tests
Assistant
Let me look at the test around that line to understand the expected
Assistant
Now I understand the issue fully. Let me implement the fix. The problem is in
packages/api/src/agents/initialize.tsat lines 842-846 wherestructuredTools(LibreChat-loaded tools) are concatenated withoptions.tools(provider-native tools fromgetLLMConfig). When both LibreChatweb_searchand Anthropic-nativeweb_searchare enabled, duplicates occur.The fix: filter out provider-native tools that have names already present in LibreChat-loaded tools, so LibreChat's definition is
Assistant
Let me open the file and look at the area to fix:
Assistant
Now I can see the exact code to fix. The issue is on line 846 where
options.tools(provider-native tools like Anthropic'sweb_search) are concatenated withstructuredTools(LibreChat-loaded tools). When both haveweb_search, Anthropic rejects the duplicate.Let me implement the fix:
Assistant
Let me try with the
replacecommand insteadAssistant
The fix looks correct. Now let me verify by running the existing tests related to this area:
Assistant
Let me fix the jest command
Assistant
The test has a parse error. Let me check if my edit introduced a TypeScript error by looking at the file again
Assistant
The TypeScript errors are pre-existing module issues. Let me check the jest error more carefully
Assistant
The test error is pre-existing in the test file (babel parsing issue, not related to my change). Let me verify my change is syntactically correct
Assistant
Let me also check the OpenAI llm.ts to see if it also has the web_search tool in its options.tools:
Assistant
Let me see the OpenAI web_search tool definition:
Assistant
Let me check the BindToolsInput type to understand the shape:
Assistant
Let me check whether the OpenAI web_search tool has a name property:
Assistant
Let me verify the fix is complete by checking if there are other code paths where tools are merged
Assistant
Let me look at the openai service.ts to check for another tool merging path
Assistant
Let me also check if Google has a similar web_search tool issue:
Assistant
Let me also look at the ToolService.js in the API server to see if it has the same issue
Assistant
Let me look at the
loadAgentToolsfunction in ToolService.js to see if it does tool merging with provider tools:Assistant
Let me look at the
loadAgentToolsfunction:Assistant
Let me check the
loadToolDefinitionsWrapperfunction:Assistant
Let me look at
loadToolDefinitionsWrapperAssistant
Let me check if there's a separate code path for non-agent Anthropic chat that might have the same issue:
Assistant
Let me check the v1.js file to see if there's another tool merging path:
Assistant
Let me also check the openai config.ts to see how Anthropic's getLLMConfig is used there:
Assistant
Let me check how it's used:
Assistant
Let me see the context around line 103:
Assistant
Let me check where the
toolsvariable goes:Assistant
Let me look at the full config.ts to understand the flow better:
Assistant
Let me check who calls
getOpenAIConfig:Assistant
Let me verify the fix is correct and run the relevant tests. Let me first check if there are unit tests for the initializeAgent function that test tool merging:
Assistant
Let me try the full path:
Assistant
Let me verify this is a pre-existing test failure by reverting my change temporarily and running the test:
Assistant
Good - the test was already failing before my change. Let me restore my fix:
Assistant
Let me check if the package.json changes are pre-existing:
Assistant
Good. Let me verify my change with git
Assistant
Let me now run the tests for related files that might be affected:
Assistant
Let me try running some tests that might work:
Assistant
There seems to be a widespread babel parsing issue. Let me check if there's a babel config:
Assistant
The babel config seems fine. Let me check the exact line causing the parse error in the test:
Assistant
Let me see if there's a jest config that sets up babel correctly:
Assistant
Let me try running with the packages/api jest config
Assistant
Let me check if there are any tests that specifically test tool deduplication or the Anthropic web_search merge behavior:
Assistant
Let me search for where Anthropic tools might be assembled in the api/ directory:
Assistant
Let me look at the nativeTools usage in ToolService.js:
Assistant
Let me look at how
toolsfromgetOpenAIConfigis used in the non-agent Anthropic chat path:Assistant
Let me look at the full return of getOpenAIConfig:
Assistant
Let me check if there's a non-agent Anthropic path that might also duplicate web_search:
Assistant
Let me check the tools.js controller:
Assistant
My fix is in the right place. Let me do a final review of the change
Assistant
Let me create a quick test script to verify the deduplication logic works correctly
Assistant
Let me write a temp script file instead:
Assistant
Let me test edge cases too:
Assistant
Let me clean up temp files and verify TypeScript syntax:
Assistant
Let me also check if there's a relevant test for the API server ToolService:
Assistant
Let me verify my change one final
Assistant
My change is complete. Let me provide a summary:
Summary
Issue: When both LibreChat web search and Anthropic-native web search are both enabled, Anthropic rejects duplicate
web_searchtool names.Root Cause: In
packages/api/src/agents/initialize.ts, when initializing an agent for Anthropic (and OpenAI/Azure), the code concatenatesstructuredTools(LibreChat-loaded tools fromhandleTools.js) withoptions.tools(provider-native tools fromgetLLMConfig). When both sources contain aweb_searchtool, duplicates occur:- LibreChat's
web_searchcomes fromloadTools→handleTools.js→createSearchTool - Anthropic-native
web_searchcomes fromgetLLMConfiginendpoints/anthropic/llm.ts
Fix: Before concatenation, filter out provider-native tools whose names already exist in LibreChat-loaded tools. This ensures:
- LibreChat's
web_searchdefinition is preferred (it comes first in the concatenated array) - Anthropic-native
web_searchis excluded when it would duplicate - Unrelated tools with different names are unaffected
- Other providers are unaffected (the logic applies but there are no current name collisions)
The fix is in
packages/api/src/agents/initialize.ts, modifying the tool concatenation logic to add deduplication by tool name.Checklist
-
✅ Reproduced the error: I traced the code to understand the root cause - duplicate
web_searchtool names from two sources when both are enabled. -
✅ Confirmed the fix resolves the error: Verified the deduplication logic works correctly with test scripts covering edge cases (Anthropic native filtered, OpenAI no-name tools preserved, no collisions preserved).
-
✅ Considered edge cases:
- OpenAI tools without
nameproperty → not incorrectly filtered - Only Anthropic-native web_search enabled → kept
- Only LibreChat web_search enabled → kept
- Neither enabled → not affected
- Other providers (OpenAI, Azure) → logic applies but no current name collisions
- OpenAI tools without
-
✅ Run existing tests: The relevant test files (
initialize.test.ts,tools.spec.ts) have pre-existing babel parsing errors unrelated to my change. Confirmed by reverting my change and running tests - they fail identically.
- LibreChat's
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 →