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 start by exploring the repository structure to understand how it's organized and find the relevant code related to web search tools and Anthropic integration.
Assistant
Let me search for files related to Anthropic and web search tools.
Assistant
Good, I can see there are files related to Anthropic and web search. Let me explore the Anthropic-related files first, especially the ones about tool definitions.
Assistant
Let me look at the Anthropic LLM endpoint files to understand how tools are defined and used.
Assistant
Let me read the Anthropic LLM file to understand how tools are defined.
Assistant
Now let me look at the web search tool definition from LibreChat:
Assistant
Let me check what
Tools.web_searchresolves to, and look at where tools are being collected and exposed.Assistant
Let me look at the agents code to understand how tools are loaded and combined:
Assistant
Let me look at the
added.tsfile that seems to be related to adding tools:Assistant
Now let me look at how tools are assembled, particularly looking at where both LibreChat tools and Anthropic tools might be combined:
Assistant
Let me look at how tools are being combined, especially in the handlers or the main run logic:
Assistant
Let me look at how tools are being defined and combined for different providers. The issue says that when LibreChat web search and Anthropic-native web search are both enabled, there are duplicate
web_searchtool names which Anthropic rejects. I need to find where tools are being assembled and deduplicated.Let me search for where tools are combined/merged.
Assistant
Let me look at the tools.ts file to understand how tools are being handled.
Assistant
Now I understand the issue. In
initialize.ts, tools are being concatenated when both LibreChat tools (structuredTools) and provider-native tools (options.tools) exist. If both have aweb_searchtool name, Anthropic rejects duplicates. Let me look at the key area more closely.Assistant
The grep command returned an error, meaning "web_search" is not directly in initialize.ts. Let me look more carefully at where tools are combined. The issue is that when tools are concatenated, they might have duplicates. Let me look at the exact spot where
toolsis being built from bothstructuredToolsandoptions.tools.Assistant
Now I can see the issue. At line 846,
structuredTools.concat(options.tools as GenericTool[])concatenates both LibreChat tools and provider-native tools. If both have aweb_searchtool name, Anthropic rejects duplicates. Let me look at the full context around this area and also check the tests.Assistant
Now I understand the issue. At line 846,
structuredTools.concat(options.tools as GenericTool[])concatenates both LibreChat tools and provider-native tools without deduplication. If both have aweb_searchtool, Anthropic rejects duplicates.Let me look at the test files to understand the expected behavior better:
Assistant
I need to focus on one command at a time. Let me first look at the test file to understand the expected behavior around deduplication.
Assistant
Now I need to find where tools are being combined and potentially deduplicated. The issue is about duplicate
web_searchtool names when both LibreChat's web_search and Anthropic-native web_search are enabled.Let me look at the test file more carefully and also check how tool definitions are handled in the event-driven mode (toolDefinitions), since that's where the Anthropic native web_search would come from.
Assistant
Let me trace how tools get combined for Anthropic specifically. Let me look at what
structuredToolscontains and how it relates totoolDefinitions:Assistant
Let me understand the flow better by looking at where structuredTools comes from and how it's populated.
Assistant
Let me look at how the toolDefinitions are handled as well, since there are two paths: tools (GenericTool) and toolDefinitions (LCTool):
Assistant
Let me understand the complete picture of how tools flow through the system by looking at the relevant parts more carefully.
Assistant
Now I need to understand how the tools are assembled for Anthropic specifically. The issue is that both LibreChat's web_search tool and Anthropic's native web_search tool end up with the same name "web_search", causing Anthropic to reject them as duplicates.
Let me trace through the code flow:
structuredToolscomes fromloadTools()- these are LibreChat tools (including web_search)options.toolscomes fromgetOptions()- for Anthropic, this includes the native web_search tool
The fix needs to deduplicate tools by name when concatenating
structuredToolsandoptions.tools, preferring LibreChat's definition.Let me also check if there's a similar issue with
toolDefinitionsin event-driven mode.Assistant
Let me look more closely at the code flow for
toolDefinitionsin event-driven mode and how Anthropic's native tools interact with LibreChat's:Assistant
Let me look at how tool definitions flow for Anthropic, specifically in the responses mode or event-driven mode.
The issue says: "When LibreChat web search and Anthropic-native web search are both enabled, expose one
web_searchtool and prefer LibreChat's definition."Let me trace how toolDefinitions (event-driven mode) are used. I need to look at how toolDefinitions gets populated and whether there's a similar deduplication issue there.
Assistant
Let me understand the flow better. The issue is:
- LibreChat has its own
web_searchtool (name: "web_search") - Anthropic also has a native
web_searchtool (name: "web_search") - When both are enabled, both get passed to Anthropic, causing duplicate tool names
I need to find where these are combined and deduplicate them, preferring LibreChat's definition.
Let me look at the
tools.tsand how tools are registered/merged.- LibreChat has its own
Assistant
Let me understand the full picture by looking at how toolDefinitions (event-driven mode) handles tools from different sources:
Assistant
Now I understand the issue. Let me look at the full picture more carefully to trace both the
toolspath and thetoolDefinitionspath:Assistant
Now I understand the issue. At line 846,
structuredTools.concat(options.tools as GenericTool[])concatenates LibreChat tools and provider-native tools without deduplication. When both have aweb_searchtool (same name), Anthropic rejects them as duplicates.Let me check the test file to understand expected behavior better:
Assistant
Now I understand the issue. At line 846,
structuredTools.concat(options.tools)concatenates LibreChat tools with provider-native tools without deduplication. When both LibreChat'sweb_searchtool and Anthropic's nativeweb_searchtool are enabled, they end up with duplicate names "web_search", which Anthropic rejects.Let me first look at the test file to understand the expected behavior:
Assistant
Now I have a clear picture of the issue. At line 846 in
initialize.ts,structuredTools.concat(options.tools)concatenates LibreChat tools with provider-native tools without deduplication. When both have aweb_searchtool with the same name "web_search", Anthropic rejects them as duplicates.The fix should deduplicate by tool name, preferring LibreChat's definition (from
structuredTools).Let me first look at the relevant test file to understand the expected behavior:
Assistant
Let me now look at the test file more carefully to understand the expected behavior when tools are combined for different providers.
Let me also check if there are any existing tests that validate the deduplication behavior for the
toolsvariable when bothstructuredToolsandoptions.toolsare present.Assistant
Now I need to understand the fix. The issue is that when both LibreChat's
web_searchtool and Anthropic's nativeweb_searchtool are enabled, they both end up with the same name "web_search". When concatenated at line 846, Anthropic rejects duplicates.Let me first create a simple test to reproduce the issue, then apply the fix.
Assistant
Now I have a thorough understanding of the issue. Let me summarize:
Issue: In
/LibreChat/packages/api/src/agents/initialize.tsat line 846, when both LibreChat'sweb_searchtool (structuredTools) and Anthropic's nativeweb_searchtool (options.tools) are enabled, they are concatenated without deduplication. Both tools have the name "web_search", which causes Anthropic to reject them as duplicate tool names.Fix: Deduplicate tools by name when concatenating
structuredToolsandoptions.tools, preferring LibreChat's definition (fromstructuredTools).Let me apply the fix:
Assistant
Now I understand the issue. The fix needs to deduplicate tools by name when concatenating
structuredToolsandoptions.tools, preferring LibreChat's definition. Let me apply the fix.The key change is at line 846, where we need to replace:
tools = structuredTools.concat(options.tools as GenericTool[]);With code that deduplicates by tool name, preferring LibreChat's definition.
Let me also check if there are similar issues with toolDefinitions.
Assistant
Now I understand the issue. I need to deduplicate tools by name when concatenating
structuredToolsandoptions.tools, preferring LibreChat's definition. Let me apply the fix:Assistant
Let me use the edit command to make the replacement.
Assistant
I need to first add the
deduplicateToolshelper function and then use it. Let me open the file and add the function.Assistant
Let me first add the
deduplicateToolshelper function near the top of the file (after the imports), then update the line that does the concatenation.Assistant
Let me now look at what's after the imports section so I know where to add the helper function.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '45,55p' /LibreChat/packages/api/src/agents/initialize.ts </tool_call>
Assistant
I need to add the helper function and then update the concatenation line. Let me do this carefully.
Assistant
Now I'll add the
deduplicateToolshelper function and update the concatenation line. Let me use the edit command:Assistant
Let me use bash to insert the function:
Assistant
I need to insert the deduplication function into the file. Let me use a different approach - I'll use sed to insert the text after line 46 (after the import line).
Assistant
Let me verify the function was inserted correctly:
Assistant
The function was inserted correctly. Now I need to update the line that concatenates
structuredTools.concat(options.tools)to use the newdeduplicateToolsfunction instead. Let me find the new line number:Assistant
Now let me replace the concatenation line with the deduplicateTools call:
Assistant
Let me verify the changes:
Assistant
The changes look good. Now let me verify the complete diff and run the tests to ensure everything works correctly:
Assistant
The diff looks correct. Now let me run the existing tests to make sure nothing is broken:
Assistant
I need to find the correct way to run tests in this repository. Let me check the package.json for the test command.
Assistant
Let me check the packages/api directory for the test command.
Assistant
Let me run the tests in the api package to verify my changes work correctly.
Assistant
The user wants me to run the tests properly. Let me use the correct jest command with
--testPathPatternsinstead of--testPathPattern.Assistant
The tests are failing due to missing module resolution. Let me check if dependencies are installed:
Assistant
Let me try running the tests from the root directory instead:
Assistant
The test environment seems to have some configuration issues. Let me verify my changes by checking the TypeScript syntax and trying to build the project:
Assistant
The TypeScript errors are from node_modules dependencies, not from my changes. Let me verify my changes are syntactically correct by checking just the modified file in isolation:
Assistant
The changes look syntactically correct. Let me verify the complete diff and also check if there's a way to run the tests that works:
Assistant
Let me check what changes were made to package.json (they shouldn't be from my changes):
Assistant
Let me reset the package.json and package-lock.json changes since they're not related to my fix, then verify my actual changes.
Assistant
Let me verify my changes are correct and complete by checking the final diff:
Assistant
The diff looks correct and addresses the issue. Let me verify my fix handles edge cases correctly by creating a quick test script:
Assistant
All tests pass. The
deduplicateToolsfunction correctly handles all edge cases:- When both LibreChat and provider have
web_search, LibreChat's definition is kept - Non-overlapping names are all included
- Only base tools case works
- Only extra tools case works
- Multiple duplicates are handled correctly
- Null/undefined tool names are safely handled
Let me verify the final state of the changes:
- When both LibreChat and provider have
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.