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 and then find the relevant Anthropic provider code.
Assistant
Now let me look at the Anthropic LLM endpoint where web search tools are likely set up.
Assistant
Now I see the Anthropic LLM config. The
web_searchtool is added to thetoolsarray. Now I need to understand how this is used downstream and where the duplication could occur with LibreChat's web search. Let me look at the initialize.ts and how tools are used.Assistant
Now let me look at the spec file for the Anthropic LLM to understand the existing test patterns.
Assistant
Now let me understand how the Anthropic endpoint's
toolsare used. Let me look at the agents code that loads tools and how the Anthropic endpoint config feeds into the agent.Assistant
Now let me see how the agent's tools combine with the LLM config tools. Let me look at where the LLM config result is used to bind tools.
Assistant
Now let me understand where
options.toolscomes from. Let me look at the caller ofinitializeAgentand how the LLM config'stoolsfield gets passed.Assistant
Let me now find where the LLM config is created and the
toolsfield is bound to the LLM. Let me look at the run.ts and how the agent model is set up.Assistant
So the
agent.tools(the tool definitions) andclientOptions: llmConfigare passed. ThellmConfigis built fromagent.model_parameters. Let me look at how thetoolsfrom the LLM config (result.toolsreturned bygetLLMConfig) flow intoagent.model_parameters. Let me look at the@librechat/agentspackage where the LLM is constructed andbindToolshappens.Assistant
Let me look at how the agent tools are loaded and what
agent.toolsrefers to in the initialized agent. Let me check the InitializedAgent type and how the llmConfig tools are passed.Assistant
Now I understand the flow. The
getLLMConfig(for Anthropic) returns atoolsarray containing the Anthropic-nativeweb_searchtool. These tools get bound to the model. The agent also has LibreChat'sweb_searchtool registered via the tool definitions registry.Let me find where
options.tools(the LLM config's tools) is consumed and bound.Assistant
So
initializedAgent.toolsis a combination ofstructuredTools(LibreChat's tools like web_search) +options.tools(the LLM-config's tools like Anthropic's native web_search). For Anthropic, when both are present, they are concatenated:structuredTools.concat(options.tools). This is where the duplicateweb_searchtool name comes from.Let me look at how the LLM is built and tools bound in
@librechat/agentsto understand the final binding.Assistant
So
initializeModelcallsmodel.bindTools(tools)with the combined tools array. For Anthropic, both the LibreChatweb_searchtool (a structured tool with nameweb_search) and the Anthropic-nativeweb_searchtool ({ type: 'web_search_20250305', name: 'web_search' }) would be in the array, causing a duplicate name.Let me confirm where the
toolspassed toinitializeModelcome from.Assistant
So
getToolsForBindingreturns:- If
toolDefinitionsexist:getEventDrivenToolsForBinding()returns schema tools (from toolDefinitions) + graphTools + instanceTools. - Otherwise: filtered instance tools + graphTools.
The
instanceToolscome fromagent.toolswhich is thetoolsreturned frominitializeAgent— the combination of structuredTools (LibreChat tools) + options.tools (Anthropic native web_search).Wait, let me check
getEffectiveInstanceTools.- If
Assistant
So I now have a clear understanding. The
agentContext.toolsincludes both LibreChat'sweb_searchstructured tool and Anthropic's native{ type: 'web_search_20250305', name: 'web_search' }tool. When these are bound viabindTools, Anthropic rejects the duplicate name.Let me reproduce the issue to understand the error precisely. Let me look at the Anthropic bindTools implementation.
Assistant
Let me look at how
structuredToolsis built and whatbaseToolNames/agentToolNamesinclude.Assistant
Let me look at the
WebSearchToolDefinitionfrom@librechat/agents.Assistant
Let me look at how
options.tools(the LLM-config tools) is set up in the initialize tests.Assistant
Now let me look at how the existing tests check the concatenation logic for ANTHROPIC.
Assistant
Let me check the
Toolsenum and the constants used.Assistant
Let me look at the imports and the top portion of
initialize.ts.Assistant
Let me write a reproduction script to confirm the duplicate
web_searchissue. First, let me check the test setup.Assistant
Let me write a reproduction test to confirm the duplicate
web_searchissue at theinitialize.tsmerge point.Assistant
Now let me create a temporary reproduction test to confirm the duplicate
web_searchissue:Assistant
The module resolution for
librechat-data-providerfails. The existing tests must use a different module resolution. Let me check how the existing test imports work — maybe there's a moduleNameMapper or the package is built. Let me check the node_modules linkage.Assistant
The
distdirectory is empty forlibrechat-data-provider. Let me check if the existing tests run at all.Assistant
The existing tests also fail because the dependent packages need to be built. Let me build the packages.
Assistant
Let me build the dependent packages so tests can run.
Assistant
Now let me try running the existing test again.
Assistant
Now the tests run. Let me run my reproduction test.
Assistant
I've reproduced the issue. The result tools have two
web_searchentries: the LibreChat structured tool and the Anthropic-native{ type: 'web_search_20250305', name: 'web_search' }.Now let me implement the fix in
initialize.ts.Assistant
Let me check what the loaded LibreChat
web_searchstructured tool actually looks like (itsnameproperty).Assistant
Let me look at how the web_search structured tool is actually created/loaded so I can confirm it has a
nameproperty equal toweb_search. Let me look at the agents tools loader inapi/server/controllers/agents/openai.jsor theloadAgentToolsfunction.Assistant
Now I understand the structure. The LibreChat
web_searchtool has.name === 'web_search'. Let me check theGenericTooltype.Assistant
Let me check the
InitializeResultBasetype for thetoolsfield.Assistant
Now I'll implement the fix. Let me add a helper function and modify the merge block in
initialize.ts.Assistant
Now let me add the helper function after
appendAdditionalInstructions:Assistant
Now let me add the helper function after
appendAdditionalInstructions:Assistant
Let me use the proper
editcommand instead. First, let me view the exact lines.Assistant
There's a missing blank line between the helper and the next comment. Let me fix that:
Assistant
Now let me modify the merge block to use the helper. Let me view it:
Assistant
Let me fix the double blank line:
Assistant
Now let me run my reproduction test to confirm the fix:
Assistant
The fix works — now only the LibreChat
web_searchtool remains. Let me extend my reproduction test to cover edge cases: (1) only Anthropic-native web_search (no LibreChat) keeps native, (2) only LibreChat web search (no native) keeps LibreChat's, (3) unrelated provider tools unaffected, (4) other providers unaffected.Assistant
All edge cases pass. Now let me run the existing test suite for
initialize.tsto make sure I didn't break anything.Assistant
All 34 tests pass. Now let me run the broader agents test suite to ensure nothing else broke.
Assistant
All agents tests pass (747 passed, 4 skipped). Now let me run the Anthropic-specific tests.
Assistant
Now let me remove my temporary reproduction test file before submitting.
Assistant
Let me also check whether there's a legacy/non-agents path that could hit the same duplicate issue.
Assistant
Let me check the
run.tsgetOptions usage around line 365 to ensure there's no other merge point.Assistant
Let me verify TypeScript compiles correctly. Let me check for a typecheck setup.
Assistant
These TypeScript errors are pre-existing and unrelated to my change (they're about
EModelEndpoint.custom/agentsindexing). Let me confirm none are in my edited file:Assistant
No TypeScript errors in
initialize.ts. Let me verify these pre-existing errors exist on the original code (before my change) by stashing:Assistant
I notice
package.jsonandpackage-lock.jsonshow as modified, which I didn't intend. Let me check what changed there:Assistant
The
package.jsonandpackage-lock.jsonchanges are unrelated to my fix (they'reallowScriptsfrom the environment setup). Let me revert them so I only submit my actual fix:Assistant
Now only
initialize.tsis modified. Let me re-run the tsc check to confirm my change introduces no new errors (same count as baseline):Assistant
Same count (33 lines) as the baseline — my change introduces no new TypeScript errors. Now let me view the final diff of my change:
Assistant
There's a missing blank line between my helper function and the next comment. Let me fix that:
Assistant
Now let me run the linter (eslint) on the file to ensure formatting compliance.
Assistant
ESLint passes. Now let me run the full agents test suite once more to confirm everything still passes after the formatting fix:
Assistant
All 125 tests pass. Let me also run the broader test suite for the agents folder one final time to ensure no regressions:
Assistant
Let me investigate the event-driven mode case, where LibreChat's
web_searchmight be intoolDefinitionsrather thanstructuredTools. Let me check whatloadAgentToolsreturns.Assistant
Let me check
buildToolClassificationto understand whethertoolDefinitionscould containweb_searchwhilestructuredTools(instance tools) doesn't (deferred loading scenario).Assistant
My analysis confirms the fix is sufficient for both regular and event-driven modes, since LibreChat's
web_searchis always an instance tool instructuredTools(never solely intoolDefinitions, which only holds MCP/PTC/toolSearch defs). Let me run the tools tests to be safe:Assistant
All tests pass. Let me do a final review of the complete change and verify the file is in good shape:
Assistant
Let me verify the
Providers.ANTHROPICvalue matches whatagent.providerwould be:Assistant
Providers.ANTHROPIC === 'anthropic', which matchesagent.provider.Let me run the complete relevant test suites one final time to confirm everything passes:
Assistant
All 911 tests pass. Let me provide the final checklist review.
Summary
I resolved the issue where Anthropic rejects duplicate
web_searchtool names when both LibreChat web search and Anthropic-native web search are enabled.Root cause
In
packages/api/src/agents/initialize.ts, when merging tools for the Anthropic provider, the LibreChatweb_searchstructured tool (loaded from the agent'sagent.tools) was concatenated with the Anthropic-nativeweb_searchserver tool ({ type: 'web_search_20250305', name: 'web_search' }, surfaced via the provider LLM config'soptions.tools). Both share the nameweb_search, so binding them produces a duplicate name that the Anthropic API rejects.Fix
I added a
resolveAnthropicWebSearchConflicthelper that, for the Anthropic provider only, drops the Anthropic-nativeweb_searchserver tool from the provider-supplied LLM-config tools when LibreChat'sweb_searchstructured tool is already loaded — preferring LibreChat's definition. The filteredproviderToolsis then used in place ofoptions.toolsthroughout the merge logic. For non-Anthropic providers the helper is a pass-through, so unrelated tools and other providers are unaffected.Checklist
- ✅ Reproduced the error: I wrote a temporary test that constructed an Anthropic agent with both web searches enabled and confirmed
result.toolscontained two tools namedweb_search(["web_search","web_search"]). - ✅ Confirmed the fix resolves the error: The same reproduction then showed only one
web_search(LibreChat's), with the native tool dropped. I also verified edge cases: only-native (keeps native), only-LibreChat (keeps LibreChat), unrelated provider tools unaffected, and other providers (OpenAI) unchanged (no regression). - ✅ Edge cases handled: Helper guards on provider === ANTHROPIC, requires both
providerToolsandstructuredToolsnon-empty, and requires a LibreChatweb_searchinstance present. Detection of the native tool uses thetypediscriminator so LibreChat's structured tool is never accidentally removed. The GOOGLE_TOOL_CONFLICT path still works sinceproviderToolsequalsoptions.toolsfor non-Anthropic. - ✅ Existing tests pass: All agents (742), anthropic (91), and tools (78) test suites pass; ESLint clean; no new TypeScript errors introduced (baseline count unchanged). I removed my temporary reproduction test before finishing.
The change is limited to a single file (
packages/api/src/agents/initialize.ts) and introduces no new public interfaces, as specified.- ✅ Reproduced the error: I wrote a temporary test that constructed an Anthropic agent with both web searches enabled and confirmed
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 →