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 analyze the issue description:
- When both LibreChat web search and Anthropic-native web search are enabled, there are duplicate
web_searchtool names - Anthropic rejects duplicate tool names
- The fix should expose one
web_searchtool and prefer LibreChat's definition - Should not affect unrelated tools or other providers
Let me start by exploring the repository structure to understand how web search tools are configured and used.
- When both LibreChat web search and Anthropic-native web search are enabled, there are duplicate
- Assistant
Let me look for Anthropic-related files and web search related files.
- Assistant
Let me focus on the Anthropic-specific files and understand how tools are handled there.
- Assistant
Let me look at the Anthropic LLM file to understand how tools are being processed.
- Assistant
Now let me understand how tools are processed in the Anthropic endpoint. The issue is that when both LibreChat's web search tool and Anthropic's native web search are enabled, there are duplicate
web_searchtool names. Let me check how tools are combined. - Assistant
Let me search for where tools are combined for Anthropic requests to understand the duplicate tool name issue.
- Assistant
Let me look at the key files related to Anthropic and web search:
- The Anthropic LLM config
- The agents/added.ts file
- The tools/toolkits/web.ts file
- The agents/load.ts file
- Assistant
Let me look at the tools toolkit for web search to understand how LibreChat defines the web_search tool.
- Assistant
Now let me look at the agents/tools.spec.ts to understand the duplicate tool issue.
- Assistant
Let me look at the agents/tools.ts file to understand how tools are built.
- Assistant
Let me look at the agents/load.ts file and see how tools are loaded.
- Assistant
Let me search for where tools from getLLMConfig are combined with agent tools.
- Assistant
Let me look at the initialize.ts file in the anthropic endpoint to understand how tools are combined.
- Assistant
Let me search for where tools from getLLMConfig are used and combined with agent tools.
- Assistant
Let me look at the agents/initialize.ts file to see how tools are initialized for agents.
- Assistant
Let me search for where the Anthropic-native tools are combined with LibreChat tools.
- Assistant
Now I can see the issue! In
initialize.ts, when the provider is Anthropic and bothoptions.tools(which comes fromgetLLMConfigand includes the Anthropic-nativeweb_search) andstructuredTools(which includes LibreChat'sweb_search), they are concatenated together. This results in duplicateweb_searchtool names, which Anthropic rejects.Let me look at this more carefully:
- Assistant
Now I understand the issue! When Anthropic is the provider and both LibreChat web search and Anthropic-native web search are enabled, there will be duplicate
web_searchtool names. The fix should be to:- When concatenating tools for Anthropic, check if there are duplicate tool names
- If there are duplicates, prefer LibreChat's definition over Anthropic's
- Do not affect unrelated tools or other providers
Let me look at the full context of the initialize.ts file to understand how to implement this fix.
- Assistant
Let me check the full context around the tool concatenation code.
- Assistant
Let me look at the context around line 846 in initialize.ts to understand the logic better.
- Assistant
Now I understand the issue. When both LibreChat web search and Anthropic-native web search are enabled, we get duplicate
web_searchtool names. The fix should filter out duplicates when combining tools for Anthropic, preferring LibreChat's definition.Let me check what the GenericTool type looks like:
- Assistant
Let me check the agents/tests/initialize.test.ts file to see if there are existing tests that might help me understand how tools are combined:
- Assistant
Let me search for tests related to Anthropic tools in the test file:
- Assistant
Let me search for Anthropic in the test file.
- Assistant
Let me look more closely at the tool merging logic and understand the full picture:
- Assistant
Let me look at how LibreChat's web search tool is defined and how tools are constructed:
- Assistant
Let me understand how tools are loaded and what LibreChat's web_search tool looks like:
- Assistant
Let me look at the OpenAI config for Anthropic to understand how tools are being handled:
- Assistant
Let me now look at where the tools from getLLMConfig are merged with agent tools to understand the flow better:
- Assistant
Let me now understand the flow better by looking at the actual tool types and how they're used:
- Assistant
Let me now look at the test file for agents/initialize to see if there are tests related to Anthropic tools:
- Assistant
Let me look at the full test file for agents/initialize to understand the structure better:
- Assistant
Let me look more carefully at how tools are merged. I see the core issue:
In
initialize.ts(line ~846):tools = structuredTools.concat(options.tools as GenericTool[]);When Anthropic provider has
web_searchenabled ingetLLMConfig,options.toolscontains{ type: 'web_search_20250305', name: 'web_search' }. And when LibreChat's web search is also enabled,structuredToolsalso contains a tool namedweb_search. This creates a duplicate.Now let me understand the problem. The
toolsarray fromgetLLMConfigcontains Anthropic-native tool definitions with anameproperty. When merged with LibreChat tools (which also have anameproperty), we get duplicates.Let me check how the Anthropic native tool is defined in getLLMConfig:
- Assistant
Now I understand the issue. In
initialize.ts, when both LibreChat web search and Anthropic-native web search are enabled, they both produce tools namedweb_search, which causes duplicates. The fix should filter out Anthropic-native tools that conflict with LibreChat tools, preferring LibreChat's definitions.Let me implement the fix:
- Assistant
Now I understand the issue. When both LibreChat web search and Anthropic-native web search are enabled, they both produce tools named
web_search, which causes duplicates when concatenated. The fix should filter out Anthropic-native tools that conflict with LibreChat tools, preferring LibreChat's definitions.Let me implement the fix:
- Assistant
Now let me understand the full picture. The issue is that when both LibreChat web_search and Anthropic-native web_search are enabled, there are duplicate
web_searchtool names. Let me check iftoolDefinitionscan also have this conflict: - Assistant
Let me check how
toolDefinitionsis used with Anthropic tools: - Assistant
Let me trace the full flow. The
options.toolsfromgetLLMConfigcontains the Anthropic native web_search tool. Let me check what the tool looks like: - Assistant
Now I understand the issue clearly. When both LibreChat web search and Anthropic-native web search are enabled:
options.toolscontains{ type: 'web_search_20250305', name: 'web_search' }(fromgetLLMConfig)structuredToolscontains LibreChat's web_search tool- When concatenated, there are duplicate
web_searchtool names, which Anthropic rejects
The fix should filter out tools from
options.toolsthat have the same name as tools instructuredToolswhen the provider is Anthropic. This ensures LibreChat's definition is preferred.Let me implement the fix:
- Assistant
Now I understand the issue clearly. When both LibreChat web search and Anthropic-native web search are enabled, they both have the same tool name
web_search, causing duplicates that Anthropic rejects.The fix should filter out Anthropic-native tools that have the same name as LibreChat tools when combining them for the Anthropic provider. Let me implement this fix:
- Assistant
Now I understand the issue. When both LibreChat web search and Anthropic-native web search are enabled, there are two
web_searchtools - one from LibreChat and one from Anthropic's native tool list. This causes Anthropic to reject the request due to duplicate tool names.The fix should filter out the Anthropic-native
web_searchtool fromoptions.toolswhen a LibreChatweb_searchtool already exists instructuredTools(ortoolDefinitions). Let me implement the fix: - Assistant
Now I need to also consider the event-driven mode where
toolDefinitionsis used instead ofstructuredTools. Let me check if there's a similar issue withtoolDefinitions: - Assistant
Now I need to also consider the event-driven mode where
toolDefinitionsis used instead ofstructuredTools. Let me check if there's a similar issue withtoolDefinitions: - Assistant
Let me also check if there are any tests that verify this behavior and need to be updated:
- Assistant
Let me verify that my fix doesn't break any existing tests. Let me run the tests for the initialize.ts file:
- Assistant
Let me try running the tests differently:
- Assistant
I'm already in /LibreChat/packages/api directory.
- Assistant
I'm already in /LibreChat/packages/api. Let me try running the test from here.
- Assistant
Let me find the jest config file.
- Assistant
The jest config is jest.config.mjs, not jest.config.js. Let me run the test from the root.
- Assistant
Let me try the correct option.
- Assistant
Let me check the package.json scripts for test commands:
- Assistant
The tests are failing because
librechat-data-providercan't be found. Let me check if there's a build step or if I need to run tests differently: - Assistant
Let me check how tests are run in this project.
- Assistant
Let me check how tests should be run in this project:
- Assistant
Let me look at the test scripts.
- Assistant
The test runner needs the module resolution. Let me check how the
moduleNameMapperis configured in the jest config. - Assistant
Let me check if there's a root-level jest config or if tests need to be run differently:
- Assistant
Let me check how the jest config is set up with module mappings:
- Assistant
The module name mapper doesn't include
librechat-data-provideror@librechat/agents. These are likely resolved through node_modules. Let me check if they exist as packages. - Assistant
It seems like the dist directory is empty. Let me check if the packages need to be built.
- Assistant
Let me check if there's a dist directory or index.js file for the module resolution.
- Assistant
Let me try running the test from within the api package directory with the correct working directory:
- Assistant
Let me check how other tests resolve this module:
- Assistant
Let me check the package.json for the main entry point.
- Assistant
Let me check if this is a monorepo with workspace packages:
- Assistant
Let me check if the dist directory exists:
- Assistant
The symlink doesn't have dist. Let me check if the tests run correctly with proper module resolution:
- Assistant
The dist directory doesn't exist. Let me check if there's a way to build the packages:
- Assistant
Let me wait a bit and check if the build completed:
- Assistant
Good, the data-provider package is built. Now let me try running the tests again:
- Assistant
I need to build all the dependent packages. Let me build them:
- Assistant
Let me wait for the build to complete and then check if the dist directory exists:
- Assistant
Let me try running the tests again:
- Assistant
All tests pass. Now let me run the Anthropic LLM tests to make sure I haven't broken anything:
- Assistant
All 90 tests pass. Now let me run other related tests to make sure nothing is broken:
- Assistant
All tests pass. Now let me also consider if the event-driven mode with
toolDefinitionsneeds similar handling. Let me check if there's a similar issue there: - Assistant
Let me try with the full path:
- Assistant
Now let me think about this more carefully. I also need to check if there's a similar issue in the event-driven mode where
toolDefinitionsare used. Let me look at how the tools are passed to the Anthropic API: - Assistant
Let me look at the run.ts file to see how tools are used:
- Assistant
Now I need to also consider the event-driven mode. Let me check how the tools are used when creating a run for Anthropic:
- Assistant
Let me now check how tools are used when creating the Anthropic API call:
- Assistant
Let me now look at how the Anthropic LLM is actually called with the tools to understand the full flow:
- Assistant
Now let me trace how the web_search tool ends up in both structuredTools/toolDefinitions AND options.tools. Let me understand the flow better:
- Assistant
Now let me also check if there's a similar issue in the event-driven mode where
toolDefinitionsmight also get duplicates. Let me look at how tools are combined in the run.ts file: - Assistant
Now let me also check if the event-driven mode (toolDefinitions) needs similar handling. Let me look at how toolDefinitions are used when creating a run:
- Assistant
Let me now check the run.ts file to see if there are other places where tools are combined:
- Assistant
Now let me also consider if the event-driven mode (
toolDefinitions) needs similar handling. Let me check iftoolDefinitionscan also have duplicateweb_searchentries when both LibreChat and Anthropic web search are enabled: - Assistant
Let me now look at the OpenAI config for Anthropic to see if there's a similar issue there:
- Assistant
Now let me also check how tools are combined in the OpenAI config to understand the broader picture:
- Assistant
Now let me run all the tests to make sure nothing is broken:
- Assistant
All tests pass. Now let me also run the OpenAI config tests to make sure my changes don't affect other providers:
- Assistant
All tests pass. Let me also run the full OpenAI config tests and the tools-related tests:
- Assistant
All tests pass. Let me also run the remaining related tests:
- Assistant
All tests pass. Now let me also check if there's a similar issue in the event-driven mode where
toolDefinitionsmight also have duplicates. Let me look at how the Anthropic run is created: - Assistant
Now let me also check how tools are passed to the Anthropic API. Let me look at how the Anthropic Chat model receives tools:
- Assistant
Let me search for how the Anthropic model is created:
- Assistant
Let me look at how tools and toolDefinitions are both used when creating a run for Anthropic:
- Assistant
Let me now check how the
createRunfunction in the agents package uses tools: - Assistant
Now let me also check the
createRunfunction in the agents package: - Assistant
Let me now check how the
createRunfunction uses bothtoolsandtoolDefinitions: - Assistant
Let me now check how
toolsandtoolDefinitionsare used when creating the Anthropic LLM: - Assistant
Let me now verify the fix is correct by looking at the full code flow. The key question is: when
options.toolscontains the Anthropic native web_search tool andstructuredToolscontains LibreChat's web_search tool, they get concatenated and there are duplicates. My fix filters out provider tools that conflict with agent tools when the provider is Anthropic.Let me also check if there's a similar issue in the event-driven mode where
toolDefinitionsmight also have duplicates: - Assistant
Let me now also check if there are any other tests that need to be run. Let me also verify that the fix doesn't break the OpenAI provider:
- Assistant
Let me now also check how tools are actually sent to the Anthropic API in the run configuration. Let me look at how the Anthropic Chat model receives tools:
- Assistant
Now let me also check if there's a similar issue in the event-driven mode. Let me look at how
toolDefinitionsandtoolsare used together: - Assistant
Now let me run a broader set of tests to make sure nothing is broken:
- Assistant
All tests pass (911 passed, 4 skipped, 0 failed). Now let me also check if the event-driven mode needs similar handling. Let me look at how
toolDefinitionsandtoolsare both used when creating a run for Anthropic: - Assistant
Let me now check if
toolDefinitionscan also have duplicates with Anthropic's native tools. Let me trace through the code to understand:toolDefinitionscomes fromloadToolDefinitionswhich loads from the tool registryoptions.toolsfromgetLLMConfigcontains the Anthropic native web_search- The Anthropic native web_search is NOT loaded from the tool registry - it's added directly in
getLLMConfig
So
toolDefinitionsshould NOT contain the Anthropic native web_search. Let me verify this by looking at the flow: - Assistant
Good, the fix looks correct. Let me now verify the fix is correct by looking at the complete code and understanding the flow:
structuredToolscontains LibreChat's web_search tool (loaded vialoadTools)options.toolsfromgetLLMConfigcontains Anthropic's native web_search tool- My fix filters out Anthropic's native web_search from
options.toolswhen the provider is Anthropic, because the LibreChat web_search is already instructuredTools
This ensures that:
- When both LibreChat web search and Anthropic-native web search are enabled, only one
web_searchtool is exposed - LibreChat's definition is preferred (since it's already in
structuredTools) - Unrelated tools are not affected
- Other providers (OpenAI, Azure) are not affected
Let me now also check if there's a similar issue in the event-driven mode where
toolDefinitionsmight also have duplicates with Anthropic's native tools: - Assistant
Let me now check the current changes I've made:
- Assistant
I see there are some unrelated changes in
package-lock.jsonandpackage.jsonthat were introduced when building the packages. Let me revert those changes and only keep the actual fix ininitialize.ts: - Assistant
Let me verify that only my fix is in the diff now:
- Assistant
The diff looks clean now. Let me run the tests one more time to make sure everything is working correctly:
- Assistant
All 335 tests pass. Now let me also check if there are any existing tests that specifically test for the Anthropic duplicate web_search scenario. Let me search for tests that might be related:
- Assistant
Let me now check if there's a similar issue with
toolDefinitionsin the event-driven mode. Let me look at how the Anthropic run handles bothtoolsandtoolDefinitions: - Assistant
Let me also check the event-driven mode path where
toolDefinitionsis used instead oftools. Let me look at how the Anthropic run handles both: - Assistant
The
options.toolsis only referenced ininitialize.ts, not inrun.ts. Therun.tsusesagent.toolswhich is already the merged tools. Let me verify my understanding of the event-driven path: - Assistant
Good, I can see that:
tools: agent.tools- this is the merged tools array that I've fixed ininitialize.tstoolDefinitions- this comes fromagent.toolDefinitionswhich is loaded fromloadToolDefinitions(LibreChat's tool definitions)
The
agent.toolscontains both LibreChat tools and provider tools (merged), whileagent.toolDefinitionsonly contains LibreChat's tool definitions. So the fix ininitialize.tshandles the duplicate issue correctly.Now let me also consider if there are any other edge cases I need to handle:
- What if there are no agent tools (structuredTools is empty)? My fix handles this because the condition
structuredTools?.lengthwould be false. - What if there are no provider tools (options.tools is empty)? My fix handles this because the condition
options.tools?.lengthwould be false. - What if the provider is not Anthropic? My fix handles this by only filtering for Anthropic provider.
- What if there are multiple duplicate tool names? My fix handles this by filtering all provider tools that have the same name as any agent tool.
My fix is correct and sufficient. Let me now also run the full test suite to make sure nothing is broken.
- Assistant
All tests pass (1129 passed, 4 skipped, 0 failed). Now let me review the final changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.