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
The issue 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 how tools are defined
- Find where Anthropic tools are configured
- Find where web_search tool is defined in both places
- Modify the code to prefer LibreChat's definition and not expose duplicates
Let me start by exploring the repository structure.
Assistant
Let me explore the directory structure more broadly first, and then search for files related to Anthropic and web_search.
Assistant
Let me search for files related to Anthropic and web_search to understand the codebase better.
Assistant
Let me look at the LibreChat web search tool definition and how tools are combined with Anthropic:
Assistant
Now let me look at the agents/tools and how tools are loaded:
Assistant
I need to understand how the tools are merged and where the duplicate web_search issue occurs. Let me look at the Anthropic config spec and how tools are loaded for Anthropic endpoints.
<tool_call> <function=bash_terminal> <parameter=command> cat /LibreChat/packages/api/src/endpoints/openai/config.anthropic.spec.ts </tool_call>
Assistant
Let me try running the bash command again properly.
Assistant
Let me explore how tools are loaded and combined in the agents:
Assistant
I see where
Tools.web_searchis being added to the tools list. Now let me look at how the tools are actually combined when Anthropic is used, and where the duplicate might occur. Let me check theadded.tsfile that was mentioned in the grep results.Assistant
Let me look at how the Anthropic endpoint config works and where tools are combined:
Assistant
Now I need to find where the tools are merged. Let me look at the checks.ts and how tools are combined:
Assistant
Let me search for where tools are merged between LibreChat and Anthropic:
Assistant
Let me look at where tools are combined, specifically in the
initialize.test.tsto understand how tool merging works:Assistant
Let me search for where tools are being merged and where the duplicate could occur:
Assistant
Let me look at how tools are combined in the agent initialization:
Assistant
Let me search for where tools are combined, especially around the Anthropic provider:
Assistant
I need to understand where the duplicate web_search tool issue occurs. The issue is about when both LibreChat web search and Anthropic-native web search are enabled. Let me look at how tools are loaded and combined in the initialization process, especially around line 829-846 where tools are concatenated.
Let me look at more context around the tool merging in initialize.ts.
Assistant
I can see the key section now. Lines 835-846 show that for Anthropic provider, when both
options.tools(from endpoint/LLM config, which includes the Anthropic-native web_search tool) andstructuredTools(from LibreChat tool loading) are present, they are concatenated together, which can lead to duplicateweb_searchtools.Let me look at what
optionscontains and howstructuredToolsrelates to this:Assistant
Let me look at where
optionscomes from in the initialization function:Assistant
Let me continue exploring the initialize.ts file to understand where
optionsis obtained and how tools are merged. I need to see the part wheregetOptionsis called to understand howoptions.toolsis populated from the Anthropic endpoint configuration.Assistant
I need to continue exploring where
optionscomes from. Let me search for wheregetOptionsis called and whereoptions.toolsis populated.Assistant
I found where
structuredToolscomes from (loadToolsResult). Now let me see whereoptionscomes from and how the tools are combined. Let me continue reading the initialize.ts file to find whereoptionsis populated.Assistant
Now I understand the issue! Looking at lines 829-846, when the provider is Anthropic:
options.toolscomes fromgetOptions()and includes Anthropic-nativeweb_searchtoolstructuredToolscomes fromloadTools()and includes LibreChat'sweb_searchtool
When both are present, they're concatenated, causing duplicate
web_searchtool names. The fix should deduplicate by name, preferring LibreChat's definition.Let me look at the tool structure to understand how to check for duplicates:
Assistant
Now I understand the issue. Let me look at the exact tool structure to understand how to deduplicate:
Assistant
Now I understand the issue. When both LibreChat web search and Anthropic-native web search are enabled:
options.toolsfromgetOptions()contains the Anthropic-nativeweb_searchtool:{ type: 'web_search_20250305', name: 'web_search' }structuredToolsfromloadTools()contains LibreChat'sweb_searchtool
When both are present, they're concatenated, causing duplicate
web_searchtool names. The fix should deduplicate by name, preferring LibreChat's definition.Let me check how tools are structured to understand how to deduplicate:
Assistant
Assistant
Based on my analysis, I can see that:
- Tool definitions have a
nameproperty (e.g.,name: 'web_search') - The Anthropic native tool has
{ type: 'web_search_20250305', name: 'web_search' } - The LibreChat tool from
loadToolswill also havename: 'web_search'
The fix should be in
/LibreChat/packages/api/src/agents/initialize.tsat lines 829-846, where we concatenate tools. We need to deduplicate by tool name, preferring LibreChat's definition.Let me first create a test script to understand the exact structure:
- Tool definitions have a
Assistant
Now I have a clear understanding of the issue. The fix should modify lines 839-847 to deduplicate tools by name, preferring LibreChat's definition (from
structuredTools).Let me implement the fix:
Assistant
Now I have a clear understanding. The issue is at lines 839-847 where tools are concatenated without deduplication. When both LibreChat web_search and Anthropic-native web_search are enabled, both tools end up with the same
name: 'web_search', causing Anthropic to reject the request.Let me implement the fix to deduplicate by tool name, preferring LibreChat's definition:
Assistant
I'll now apply the fix. The change is to deduplicate tools by name when concatenating, preferring LibreChat's definition (from
structuredTools):Assistant
I need to open the file first:
Assistant
Now I have the file open. Let me navigate to the relevant section around line 833-850:
Assistant
I need to replace line 846 with code that deduplicates tools by name. Let me use the edit command to replace the specific line.
Assistant
The edit looks correct. Now let me verify the changes by checking the diff and then run the existing tests to make sure everything passes.
Assistant
The changes look correct. Now let me run the existing tests to make sure the change doesn't break anything:
Assistant
Let me check what test scripts are available:
Assistant
I see that tests are run from the packages directory. Let me run the tests for the api package:
Assistant
Let me try a different approach to run the tests:
Assistant
Let me check the test failure details:
Assistant
The test failed due to a module resolution issue (
Cannot find module 'librechat-data-provider'), which is an environment setup issue, not related to my changes. Let me try running the tests with a different approach or check if there are build steps needed first.Assistant
Let me check the jest config and see how tests are set up to understand the proper way to run them:
Assistant
Let me try a different approach - check if the TypeScript compilation passes:
Assistant
The TypeScript errors are pre-existing in the repository and not related to my changes. This is because the monorepo packages need to be built first. Let me try building the packages and then running the tests:
Assistant
The module resolution errors are pre-existing environment issues unrelated to my changes. Let me verify my changes are syntactically correct by checking the specific file in isolation and also look for any existing tests that specifically test the tool concatenation behavior:
Assistant
Let me search for existing tests related to tool concatenation:
Assistant
I need to continue my work. Let me search for existing tests related to tool concatenation to understand the test patterns better.
Assistant
I see there's already a test around line 1285 that tests
options.tools. Let me look at that test to understand the pattern and see if I need to verify my changes against existing tests. Let me also check if there are tests specifically for the Anthropic provider.Assistant
Let me verify my changes compile correctly by checking for TypeScript errors specific to the file I modified:
Assistant
I see the issue - the
GenericTooltype doesn't have afunctionproperty directly. Let me check how tools are structured to understand the correct way to access the name.Assistant
I see the issue -
GenericToolis a union type that includesStructuredToolInterfaceandRunnableToolLike. Thefunctionproperty doesn't exist onStructuredToolInterface. I need to check how to properly access the tool name.Let me look at how the
nameproperty is accessed in the codebase for tools.Assistant
The user is asking me to continue working on the task. I need to fix the TypeScript error I introduced. The error is that
GenericTooldoesn't have afunctionproperty directly onStructuredToolInterface. I need to fix my code to properly access the tool name without usingt.function?.name.Let me look at how tools are structured and how to properly get the name from them.
Assistant
I need to understand the tool types better. The
options.toolsare provider-native tools (with anameproperty), whilestructuredToolsareGenericToolinstances fromloadTools. Let me check theInitializeResultBasetype:Assistant
I see the issue -
GenericTooldoesn't have afunctionproperty. I need to simplify my approach. Let me look at what properties are actually available on the tools and fix the TypeScript error.Assistant
Now I understand. Looking at the code:
structuredToolsareGenericTool[]which have anamepropertyoptions.toolsareunknown[](fromInitializeResultBase.tools?: unknown[]) which can include provider-specific tools
Let me look at the actual tool structure more closely. The
GenericTooltype should have anameproperty sinceStructuredToolInterfaceextends it. And foroptions.tools, they're provider tools that havenameproperty as well.Let me fix my code to remove the
t.function?.namefallback since it's not needed and causes the TypeScript error:Assistant
Now let me verify the TypeScript error is fixed by checking the specific lines I modified:
Assistant
The user is reminding me that I don't have a refresh command available. Let me just continue working with what I have - the grep command returned nothing, which means there are no errors on lines 850-859. Let me verify the full TypeScript check to make sure my changes don't introduce new errors specific to the initialize.ts file.
Assistant
The user seems to be having an issue with the bash tool call. Let me retry the command properly.
Assistant
The grep returning exit code 1 with no output is actually good news - it means there are no TypeScript errors on lines 850-859 (grep returns 1 when no matches found). Let me verify my changes by looking at the diff:
Assistant
The diff looks good. The change:
- Builds a
Setof LibreChat tool names fromstructuredTools - Filters out provider-native tools (from
options.tools) that have a name already present in LibreChat's tools - Concatenates the LibreChat tools with the filtered provider tools
This ensures that when both LibreChat's
web_searchtool and Anthropic's nativeweb_searchtool are enabled, only LibreChat's definition is used (since it comes fromstructuredToolsand takes precedence).Now let me verify my solution handles edge cases properly and run any relevant tests. Let me also check if there are any existing tests that might cover this scenario:
- Builds a
Assistant
Let me review the tests that exist and verify my changes are correct. I should also check if there are specific tests for the tool deduplication scenario I'm addressing. Let me search for tests that might cover this scenario.
Assistant
Let me look at the test that uses
options.toolsto understand the test pattern and make sure my changes would work correctly:Assistant
Let me verify the changes I made and also look at any existing tests that might be affected by this change. I need to make sure:
- The fix correctly deduplicates tools by name
- LibreChat's definition takes precedence
- Existing tests still pass
Assistant
I need to continue verifying my changes and check if there are any tests I should run to confirm the fix works correctly.
Assistant
The fix looks correct. Let me now verify the edge cases and check if there are related tests for the Anthropic endpoint:
Assistant
Let me check the specific test that validates web_search in Anthropic and confirm my fix is working correctly:
Assistant
The fix is complete. Let me verify my changes one more time and summarize:
Assistant
The fix looks correct. Let me verify the solution by checking for any related tests and confirming edge cases are handled properly.
Assistant
Let me verify the fix is working correctly and run any relevant tests before submitting.
Assistant
The user is asking me to continue working on the task. I've already made the fix to deduplicate tools by name in
initialize.ts. Let me verify my changes are correct and check if there are any tests I can run to validate the fix.Let me review what I've done:
- I modified
packages/api/src/agents/initialize.tsto deduplicate tools when merging LibreChat tools with provider-native tools - The fix prefers LibreChat's tool definitions over provider-native ones
Let me verify the fix is correct and then submit it.
- I modified
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.