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 find the relevant files related to web search and Anthropic tool handling.
Assistant
Let me look at the Anthropic-specific files to understand the issue:
Assistant
Now let me look at where tools are being combined or merged, particularly around Anthropic:
Assistant
Let me look at the load.ts file which likely handles tool loading:
Assistant
Let me search for where tools are being processed and combined for Anthropic:
Assistant
Let me look for where tools get processed when Anthropic is the provider:
Assistant
Now let me look at how tools are handled when combining tools from different sources. Let me search for tool handling more broadly:
Assistant
Let me look at the tools directory:
Assistant
Let me look at where the web_search tool from LibreChat is defined and how tools are merged when Anthropic is the provider:
Assistant
Let me look at the registry definitions to see how the web_search tool is registered:
Assistant
Now let me look at the
@librechat/agentspackage to understand the WebSearchToolDefinition:Assistant
Now I need to understand where the tools are actually combined/passed to Anthropic. Let me look for where tools are formatted and combined for the Anthropic provider:
Assistant
Let me look at the agents run.ts and related files to understand how tools are processed for the Anthropic provider:
Assistant
Let me search for where the duplicate tool issue happens - specifically how tools are added to requests for Anthropic:
Assistant
Let me look for where the tools get combined with Anthropic's native tools, especially how the
getLLMConfigresult is used:Assistant
Let me look at the Anthropic initialize.ts to see how tools are combined:
Assistant
Now I need to find where the tools from
getLLMConfigare combined with other tools. Let me search for where this result is used:Assistant
Let me look for where the result from
initializeAnthropicis used to understand how tools are merged:Assistant
Now let me look at where the result is consumed. Let me search for how tools are combined when there's both an agent-level
web_searchtool and Anthropic's nativeweb_search:Assistant
Let me look at the agents/run.ts file more closely around where tools are being used:
Assistant
Let me continue reading the run.ts file, especially around line 917 where tools are set:
Assistant
Assistant
Now I found the key section. Let me look more closely at this part of the code around line 690-710:
Assistant
Now let me find the exact section where tools are combined for Anthropic:
Assistant
Now I found the key area! The issue is around lines 829-836. When the provider is
ANTHROPIC, and bothoptions.tools(from Anthropic's native web_search) andstructuredTools(from LibreChat's web_search tool) exist, they are simply concatenated together, which can cause duplicateweb_searchtool names.Let me understand what
options.toolsandstructuredToolscontain:options.toolscomes from the AnthropicgetLLMConfig()which returns tools like{ type: 'web_search_20250305', name: 'web_search' }structuredToolscomes from the agent's tools array loaded vialoadTools, which includes LibreChat'sweb_searchtool definition
The issue is that when both LibreChat web search and Anthropic-native web search are enabled, both expose a tool named
web_search, causing a conflict.Let me check what
WebSearchToolNameis to confirm:Assistant
Now let me look at the Anthropic llm.ts again to see the exact tool definition:
Assistant
Now I understand the issue completely. When both LibreChat's web_search and Anthropic's native web_search are enabled:
- The agent's
toolsarray includes'web_search'(LibreChat's web search tool) - The Anthropic
getLLMConfig()returns{ type: 'web_search_20250305', name: 'web_search' }inoptions.tools - In
initialize.ts, they get concatenated:tools = structuredTools.concat(options.tools) - This results in TWO
web_searchtools being sent to Anthropic, which rejects duplicates
The fix should be in the Anthropic
llm.tsfile - we should NOT add the nativeweb_searchtool if LibreChat's web_search is already being used. According to the issue: "expose oneweb_searchtool and prefer LibreChat's definition."Let me check how to determine if LibreChat's web_search tool is already in use:
- The agent's
Assistant
The tool encountered an error (exit code 1) but the description is empty. Let me try a different approach to continue exploring the codebase.
Assistant
The Tools enum is defined in librechat-data-provider. Let me look at how tools are checked/filtered to understand the deduplication logic needed.
Assistant
Now let me understand the flow better. The key is that in
initialize.ts, when combining tools for Anthropic:options.toolscomes from Anthropic'sgetLLMConfig()- contains native Anthropic toolsstructuredToolscomes from agent's tool loading - contains LibreChat's tool definitions
The fix should be: when both exist and there would be a
web_searchconflict, we should prefer LibreChat's definition and NOT include Anthropic's nativeweb_searchtool.Let me check how the tools are structured:
Assistant
Let me look at the relevant test for web_search in the Anthropic config spec:
Assistant
Let me look at the structure of how tools are combined in initialize.ts more carefully:
Assistant
Now I understand the problem. The fix needs to be made in the tool concatenation logic. When both Anthropic native tools and structured tools are present, we should filter out duplicate tool names, preferring the LibreChat (structuredTools) definition.
The best place to make this fix is in the
initialize.tsfile where tools are concatenated. I need to:- When combining
structuredToolsandoptions.toolsfor Anthropic (and OpenAI/Azure), filter out tools fromoptions.toolsthat have the same name as tools instructuredTools.
Let me look at the structure of the tool objects to understand how to compare names:
- When combining
Assistant
Need to understand the tool structure better.
Assistant
Let me look at the GenericTool type definition to understand the structure.
Assistant
Let me search for how the tool name is accessed on different tool types to understand the structure.
Assistant
Now I understand the structure better. Let me check what properties the tools from
options.tools(Anthropic native) have vsstructuredTools(LibreChat):Assistant
Now I have a clear understanding of the issue. Let me implement the fix:
The Problem:
- When both LibreChat web_search and Anthropic-native web_search are enabled, both tools are added to the tools array
- LibreChat's
web_searchtool hasname: 'web_search' - Anthropic's native web_search tool also has
name: 'web_search' - This causes duplicate tool names which Anthropic rejects
The Solution: When concatenating tools for Anthropic provider, filter out native Anthropic tools that have the same name as LibreChat tools, preferring LibreChat's definition.
Let me implement this fix:
Assistant
Now I understand the issue completely. Let me implement the fix. The problem is on line 846 where tools are simply concatenated without checking for duplicates. For Anthropic, we need to filter out native tools that have the same name as LibreChat tools.
Assistant
Now I can see the code that needs to be modified. I need to change line 846 to filter out tools from
options.toolsthat have the same name as tools instructuredTools, preferring the LibreChat definition.Assistant
Let me use the replace command instead:
Assistant
Let me try a different approach - use the sed command:
Assistant
I need to use a different approach since Python is not available. Let me try using sed to make the replacement. I'll need to carefully construct the sed command to replace the specific code block.
Actually, let me re-examine the edit command syntax. The issue is that I was providing too many arguments. Let me try using the edit command with just the line range and replacement text.
Assistant
Interesting, there was a bash history expansion issue but it seems the file was still updated. Let me verify the changes:
Assistant
I see the issue - the filter callback got cut off due to the bash history expansion issue with the
!character. Let me fix this:Assistant
Let me verify the changes were made correctly by viewing the updated file section.
Assistant
The code changes look correct. Now let me verify the changes compile correctly and run any existing tests:
Assistant
Let me try a different approach to find and run the tests.
Assistant
Good, I found the test file. Let me look at the initialize.test.ts file to understand what tests exist and if I need to verify my changes don't break anything.
Assistant
Let me continue looking at the test file to understand how to verify my changes, and then run the tests to ensure nothing is broken.
Assistant
Let me check if there are any tests specifically around the duplicate tool issue, and also run the existing tests to make sure my change doesn't break anything. Let me first look at how the tests are structured and if there's a test for Anthropic tool conflicts.
Assistant
Let me continue by running the tests to verify my changes work correctly.
Assistant
I need to find the correct way to run tests in this repository.
Assistant
The tests are failing due to module resolution issues. This is likely a build/linking issue rather than a problem with my code changes. Let me check if the code compiles correctly using TypeScript first, and also look for any existing tests that might be related to the duplicate tool issue.
Let me try to build the package first to make sure there are no compilation errors.
Assistant
The build succeeded! The warnings are expected in this monorepo setup. Now let me verify my changes are correct and create a test to validate the fix works as expected.
Assistant
Good, my changes are in place. Let me now verify the logic by creating a simple test script to confirm the deduplication works correctly.
Assistant
Excellent! The deduplication logic is working correctly. Now let me verify the complete solution and check for edge cases:
Assistant
All edge cases pass. Now let me verify my changes one more time and ensure the code is correctly formatted. Let me also check if there are any TypeScript compilation issues with the specific file I modified.
Assistant
I can see there's a TypeScript error at line 859:
packages/api/src/agents/initialize.ts(859,42): error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.Let me look at that specific line to see what the issue is:
Assistant
The TypeScript error is because
(tool as { name?: string }).namecan bestring | undefined, butSet.has()expects astring. Let me fix this:Assistant
The TypeScript error is because
(tool as { name?: string }).namecan bestring | undefined, butSet.has()expects astring. I've already updated the code to handle this case by checking iftoolNameis null before callingstructuredToolNames.has(toolName).Now let me verify the changes are correct and check if the TypeScript compilation passes.
Assistant
Good, the changes look correct now. Let me run the TypeScript compiler again to verify there are no more errors in the initialize.ts file related to my changes:
Assistant
Good, the TypeScript error at line 859 is gone. Let me verify the complete diff of my changes:
Assistant
Let me review my changes one final time before submitting:
Assistant
I see there are changes to package-lock.json and package.json as well. Let me check what those changes are - they shouldn't be part of my fix:
Assistant
These package.json and package-lock.json changes appear to be unrelated configuration changes that were already in the repository. Let me revert those changes to ensure only my actual fix is submitted:
Assistant
Let me verify the final state of my changes before submitting.
Assistant
The user is asking me to take action since I made a statement but didn't invoke a tool call. Let me verify the final state and then submit my changes.
Assistant
Now let me verify my final changes one more time and submit the solution.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.