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. The problem is about duplicate
web_searchtool names when both LibreChat web search and Anthropic-native web search are enabled. I need to:- First, explore the repository structure to understand the codebase
- Find where web_search tools are defined for Anthropic
- Implement a fix that exposes only one
web_searchtool and prefers LibreChat's definition - Make sure I don't affect unrelated tools or other providers
Let me start by exploring the repository.
Assistant
Good, I found files related to web_search. Let me focus on the Anthropic-related files since the issue is about Anthropic rejecting duplicate web_search tool names.
Assistant
Let me explore how tools are loaded and combined, especially for Anthropic:
Assistant
Now let me look at the web search tool definition to understand how LibreChat's web search tool is defined:
Assistant
Let me look at the tool definitions to understand how
web_searchis defined in the registry:Assistant
Let me look at the definitions.ts file to understand how tool definitions are loaded:
Assistant
Now let me look at the Anthropic-specific handling for tools. Let me look at where tools are actually combined/sent to Anthropic:
Assistant
Now I need to understand how tools are combined when both LibreChat web search and Anthropic-native web search are enabled. Let me search for how tools are formatted for Anthropic:
Assistant
Now let me look at how tools are formatted for the Anthropic endpoint. Let me look at the transform files and also how the agents/tools are processed:
Assistant
Let me look at how Anthropic tools are combined with agent tools and sent to the API. Let me check the agents handlers:
Assistant
Now I need to understand where the tools are actually combined. Let me look at how the agents handle tools with Anthropic:
Assistant
Let me search for how tools are sent to Anthropic:
Assistant
Let me look at how tools are merged when sent to the Anthropic API. The key is understanding how the LLM's
toolsfromgetLLMConfigare combined with agent tools:Assistant
Let me look at where tools are actually formatted when calling the Anthropic API. I need to find where
getLLMConfigresult'stoolsare combined with agent tools:Assistant
I've found where tools are returned from the config. Now I need to understand how these tools from
getAnthropicLLMConfig(which includes theweb_searchtool when Anthropic-native web search is enabled) are combined with the LibreChat agent tools. Let me look at how these are combined further.Assistant
Good. So
toolsfromgetAnthropicLLMConfigare returned in the config result. Now I need to find where thesetoolsare combined with agent tools. Let me look at how the agents use the config:Assistant
Let me search for where the tools from the config result are combined with agent tools when running the agent:
Assistant
Let me look at the
initialize.tsfile for agents to understand how tools are combined:Assistant
Let me continue reading the agent initialization, focusing on where tools are loaded:
Assistant
I found the key section! Let me continue reading to understand the full flow:
Assistant
Now I can see the key issue. Look at this code around line 830-847:
let tools: GenericTool[] = options.tools?.length ? (options.tools as GenericTool[]) : (structuredTools ?? []); if ( (agent.provider === Providers.GOOGLE || agent.provider === Providers.VERTEXAI) && options.tools?.length && hasAgentTools ) { throw new Error(`{ "type": "${ErrorTypes.GOOGLE_TOOL_CONFLICT}"}`); } else if ( (agent.provider === Providers.OPENAI || agent.provider === Providers.AZURE || agent.provider === Providers.ANTHROPIC) && options.tools?.length && structuredTools?.length ) { tools = structuredTools.concat(options.tools as GenericTool[]); }So when both
options.tools(Anthropic-native tools, which includesweb_search) andstructuredTools(LibreChat agent tools, which may also includeweb_search) are present, they get concatenated. This can cause duplicateweb_searchtool names.The issue says: "When LibreChat web search and Anthropic-native web search are both enabled, expose one
web_searchtool and prefer LibreChat's definition."Assistant
Now I understand the issue. The Anthropic LLM config produces tools like
{ type: 'web_search_20250305', name: 'web_search' }. When combined with LibreChat'sweb_searchtool fromstructuredTools, both havename: 'web_search', causing a duplicate.The fix should be in the agent initialization where tools are concatenated. When the provider is Anthropic and both tool lists contain
web_search, we should prefer the LibreChat definition (fromstructuredTools) and skip the Anthropic-native one.Let me look at the exact location where I need to make the change:
Assistant
Now I understand the fix needed. When concatenating tools for Anthropic, I need to filter out any tool from
options.toolsthat has the same name as a tool already instructuredTools. This way, LibreChat'sweb_searchdefinition (fromstructuredTools) is preferred.Let me first look at the existing tests to understand the expected behavior better:
Assistant
Let me check what the test file looks like around where options.tools and structuredTools are tested together:
Assistant
Let me look at that test:
Assistant
Now I have a clear understanding. Let me implement the fix. The issue is in the tool concatenation logic. When
options.tools(provider tools) containsweb_searchandstructuredTools(agent tools) also containsweb_search, we should prefer the LibreChat'sweb_searchdefinition.The fix should be in
/LibreChat/packages/api/src/agents/initialize.ts. When concatenating tools for Anthropic, filter out tools fromoptions.toolsthat have the same name as tools already instructuredTools.Assistant
Now I need to modify the tool concatenation logic. When both
options.toolsandstructuredToolsare present, I need to filter out any tools fromoptions.toolsthat have the same name as tools instructuredTools. This way, LibreChat's tool definitions take precedence.Let me also check how tool names are accessed in the GenericTool type:
Assistant
So
GenericToolextendsStructuredToolInterfacewhich has anameproperty. Now let me also check whatoptions.toolslooks like:Assistant
Let me continue searching for the LLMConfigResult type definition.
Assistant
So
options.toolsis of typeBindToolsInput[]. Let me check what that type is:Assistant
Now I understand the types. Let me implement the fix. The key change is in the tool concatenation logic - when both
options.toolsandstructuredToolsare present for Anthropic, I need to filter out any tools fromoptions.toolsthat have the samenameas tools instructuredTools.Assistant
Now I understand the issue. When both
options.tools(Anthropic-native tools likeweb_search) andstructuredTools(LibreChat agent tools) are present, they get concatenated without deduplication. This causes duplicateweb_searchtool names when both LibreChat web search and Anthropic-native web search are enabled.The fix should filter out tools from
options.toolsthat have the samenameas tools instructuredTools, preferring LibreChat's definition. Let me implement this:Assistant
I need to modify the tool concatenation logic to filter out duplicate tool names from
options.toolswhen a tool with the same name already exists instructuredTools. This ensures LibreChat's definition takes precedence.Assistant
Looking at line 846, I need to modify the concatenation to filter out duplicate tool names from
options.toolsthat already exist instructuredTools. This will ensure LibreChat's definition (fromstructuredTools) is preferred.Assistant
The edit command failed because I didn't use the correct syntax. Let me use the proper syntax with start:end line range.
Assistant
Let me try a different approach to the edit command. I need to make sure the syntax is correct.
Assistant
The edit command is not working because I'm not passing the arguments correctly. Let me try using sed to make the replacement instead.
Assistant
thinking: The edit command keeps failing. Let me try using sed to make the replacement instead. I need to replace line 846 with the new code that filters out duplicate tool names.
Assistant
The sed command seems to have run, but let me verify the change was made correctly.
Assistant
The edit was applied correctly. Let me verify the change looks good by viewing more context:
Assistant
The change looks correct. Now let me run the existing tests to make sure the change doesn't break anything.
Assistant
Many tests are failing due to module resolution issues, not because of my change. Let me try to run just the initialize tests more specifically:
Assistant
Let me check if the build step is needed first, and then try running the tests:
Assistant
Let me continue by running the tests to verify my change doesn't break anything. The build completed successfully. Now let me run the specific test file for the initialize tests.
Assistant
All 34 tests pass. Let me also run the Anthropic-related tests to make sure nothing is broken:
Assistant
All tests pass. Now let me also run the tool-related tests to make sure nothing is broken there:
Assistant
All tests pass. Now let me also verify the fix more carefully by checking if there are any TypeScript errors with my change:
Assistant
Good, no TypeScript errors. Now let me create a simple test script to verify the fix works as expected for the duplicate web_search scenario:
Assistant
My test script confirms that the fix works correctly for all edge cases. Now let me verify the full diff of changes I've made and then submit the solution.
Assistant
I see there are some unrelated changes to
package.jsonandpackage-lock.json. Let me revert those and only keep my actual fix:Assistant
The diff now only shows my fix. Let me run the tests once more to confirm everything is working:
Assistant
All 34 tests pass. Let me also run the other relevant tests to make sure everything is working:
Assistant
All tests pass. Let me now verify my solution one more time by looking at the complete change:
Assistant
Let me go through the checklist:
-
[x] If an error was reported in the issue description, I have successfully reproduced it.
- The issue describes that Anthropic rejects duplicate
web_searchtool names when both LibreChat web search and Anthropic-native web search are enabled. The code analysis showed that ininitialize.ts, when bothoptions.tools(Anthropic-native tools like{ type: 'web_search_20250305', name: 'web_search' }) andstructuredTools(LibreChat agent tools) are present, they were simply concatenated without deduplication, causing duplicate tool names.
- The issue describes that Anthropic rejects duplicate
-
[x] If an error was reported in the issue description, I have confirmed that my fix resolves the error.
-
Assistant
I notice the previous submit attempt had an issue. Let me verify the current state and submit the changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.