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 to understand the codebase and locate relevant files for the Anthropic web_search tool issue. First, let's get a high-level view of the directory structure.
- Assistant
Good, we have some files. Let's look at the repository more broadly to see if there is a web search tool definition. Probably the issue is about tool definitions for Anthropic's API. Let's search for "web_search" in the repository.
- Assistant
Now we need to look at Anthropic's implementation. Let's search for Anthropic's tool definitions, especially web_search. Let's examine the Anthropic endpoint files. First, let's check the Anthropic LLM file.
- Assistant
Let's scroll down to see the rest of the file, especially where tools are defined.
- Assistant
Let's continue reading to see the tool definitions and how web_search is handled. We need to see where tools are being defined for Anthropic.
- Assistant
Interesting! In the Anthropic llm.ts file, I see that the tools array is built at line 296. If enableWebSearch is true, it pushes a tool with
type: 'web_search_20250305'andname: 'web_search'. That's Anthropic's native web_search tool definition. However, the issue mentions that LibreChat also has a web_search tool. So we need to deduplicate when both are present.We need to understand where LibreChat's web_search tool is defined. Let's search for where tools are built across the codebase for Anthropic. Might be in the
initialize.tsfile or somewhere else. Let's examine the Anthropic index.ts file. - Assistant
Let's look at the initialize.ts file to see how tools are set up.
- Assistant
Let me first create a reproduction script to understand the exact issue. Let me look at how tools are combined for Anthropic.
- Assistant
Let me look at the load.ts file in agents which seems to handle tool loading:
- Assistant
Now I need to understand the issue better. Let me look at how tools are combined when creating Anthropic requests. The issue is about duplicate
web_searchtool names when both LibreChat web search and Anthropic-native web search are enabled.Let me check how tools are combined in the Anthropic endpoint initialization. The key is likely in the agent creation or LLM initialization where both LibreChat's web_search tool and Anthropic's native web_search tool might be added.
Let me look at the agent tools.ts file to see how tools are loaded.
- Assistant
Let me look at the Anthropic tools.ts file in the agents package, which likely combines tools:
- Assistant
Let me check where the Tools enum is used and how the Anthropic endpoint config is built. Let me search for where tools are actually passed to Anthropic:
- Assistant
Let me look at the Anthropic endpoint more broadly and see how tools are handled. Let me look at the anthropic endpoint directory structure and check for tool handling:
- Assistant
The structure is clearer now. There are:
- LibreChat's built-in
web_searchtool which gets pushed to thetoolsarray (string arrays) when enabled. - Anthropic's native
web_searchtool which is pushed astype: 'web_search_20250305', name: 'web_search'into thetoolsarray ingetLLMConfig.
The
toolsarray ingetLLMConfigreturns Anthropic-specific tool objects. Thetoolsarray inaddTool(ininitialize.tsandload.ts) returns string tool names.Wait, let me look at
addToolagain. Inload.tsandadded.ts,toolsis astring[]. IngetLLMConfig,toolsis an array of Anthropic tool objects.Wait, if both push to the same
toolsarray, they are different types of objects. But if they are passed to the API, it might be an issue.Let's look at the
@librechat/agentsAnthropic implementation to see how it combines tools. Let's find where tools are combined for Anthropic in@librechat/agents. - LibreChat's built-in
- Assistant
The issue mentions "Anthropic rejects duplicate web_search tool names". This likely means when both LibreChat's
web_searchtool (a function/tool definition) and Anthropic's nativeweb_searchtool are sent to Anthropic API, it sees two tools with the same name and rejects the request.We need to find where the tool lists are combined before being sent to Anthropic. Let's check the
initialize.tsor other files that callgetLLMConfigand then use the result.Let me look at the
run.tsor other agent files that might handle tool combination. Let's also check the@librechat/agentslibrary's Anthropic LLM implementation. - Assistant
Let me look at the Anthropic LLM index to understand how tools are handled in the actual call:
- Assistant
This file is very long - 625 lines. Let me look at the
_generatemethod or theinvokemethod to see where tools are used. Also, I need to see where tools are combined. Let me search for where tools are merged or combined with the Anthropic tools. - Assistant
Let me look at the relevant section around line 411:
- Assistant
Now I need to understand how the tools from
getLLMConfigare combined with the agent's tools. Let me look at how the Anthropic endpoint creates the agent and combines tools. Let me look at the agents/added.ts file: - Assistant
Let me continue reading this file to see how the tools are consolidated.
- Assistant
Now I need to understand the flow better. Let me look at how tools are used in the Anthropic endpoint initialization. I need to see how the
toolsarray fromgetLLMConfigand thetoolsarray from the agent's tool list are combined. Let me search for where both are used together.Looking at the Anthropic llm.ts file, I see that
getLLMConfigreturns an object withtools(which contains Anthropic's native tool definitions) andllmConfig. The issue is likely that when both LibreChat's web_search tool (which would be in the agent's tools array as a string) and Anthropic's native web_search tool (which is in the returnedtoolsarray) are both present, they have the same name.Let me search for where the Anthropic tools are combined with the agent's tools. I need to check the actual API call construction.
- Assistant
So
getLLMConfigis called ininitializeAnthropic, and also in./packages/api/src/endpoints/openai/config.tsperhaps for Anthropic's specific handling?Let's look at
./packages/api/src/endpoints/openai/config.ts: - Assistant
I see it!
Line 82:
const isAnthropic = options.customParams?.defaultParamsEndpoint === EModelEndpoint.anthropic;This suggests that if
isAnthropicis true, it uses Anthropic-specific logic, likely including Anthropic tools.Let's look at
getAnthropicConfigor similar logic inopenai/config.ts. Let's search forgetAnthropicConfigor the block handling Anthropic endpoints. - Assistant
I need to continue looking at the openai/config.ts file to understand how the Anthropic endpoint configuration tools are being merged together with the LibreChat agent/tools. Let me continue viewing this file.
- Assistant
Now I can see that on line 119, the
toolsarray from the anthropicResult is assigned totools. In lines 112-118, only the llmConfig is transformed, buttoolsis preserved from the anthropicResult.Now, I need to understand where the final
toolsarray is built. In theinitializeAnthropicfunction in./packages/api/src/endpoints/anthropic/initialize.ts,getLLMConfigis called on line 83:typescriptconst result = getLLMConfig(credentials, clientOptions);The result is directly returned.
Let me trace how the finalize tool list is added. In OpenAI's
config.ts, thetoolsvariable is used later. Let's look at the rest of theopenai/config.tsfile to see how tools are combined. - Assistant
Let me look at how the agent tools are combined with endpoint-specific tools in the agent framework:
- Assistant
I'm making progress. The key issue is now clearer: when Anthropic endpoint is being used,
getAnthropicLLMConfigcreates a tool withtype: 'web_search_20250305', name: 'web_search'. However, when processing agents,addToolalso pushes'web_search'string to the tools array, which will likely be converted to another tool definition for web_search, leading to two tools with the same name.To understand the issue fully, I should look at how tools are merged and sent to the model. It's likely in the agent initialization or in the final API call preparation. Let me run the existing tests to see if any are currently failing."
Wait, I need to be careful here. The instructions say I should not modify existing tests or add new tests to the repository. My goal is only to edit the source code to resolve the issue. However, running the existing tests to verify that my changes do not break anything is allowed and encouraged.
I need to search for where the tool lists are actually being combined. Let's check in
@librechat/agents's source or inpackages/api/src. - Assistant
- Assistant
Let me search for where tools are used in run.ts.
- Assistant
Let me see line 917 and its surrounding context.
- Assistant
Let me search for where the initResult.tools are actually used, or if the tools are combined with the
agent.toolslater. Let me look at howbuildAgentInputconstructs thetoolsproperty.I see
tools: agent.toolsat line 917, but the endpoint-specific tools (frominitResult.tools) don't seem to be included here.Wait, maybe the tools passed to the
RunConfigorAgentInputsare just the tool names (strings), and the actual tool definitions are separate. Let me look at theRunclass from@librechat/agentsto understand how it processes tools.Actually, looking at line 917 more closely,
tools: agent.toolsis the list of tool names (strings). These tool names are used by theRun/ToolNodeto look up tool definitions.The Anthropic native tools (from ) are NOT tool names - they're Anthropic-specific tool payload objects (like ).
- Assistant
Let me look at how the
llmConfigis built, and where theinitResultis actually used. Let me search forinitializeAnthropicusage. - Assistant
Let me look at where
getProviderConfigis called and how the LLM config is built. - Assistant
Let me check line 365 and see how it's used.
- Assistant
Now I need to look at how
getProviderConfigworks and how it includes the endpoint-specific tools. Let me look atgetProviderConfigfunction to understand how the tools are combined. - Assistant
Let me look at the providers.ts file to see how tools are combined.
- Assistant
Now I understand the flow.
getProviderConfigreturns an initialization function (getOptions) which is called to get the endpoint-specific configuration. This function is eitherinitializeAnthropic,initializeOpenAI, etc.But I still need to see how these tools are combined when agents are run. Let me search for where
getProviderConfigis actually called for the agent run. - Assistant
The
getOptionsis not used anywhere directly inrun.ts. Let me search the whole codebase for wheregetOptionsis used or whereProviderConfigResultis used.Wait, the
ProviderConfigResultis returned fromgetProviderConfig. Let me look atrun.tsto see ifgetOptionsis called.Looking at line 365 again,
getProviderConfigreturns{ overrideProvider, customEndpointConfig }, not{ getOptions, overrideProvider, customEndpointConfig }.Let me look at line 365 more closely. I see
const { overrideProvider, customEndpointConfig } = getProviderConfig({ ... }).So it doesn't destructure
getOptions. Where isgetOptionsused then?Let me search again.
- Assistant
getOptionsis used ininitialize.ts! Let me look at how it's used. - Assistant
Now I need to see how the
optionsare used further. Let's continue looking at the initialize.ts file to see how tools are combined. - Assistant
- Assistant
- Assistant
I need to run the existing tests to verify my fix doesn't break anything. Let me search for test files related to the initialize function.
- Assistant
Good, there's an initialize.test.ts file. Let me check if there are existing tests related to web_search tool deduplication. Let me also check the config.spec.ts file mentioned in the search results for Anthropic config tests.
Let me look for tests specifically related to the Anthropic web_search issue. I found
config.anthropic.spec.tsin the openai directory. Let me check that. - Assistant
Now I need to write a test to verify that my fix works correctly. But I should first check if there's already a test for this issue or if I need to add one. The issue says I should run existing tests to verify my changes don't break anything. Let me check if there's a test for duplicate web_search tools in Anthropic. Let me search for "duplicate" or "web_search" in the test files.
First, let me run the existing tests to see if they pass with my changes. Let me look for test scripts in the repository configuration.
- Assistant
The issue is about creating a test case to reproduce the problem where Anthropic API rejects duplicate
web_searchtool names. I need to design a scenario where both LibreChat's web_search tool and Anthropic-native web_search tool are present, and verify that my fix (filtering out Anthropic's native web_search when LibreChat's is present) resolves the issue.However, the system prompt explicitly states: "UNDER NO CIRCUMSTANCES should you modify existing tests or add new tests to the repository. This will lead to an error in the system that evaluates your performance."
I need to find an existing test that can be used to verify my fix, or run the existing tests to ensure I didn't break anything. If there are already tests for this scenario, I should run them. If not, I should run the existing tests to verify general functionality.
Let me search for existing tests that test tool deduplication or Anthropic web_search tool conflict.
I know there are test files like
config.anthropic.spec.tsandinitialize.test.ts. Let me check if there's already a test for this issue, or run the existing tests to see if my change breaks anything.Let me first search for tests that might be affected by my change.
- Assistant
- Assistant
Now, let's verify that the existing tests for the Anthropic endpoint configuration pass. Run the spec tests from earlier:
- Assistant
I need to run the existing tests to ensure my changes don't break anything. I'll run the npn run test:packages:api command.
bashnpm run test:packages:apiBut first, let me check if I'm in the right directory. I'm in /LibreChat root. The script says "test:packages:api": "cd packages/api && npm run test:ci". So I should run that.
Actually, let me run the tests to verify.
bashnpm run test:packages:api - Assistant
I see a lot of failing tests but they seem to be related to missing modules (
@librechat/data-schemas,librechat-data-provider) rather than my changes. These appear to be pre-existing environment issues, not caused by my fix.Let me check specifically the tests that are relevant to the initialize functionality. The key test file is
src/agents/__tests__/initialize.test.tswhich tests the initialize function. Let me look at that test file to see if it already has tests for the scenario I'm fixing. - Assistant
Let me understand the exact issue better. I need to also check how the tools from
options.toolsare shaped when the endpoint is Anthropic. Let me look at thegetLLMConfigreturn for Anthropic: - Assistant
I see the same syntax error repeated many times. The issue is clear - there's a missing comma after the endpoint dictionary definition, but the code that references
endpointis also where the problem is. The actual problem is at line 15 whereendpointis referenced:typescriptendpoint: string;Wait, that's the interface definition. The issue is actually at line 15, where
endpointis referenced in the interface definition.Let me check line 15 directly:
- Assistant
Let me focus. The issue is clear from my analysis:
- In
initialize.tsat line 846, when bothstructuredTools(agent tools) andoptions.tools(endpoint-specific tools like Anthropic's nativeweb_search) exist, they get concatenated. - This causes duplicate
web_searchtool names when both LibreChat's web_search and Anthropic's native web_search are enabled. - My fix at lines 846-855 filters out endpoint tools that have the same name as agent tools for Anthropic.
The test failures are all pre-existing environment issues (missing modules like
@librechat/data-schemasandlibrechat-data-provider), not related to my changes. These are all module resolution failures, not test assertion failures.Let me check the diff of my change to verify it's correct.
- In
- Assistant
Now let me create a temporary test to verify my fix works correctly by testing the tool deduplication logic.
- Assistant
Let me use bash_terminal with proper bash commands instead of create.
- Assistant
Good, the tests pass. Let me clean up the test file and review my changes one more time.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.