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 the codebase and locate relevant files related to Anthropic and web search tools. Let's first get an overview.
- Assistant
Let me explore the repo structure more carefully, focusing on Anthropic-related files and web search tools.
- Assistant
Let me focus on the Anthropic endpoint files and search for web search related code.
- Assistant
Let me look at the Anthropic-specific files, particularly the LLM and config files that handle tools.
- Assistant
Let me look at the anthropic llm.ts file to understand the current implementation.
- Assistant
Now let me understand the issue. The problem is: when both LibreChat web search and Anthropic-native web search are enabled, there are duplicate
web_searchtool names, which Anthropic rejects. The fix should expose oneweb_searchtool and prefer LibreChat's definition.Let me look at the tools handling to understand how LibreChat's web search tool is defined.
- Assistant
Let me search for where web_search tools are being added or constructed, especially in the context of Anthropic endpoints and agents.
- Assistant
Let me understand the issue better. The problem is that when both LibreChat web search and Anthropic's native web search tool are enabled, there will be two tools with the name
web_search- one from LibreChat and one from Anthropic's nativeweb_search_20250305tool. Anthropic rejects duplicate tool names.Looking at the Anthropic LLM config, in
getLLMConfig(), whenenableWebSearchis true, it adds:{ type: 'web_search_20250305', name: 'web_search', }So the issue is that when the agents system also includes a LibreChat
web_searchtool, there would be a duplicate.Let me look at how tools are collected and sent to the Anthropic API.
- Assistant
Let me look at how the tools are sent to the Anthropic API, specifically how the LLM tools and the anthropic native tools are merged.
- Assistant
Now let me look at how these tools are used in the context of agents or the actual request. Let me look at how the Anthropic client uses these tools.
- Assistant
Let me look at the config.anthropic.spec.ts file to understand how tools flow through:
- Assistant
Now let me look at how tools are collected when agents use them, especially for Anthropic. Let me check the agents/tools.spec.ts to understand the flow:
- Assistant
Let me search for where tools get sent to Anthropic and how the tools from
getLLMConfigare combined with other tools: - Assistant
Let me look at the initialize.ts file to see how the tools from getLLMConfig are used:
- Assistant
Let me look at how
initializeAnthropicresult is used, and how tools from LibreChat are combined with the native Anthropic tools: - Assistant
Let me look at how the tools from getLLMConfig are handled in the config:
- Assistant
Now I need to find where tools from getLLMConfig are combined with LibreChat tools in the Anthropic path. Let me search for where tools are assembled for the agent:
- Assistant
Let me look at how tools are assembled in the agents to find where duplicate names could occur:
- Assistant
Let me look at how the LLM is created and tools are passed to it:
- Assistant
Let me look at the agents/run.ts file to understand how tools from getOpenAIConfig are used with the agent:
- Assistant
Let me look at how tools are passed to the Anthropic LLM from the agent. Let me search for where
llmConfig.toolsorgetLLMConfigresult's tools are used: - Assistant
Let me look at how tools from the Anthropic getLLMConfig result are used. Let me search for where the tools result is consumed:
- Assistant
Let me look at how tools are passed from the config to the actual Anthropic model invocation. Let me search for where tools from the config result are combined:
- Assistant
Let me look at the agents openai service where tools might be combined:
- Assistant
Let me look at how the Anthropic LLM and its tools are actually set up. Let me check the
@librechat/agentspackage: - Assistant
Let me search for where the Anthropic tools (from getLLMConfig) are combined with LibreChat tools (like web_search). Let me look at how tools are combined in the initialization process:
- Assistant
Let me look at the
@librechat/agentspackage to see how tools are passed: - Assistant
Let me check if the
@librechat/agentspackage is local or an npm package. - Assistant
Let me look at how tools are managed when the Anthropic client creates tools. Let me look at the agents package source or the agents initialization code:
- Assistant
Let me check the agents package for how tools are combined with native Anthropic tools:
- Assistant
Let me look at the
@librechat/agentsAnthropic LLM to see how it handles the tools array: - Assistant
Let me look at the Anthropic LLM in the agents package to see how it handles tools:
- Assistant
Let me look at the Anthropic search tool to understand its definition:
- Assistant
Let me look at the web search tool definition in the agents package:
- Assistant
Now I understand the issue better. Let me look at how tools from the LLM config and LibreChat tools are combined in the agent initialization:
- Assistant
Let me look at how the tools from
getLLMConfigare combined with LibreChat tools in the agent initialization. Let me find where both sets of tools are combined: - Assistant
Let me look at where the tools from
getLLMConfigget used in the agent context. Let me check@librechat/agentsfor how tools are passed to the Anthropic LLM: - Assistant
Let me search for where the result from
initializeAnthropicis used, particularly itstoolsproperty: - Assistant
Let me look at how the initializers are used:
- Assistant
Now I need to understand how the tools from getLLMConfig (which includes the Anthropic native
web_searchtool) are combined with the LibreChat web search tool. Let me search for where the tools are combined for Anthropic: - Assistant
Let me look more broadly at how tools from
getLLMConfigare combined with LibreChat tools for the Anthropic agent. - Assistant
Let me search for where the Anthropic endpoint tools are assembled for the LLM call:
- Assistant
Let me look at the AgentContext.ts file to understand how tools are combined, especially native tools.
- Assistant
Let me look at the filterToolsForBinding function to understand how tools are filtered:
- Assistant
Let me look at the full context around line 1368 to understand how filterToolsForBinding works and how native tools are handled.
- Assistant
Let me search for where the tools from
getLLMConfigare combined with agent tools (fromload.ts): - Assistant
Let me look at how tools are handled when constructing the Anthropic LLM. Let me search for where tools from config are passed to the agent:
- Assistant
Let me look at how tools from the Anthropic config (from
getLLMConfig) are combined with the LibreChat tools. Let me search for where tools from the config result are actually used: - Assistant
Let me look at how the tools from getOpenAIConfig are passed to the agent creation. Let me search for where getOpenAIConfig is called in the agent run:
- Assistant
Let me look at the main run function to see where tools from getOpenAIConfig are used:
- Assistant
Let me look at how the Anthropic model in
@librechat/agentshandles tools, specifically looking at how the native Anthropic tools are passed: - Assistant
Let me look at
formatStructuredToolToAnthropicto understand how tools are formatted for Anthropic: - Assistant
Let me search for the
formatStructuredToolToAnthropicmethod: - Assistant
Let me look at how tools from
getLLMConfigare combined with LibreChat tools for the agent. Let me check where these tools come together: - Assistant
Let me search for where tools are combined in the agents package. Let me look at the main agent initialization flow:
- Assistant
Let me look at the AgentContext.ts file to understand how tools are combined, specifically the area where native tools might be handled:
- Assistant
Let me trace the flow of tools through the system more carefully. Let me look at where
getLLMConfigresult'stoolsare combined with other tools: - Assistant
Let me search more broadly for where tools from getOpenAIConfig are used and combined with agent tools:
- Assistant
Let me understand the full flow. Let me look at how tools from the config are combined with agent tools:
- Assistant
Let me look at how tools from the config are combined with agent tools in the initialization. Let me check the types to understand the result structure:
- Assistant
Let me understand the type definitions better.
- Assistant
Let me look at the LLMConfigResult type to understand what tools are returned.
- Assistant
Now let me look at how the tools from the config are combined with LibreChat tools. Let me look at the agents initialization flow:
- Assistant
Let me look at how
graphToolsare set, particularly for Anthropic native tools: - Assistant
Let me look at how
graphToolsare set in the constructor or initialization: - Assistant
Let me search for where the tools from config are passed to the agent/LLM. Let me look at the
added.tswhich seems to add tools: - Assistant
Now I need to understand the full flow. Let me look at how tools from
getLLMConfigand LibreChat tools are combined. Let me check the@librechat/agentspackage's handling of tools: - Assistant
Let me look at the main agent initialization path where tools from getOpenAIConfig are combined with agent tools:
- Assistant
Let me look at the initialize.ts file to see how tools are combined:
- Assistant
Now I understand the issue. Let me look at the exact code where tools are combined in initialize.ts:
- Assistant
Let me look at the exact code around lines 829-846:
- Assistant
Now I understand the flow. Let me look at how the tools are structured:
- Assistant
Now I understand the issue fully. The problem is in
initialize.tswhere tools are concatenated - when both LibreChat's web_search tool and Anthropic's native web_search tool are present, they create duplicate names. Let me look at how the tools are structured to understand how to fix this: - Assistant
Let me look at the full flow to understand how tools get combined in the agents path. Let me trace through the code:
- Assistant
Now I understand the issue. The problem is in
initialize.tsat line 846 where tools are concatenated. When both LibreChat web_search tool and Anthropic native web_search tool are present, they get concatenated resulting in duplicate tool names.Let me now look at the existing tests to understand the expected behavior:
- Assistant
Let me look at the test file for initialize to understand the expected behavior:
- Assistant
Now I understand the issue fully. Let me check what
GenericToollooks like and how to detect duplicates: - Assistant
Let me look at the types to understand GenericTool and GraphTools.
- Assistant
Let me look at what
BindToolsInputlooks like for the native Anthropic tools: - Assistant
Now let me understand the exact issue. Let me look at the test spec for the Anthropic llm to see what the expected behavior is:
- Assistant
Now I understand the full picture. Let me write a script to reproduce the issue first:
- Assistant
Now I understand the issue completely. The fix needs to be in
initialize.tswhere tools are combined. Let me now look at the exact location and make the fix: - Assistant
Now I understand the issue. The problem is in
initialize.tsat line 846 where tools are concatenated without checking for duplicates. When both LibreChat's web_search tool and Anthropic's native web_search tool are present, they get concatenated creating duplicate tool names.The fix should filter out tools from
options.toolsthat have the same name as tools instructuredTools. This way, LibreChat's definition is preferred.Let me make the fix:
- Assistant
Now I need to fix line 846 where tools are concatenated. The fix should filter out tools from
options.toolsthat have the same name as tools instructuredTools. This way, LibreChat's definition is preferred. - Assistant
The type casting is a bit ugly. Let me simplify this. Let me look at the GenericTool type more closely to understand its properties:
- Assistant
Let me check what
StructuredToolInterfacelooks like: - Assistant
Let me simplify the implementation. The
GenericTooltype has anameproperty from eitherStructuredToolInterfaceorRunnableToolLike. Let me simplify the code: - Assistant
Now let me run the existing tests to make sure the change doesn't break anything. Let me find the relevant test files:
- Assistant
Let me check how to run tests in this project.
- Assistant
Let me check for test scripts in the packages/api directory.
- Assistant
Now let me run the tests for the initialize test file.
- Assistant
Let me try the correct flag:
- Assistant
It looks like the test suite can't find the module. Let me check the package setup.
- Assistant
Let me check if the test suite has the proper configuration to run the initialize tests. The error is about module resolution. Let me look at the jest config for the API package:
- Assistant
Let me find the jest config for the api package.
- Assistant
Let me look at the jest config:
- Assistant
The tests can't find
librechat-data-provider. This is likely a monorepo issue where the test needs to be run from the root or with proper module resolution. Let me check if there are existing ways to run these tests successfully. - Assistant
Let me check if the jest config in the api package needs a proper module mapper for the
librechat-data-providerpackage. - Assistant
The test suite failed to run because it can't find
librechat-data-provider. This is a monorepo setup issue. Let me check if we need to build the packages first or if there's a specific way to run tests. - Assistant
Let me check if the module symlink is set up correctly:
- Assistant
Let me check if the node_modules has a proper symlink or if it's the actual package:
- Assistant
OK, so it's a symlink. The issue might be with babel module resolution. Let me try to run the test with proper module resolution. Let me look at how other tests are run.
- Assistant
Let me try building the data-provider package first:
- Assistant
Let me check if there's a dist directory or if we need to build the packages:
- Assistant
Let me try to build the packages first:
- Assistant
Let me wait for the build to complete:
- Assistant
Good, the build succeeded. Now let me try running the tests again:
- Assistant
All 34 tests passed. Now let me also run the Anthropic-specific tests:
- Assistant
All Anthropic tests pass. Now let me also run the agents tools spec tests to make sure nothing is broken:
- Assistant
All tests pass. Let me also run the
tools/definitions.spec.tstest: - Assistant
Let me also run the web toolkit tests:
- Assistant
Let me also run the config.anthropic.spec.ts tests to make sure nothing is broken:
- Assistant
All tests pass. Let me also run the OpenAI llm tests to make sure nothing is broken:
- Assistant
All tests pass. Let me also run the OpenAI config tests:
- Assistant
All tests pass. Let me also run the Google llm tests to make sure I haven't affected other providers:
- Assistant
All tests pass. Let me also run the agents handler tests:
- Assistant
All tests pass. Now let me also run the test that tests Google tool conflict, since that's the most closely related to our change:
- Assistant
Good, the GOOGLE_TOOL_CONFLICT test still passes. Now let me verify my change looks correct by reviewing the diff:
- Assistant
The diff shows some unrelated changes to
package-lock.jsonandpackage.jsonfrom the build process. Let me revert those unrelated changes: - Assistant
Good. Now let me verify only the relevant change remains:
- Assistant
Now let me run the tests again to make sure everything still passes after reverting the unrelated changes:
- Assistant
All 34 tests pass. Let me now verify the final diff one more time:
- Assistant
Now let me verify the final state of the fix:
- Assistant
The fix looks clean and correct. Let me now run a comprehensive set of tests to make sure nothing is broken:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.