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 search for files related to Anthropic and web search to understand the issue better.
Assistant
The issue is about Anthropic rejecting duplicate
web_searchtool names when both LibreChat web search and Anthropic-native web search are enabled. Let me look at the Anthropic-related files to understand how tools are being constructed.Assistant
Now I can see the
getLLMConfigfunction that creates tools for Anthropic. It adds aweb_searchtool whenenableWebSearchis true. Let me look at how tools are combined and where the duplicate issue might occur.Assistant
Let me look at where tools are combined and how Anthropic's native tools are merged with LibreChat's tools.
Assistant
Let me search for where the tools from the Anthropic LLM config are being combined with other tools.
Assistant
This file shows the
getOpenAIConfigfunction that handles tool configuration. When using Anthropic through OpenAI, it callsgetAnthropicLLMConfigand stores the tools. Now I need to find where these tools are combined with other tools, like LibreChat's web search tool.Assistant
Let me look for where tools are being combined for Anthropic, especially where the web_search tool might be duplicated.
Assistant
Let me search for where Anthropic tools are being combined with LibreChat tools in the agents or handlers.
Assistant
Let me search for where tools are being combined in the handlers or agent initialization.
Assistant
Let me look at the
initialize.tsfile where tools are being set up for agents.Assistant
Let me continue reading the initialize.ts file to find where tools are combined:
Assistant
I found the key section! Let me see more of this file, particularly around line 829 where tools are concatenated:
Assistant
Now I understand the issue. When both LibreChat web search and Anthropic-native web search are enabled:
options.toolscontains the Anthropic-nativeweb_searchtool (fromgetLLMConfig)structuredToolsmay contain the LibreChatweb_searchtool- They are concatenated, resulting in duplicate
web_searchtool names
Let me check what the
Tools.web_searchconstant is:Assistant
So
Tools.web_searchis'web_search'. Now let me check how the Anthropic tools are defined and look for whereoptions.toolscomes from:Assistant
Let me look at how tools flow through the endpoints. Let me check the OpenAI config file more thoroughly:
Assistant
I need to understand how the tools are returned from getOpenAIConfig and where options.tools comes from. Let me check the return statement of getOpenAIConfig.
Assistant
Let me look at the full
getOpenAIConfigfunction to see how tools are built:Assistant
Now I understand the flow. Let me check how the tools look and where the deduplication should happen:
Assistant
Now let me look at the tests to understand the expected behavior better:
Assistant
Let me look at the initialize.ts file more carefully to understand where the duplicate tools can be introduced:
Assistant
Now I understand the issue. When:
- Anthropic provider is used with
web_searchenabled natively (inoptions.tools) - AND LibreChat's
web_searchtool is also instructuredTools
They get concatenated on line 846, creating duplicate tool names.
Let me check where the LibreChat web_search tool might be coming from:
- Anthropic provider is used with
Assistant
Now let me check the tool registry definitions to understand how web_search is defined:
Assistant
Now I can see that
WebSearchToolDefinitionis imported from@librechat/agents. This is the LibreChat web_search tool. Let me check what the name is:Assistant
Now I understand the problem clearly. The LibreChat
WebSearchToolDefinitionhas nameweb_search, and the Anthropic native web search tool also hasname: 'web_search'. When both are enabled, they get duplicated.Let me look at the test file that was mentioned to understand the expected behavior better:
Assistant
Let me look at the test file for initialize to understand how duplicate web_search is expected to be handled.
Assistant
Now I need to understand where the deduplication should happen. Looking at the issue description again:
When LibreChat web search and Anthropic-native web search are both enabled, expose one
web_searchtool and prefer LibreChat's definition.So the fix should be in the initialization code where tools are combined. When
options.toolscontains the Anthropic-nativeweb_searchandstructuredToolscontains LibreChat'sweb_search, we should deduplicate and prefer LibreChat's definition.Let me look at the specific location where tools are combined for Anthropic:
Assistant
Now I understand the problem clearly. The fix needs to deduplicate tools when concatenating
structuredToolswithoptions.tools, preferringstructuredTools(LibreChat's definition). Let me implement this fix:Assistant
I can see that
Toolsis already imported fromlibrechat-data-provider. Let me look at the exact location and make the fix:Assistant
Now I need to modify this section to deduplicate tools when there's an overlap. The fix should:
- When concatenating
structuredToolswithoptions.toolsfor Anthropic provider - Filter out any tool from
options.toolsthat has the same name as a tool instructuredTools - This way, LibreChat's definition (in
structuredTools) takes precedence
Let me create the fix:
- When concatenating
Assistant
I'll edit the file to fix the duplicate tool issue. The solution is to deduplicate by tool name when concatenating, preferring
structuredTools(LibreChat's definition) overoptions.tools(provider-native definition):Assistant
Let me use the heredoc syntax to avoid the bash escaping issues:
Assistant
The changes look good. Now let me verify the fix by running the existing tests to make sure I didn't break anything:
Assistant
The tests failed but let me check what the errors are about:
Assistant
let me check if the tests were passing before my changes by looking at the test infrastructure and trying to build the project first.
<tool_call> <function=bash_terminal> <parameter=command> cd /LibreChat && npm run build 2>&1 | tail -50 </tool_call>
Assistant
I made an error by calling bash_terminal twice. Let me correct that.
Assistant
Good, the build succeeded. Let me now run the tests for the specific initialize test file:
Assistant
All 34 tests passed. Now let me also run the tests related to Anthropic to make sure my fix works correctly:
Assistant
All 29 Anthropic config tests passed. Let me now check the diff to confirm the changes I made:
Assistant
Good, I can see my changes are correct. Now let me create a test script to verify the fix handles the specific case described in the issue:
Assistant
The test script confirms that my fix works correctly:
- When both LibreChat and Anthropic have
web_search, only one is kept (LibreChat's definition) - When only one side has a tool, it's included
- Empty arrays are handled correctly
Let me also run more comprehensive tests to ensure nothing is broken.
- When both LibreChat and Anthropic have
Assistant
All 742 agent tests passed. Let me verify my changes one more time:
Assistant
... I need to revert the package.json and package-lock.json changes since those are not part of my fix. Let me do that now.
Assistant
Now let me verify the final diff only contains my changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.