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 what we're dealing with. Let's first check the current directory and see what's there.
- Assistant
Let me understand the issue. The problem is about Anthropic rejecting duplicate
web_searchtool names. When both LibreChat web search and Anthropic-native web search are enabled, we need to expose only oneweb_searchtool and prefer LibreChat's definition. Let me explore the codebase more to understand the structure. - Assistant
Let me search for Anthropic-related code and web_search related code.
- Assistant
Let me look at the key files related to Anthropic tools and web_search:
- Assistant
Let me focus on the Anthropic endpoint and tool handling code.
- Assistant
Now let me look at the agents load.ts and related files to see where tools are assembled:
- Assistant
Let me look at the agents/added.ts and related files to understand how tools are added for Anthropic.
- Assistant
Let me look at the tools-related files and the Anthropic search tool to understand the issue better.
- Assistant
Let me look at the Anthropic tools handling in the @librechat/agents package.
- Assistant
Now let me look at the agents tools spec to understand how tools are being assembled for Anthropic:
- Assistant
Let me look for the Anthropic agent initialization and tool handling, especially where native web search tools are combined with LibreChat web search tools:
- Assistant
Let me look at the handlers.spec.ts and handlers files to understand the web_search tool handling:
- Assistant
Let me look at the
packages/api/src/app/checks.tsfile, which might be where the tools are assembled, and also search for where tools are prepared for the Anthropic endpoint: - Assistant
Now let me look at how tools are assembled and sent to Anthropic. Let me check the tools.ts file and the handlers:
- Assistant
Let me look at these files to understand the tool handling.
- Assistant
Now let me understand where the tools are configured and passed to Anthropic. Let me search for the Anthropic endpoint's tool handling in the agents initialization:
- Assistant
Let me search for the Anthropic-specific tools handling. The issue is about duplicate
web_searchtool names when both LibreChat web search and Anthropic-native web search are enabled. Let me look at the Anthropic search tool definition and how tools are assembled for the Anthropic endpoint. - Assistant
Let me look at the Anthropic search schema to understand the tool definitions better.
- Assistant
Now I understand the issue. Let me look at how the Anthropic endpoint assembles tools:
- Assistant
Let me now look at how the Anthropic client combines the tools from
getLLMConfigwith LibreChat's web_search tool. Let me find the code that callsgetLLMConfigand uses the result: - Assistant
Let me look at the initialize.ts file to understand how tools are combined.
- Assistant
Now let me look at how tools are assembled in the agents initialization flow:
- Assistant
Let me look at where tools from
getLLMConfigand the LibreChat web_search tool are combined: - Assistant
Let me look at how tools are assembled in the
@librechat/agentspackage, especially for Anthropic: - Assistant
Now I'm getting a clearer picture. The issue is about Anthropic rejecting duplicate web_search tool names. When both LibreChat web search and Anthropic-native web search are enabled, there can be duplicate
web_searchtools - one from the Anthropic native tool (type: 'web_search_20250305') and one from LibreChat's own web_search tool definition.Let me look at how these tools are assembled together for the Anthropic endpoint. Let me check the OpenAI config.anthropic.spec.ts and config.ts files since that's where Anthropic configuration is used:
- Assistant
Now let me look at how tools are combined at the agent level. Let me search for where tools are sent to the Anthropic API and where duplicates could occur:
- Assistant
Let me look at the full config.ts to understand how the tools are used.
- Assistant
Now let me understand the flow by looking at how tools are assembled in the agents package and how they get sent to Anthropic. Let me check the key files:
- Assistant
Let me look at the Anthropic LLM spec in the agents package to understand how tools are handled.
- Assistant
Let me look at the agents run initialization to understand how tools are assembled:
- Assistant
Let me look at the run.ts file to understand how tools are assembled.
- Assistant
Let me look at how the
toolsfromgetLLMConfigare used in the agent initialization, and where LibreChat's web_search tools get combined with Anthropic's native tools: - Assistant
Let me look at
getProviderConfigand how the tools are assembled: - Assistant
Now let me look at how tools are combined in the agent initialization. Let me search for the
initializeAgentfunction: - Assistant
Let me look at the initialize.ts file to understand how tools are assembled.
- Assistant
Now I need to understand the key section where tools are combined. Let me look more closely at the part of initialize.ts that merges tools:
- Assistant
Let me see the context around this code:
- Assistant
Now I can see the issue clearly. When both LibreChat web search and Anthropic native web search are enabled:
options.toolsfromgetLLMConfigcontains[{ type: 'web_search_20250305', name: 'web_search' }](Anthropic's native web search server tool)structuredToolsfromloadToolscontains LibreChat'sweb_searchtool
On line 846, these are concatenated:
tools = structuredTools.concat(options.tools as GenericTool[])This results in two tools named
web_searchbeing sent to Anthropic's API, which rejects the request due to duplicate tool names.Now let me also check the
toolDefinitionspath (event-driven mode): - Assistant
Let me look at how the Anthropic model in the agents package binds tools to understand the full picture:
- Assistant
Let me search for how tools are bound in the Anthropic model.
- Assistant
Let me search for the spot in the agents package where the Anthropic tools are bound and duplicates could be created:
- Assistant
Let me look at the tools.ts in the anthropic utils
- Assistant
Let me look at the main Anthropic LLM index.ts in the agents package to understand how tools are processed:
- Assistant
Let me now look at the
formatStructuredToolToAnthropicmethod to see how tools are formatted for Anthropic: - Assistant
Let me search for the
formatStructuredToolToAnthropicmethod definition: - Assistant
Let me look at
bindToolsmethod in the ChatAnthropic class: - Assistant
Let me look at how the tools are actually sent to Anthropic in the
@langchain/anthropicpackage since that's what@librechat/agentsuses: - Assistant
Let me look at the
formatStructuredToolToAnthropicmethod in the@langchain/anthropicpackage to understand how tools are formatted: - Assistant
Now I have a clear picture of the issue. Let me trace through the flow:
getLLMConfigreturns{ tools: [{ type: 'web_search_20250305', name: 'web_search' }], llmConfig: ... }when Anthropic native web search is enabledinitializeAgentcallsgetOptionswhich callsinitializeAnthropicwhich callsgetLLMConfig- The tools are merged in
initializeAgent:tools = structuredTools.concat(options.tools as GenericTool[]); - When both LibreChat's web_search tool is in
structuredToolsand Anthropic's native web_search is inoptions.tools, they both get concatenated, creating duplicateweb_searchnames.
The fix needs to be in
initialize.tswhere tools are merged. When the provider is Anthropic, we should filter out the Anthropic-nativeweb_searchtool fromoptions.toolsif a LibreChatweb_searchalready exists instructuredToolsortoolDefinitions.Let me look at the relevant section again:
- Assistant
Let me look at the existing tests for initialize.ts:
- Assistant
Let me also check the Anthropic llm.spec.ts test for web_search specifically:
- Assistant
Now let me look at the handlers.spec.ts for the duplicate web_search context:
- Assistant
Now I have a clear picture of the issue. Let me implement the fix. The problem is in
initialize.tswhere tools are merged. When both LibreChat's web_search tool and Anthropic's native web_search are present, they get concatenated, creating duplicate tool names that Anthropic rejects.Let me write a reproduce script first to confirm the issue:
- Assistant
Let me run the reproduce script to confirm the issue.
- Assistant
Let me look at the actual test setup to understand how to run tests:
- Assistant
Let me look at the
Toolsconstant to understand what theweb_searchtool name is: - Assistant
Now I have a clear understanding of the issue. Let me look at the key area where the issue occurs. When Anthropic is the provider, and both LibreChat's
web_searchtool and Anthropic's native web search (web_search_20250305) are enabled, they get concatenated creating two tools with the nameweb_search. Let me look at the exact code that needs to be modified: - Assistant
Let me look at how
structuredToolsis populated: - Assistant
Let me look at line 681 for context on where
structuredToolscomes from: - Assistant
Now let me check the
toolDefinitionspath as well: - Assistant
Now I have a clear understanding. Let me implement the fix. The issue is in
packages/api/src/agents/initialize.tsat the line where tools are concatenated for Anthropic. When both LibreChat'sweb_searchtool and Anthropic's nativeweb_searchtool exist, we need to prefer LibreChat's definition.Let me now implement the fix:
- Assistant
Let me navigate to the exact line where tools are merged:
- Assistant
Now I understand the issue clearly. The fix needs to be in the
else ifbranch at line 839-847. When the provider is Anthropic and bothstructuredTools(LibreChat'sweb_search) andoptions.tools(Anthropic's nativeweb_search) are present, the concatenation creates duplicateweb_searchtool names.Let me implement the fix:
- Assistant
Now let me also check if there's a similar issue in the OpenAI config path for custom endpoints using Anthropic:
- Assistant
Now let me also check how
toolDefinitionsinteract with this. In event-driven mode,structuredToolsmight be empty buttoolDefinitionscould still have web_search: - Assistant
Wait, there's another case to consider. When
structuredToolsis empty buttoolDefinitionshas web_search (event-driven mode), the current code would settools = options.tools(Anthropic native web_search) andtoolDefinitionswould have LibreChat's web_search. Both get passed to the Run, potentially creating duplicates. Let me update the fix to handle this case too: - Assistant
I see there's a duplicate
hasAgentToolsline. Let me fix that: - Assistant
Good. Now let me verify the full modified section looks correct:
- Assistant
The code looks good. Now let me also check if the
getLLMConfigfor the Anthropic endpoint via the OpenAI config path also needs similar treatment: - Assistant
Now let me run the existing tests for the initialize.ts file to make sure I haven't broken anything:
- Assistant
There's a parse error. Let me check the file for syntax issues:
- Assistant
The error is in the test file itself, not in my changes. Let me check if this is a pre-existing issue:
- Assistant
Good - this is a pre-existing issue with the test suite, not caused by my changes. Let me restore my changes:
- Assistant
Let me run the other relevant test files:
- Assistant
The tests seem to have a pre-existing issue with TypeScript compilation. Let me check if this is a babel/Jest configuration issue.
- Assistant
Let me try running the tests from within the packages/api directory:
- Assistant
Good, tests pass from within the packages/api directory. Let me also run the Anthropic llm tests:
- Assistant
The tests seem to have a module resolution issue. Let me check how to properly run tests in this project.
- Assistant
Let me check the package.json to find the right way to run tests.
- Assistant
Let me check for jest configuration:
- Assistant
Let me check the jest config for the api package:
- Assistant
I see. The tests use
babel-jestfor TypeScript compilation. The test failure might be a pre-existing babel configuration issue. Let me try running a simpler test first to verify the test environment works: - Assistant
The jest config only maps
~/but notlibrechat-data-provider. Let me check if there are other configs that might be needed. - Assistant
Let me look at how the CI runs the tests:
- Assistant
Let me check the package.json for the api package
- Assistant
The
initialize.test.tshas a pre-existing issue with babel parsing TypeScript destructuring in jest.mock(). Let me check if the tests pass for the key test files we care about: - Assistant
It seems like the jest config for
packages/apidoesn't havelibrechat-data-providerin its module paths. Let me check howtools.spec.tsmanages to work but others don't. - Assistant
Let me try running the tests from the root:
- Assistant
The tests seem to have a pre-existing issue with module resolution. Let me check if packages need to be built first:
- Assistant
The packages haven't been built yet. Let me build them first:
- Assistant
Let me build the data-schemas package as well.
- Assistant
Let me check the workspace structure:
- Assistant
Let me try building with turbo:
- Assistant
There's a TypeScript type error in my code. Let me fix it:
- Assistant
I need to fix the TypeScript type error. The issue is that
(t as { name?: string }).namecan beundefined, butSet.has()expects astring. - Assistant
Now let me rebuild and check if the TypeScript error is fixed:
- Assistant
The build passes without TypeScript errors. Now let me try running the tests with the built modules:
- Assistant
Good, the tools.spec.ts tests pass. Now let me try running the Anthropic llm.spec.ts:
- Assistant
All 90 tests pass. Let me also run the OpenAI config tests for Anthropic compatibility:
- Assistant
All tests pass. Let me also run the other test files related to the issue:
- Assistant
All existing tests pass. Let me also run the agents load.spec.ts test:
- Assistant
All tests pass! Let me also run the app/checks tests:
- Assistant
All tests pass! Let me also run some broader tests to make sure nothing is broken:
- Assistant
All tests pass. Now let me verify my changes by looking at the diff:
- Assistant
The diff looks clean and minimal. Let me now verify the complete logic works correctly by creating a simple test script:
- Assistant
All 6 test cases pass. The fix correctly handles all scenarios. Let me clean up and do a final review:
- Assistant
Let me do a final review of the complete diff:
- Assistant
I see there are some unrelated changes in package-lock.json and package.json. Let me revert those:
- Assistant
Let me verify only my changes remain:
- Assistant
Only my changes to
initialize.tsremain. Let me verify the final diff: - Assistant
Let me run the key test suites one more time to confirm everything passes:
- Assistant
All 196 tests pass across 5 test suites. Let me verify the build also passes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.