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.
Assistant
Now let me look at how the tools are passed from LibreChat's web_search to the agent call, to understand the full picture:
Assistant
Let me look at how tools are loaded for the agent and how Anthropic gets them:
Assistant
Let me look at how the Anthropic LLM receives tools and where the issue is. Look at where the tools (from LibreChat) and the Anthropic-native tools both end up at the LLM:
Assistant
Now let me look at how LibreChat's web_search tool gets loaded into the agent. Let me check the ToolService.js:
Assistant
Now let me look at how Anthropic gets the tools. Looking at
loadToolDefinitionsfor what the Anthropic endpoint receives:Assistant
Now let me understand the issue better. The
web_searchtool name appears in two places:- As a LibreChat native tool (
Tools.web_search= 'web_search') - As an Anthropic-native tool defined in
llm.ts({ type: 'web_search_20250305', name: 'web_search' })
If both are enabled, two
web_searchtools get sent to Anthropic, which rejects them.Let me look at how the LibreChat web_search tool gets added to the tool definitions sent to the LLM:
- As a LibreChat native tool (
Assistant
Now I understand the flow. Let me look at where the toolDefinitions get sent to the LLM. Looking at
options.toolsfor Anthropic:Assistant
Now I need to understand how tools end up in
options.tools(which is fromresultreturned byinitializeAnthropic, whereresult.toolscontains the Anthropic-nativeweb_searchtool):Assistant
So
options.toolsfrominitializeAnthropiccomes fromresult.toolswhich is the Anthropic-native tools array. Let me trace the issue.Looking at
initialize.tsagain:let tools: GenericTool[] = options.tools?.length ? (options.tools as GenericTool[]) : (structuredTools ?? []); if ( (agent.provider === Providers.OPENAI || agent.provider === Providers.AZURE || agent.provider === Providers.ANTHROPIC) && options.tools?.length && structuredTools?.length ) { tools = structuredTools.concat(options.tools as GenericTool[]); }So when the provider is Anthropic, both
structuredTools(LibreChat tools likeweb_search) ANDoptions.tools(Anthropic-native tools) get concatenated. This is where the duplicateweb_searchis created!Now let me check how the tools are finally sent to the LLM:
Assistant
Let me look for how the LLM gets bound tools:
Assistant
Now I understand the issue. Let me trace the flow more carefully:
initializeAnthropicreturnsresultwhich hastoolsarray containing[{ type: 'web_search_20250305', name: 'web_search' }](Anthropic-native)loadToolsResulthastools: structuredToolscontaining LibreChat native tools (could includeweb_search)- In
initialize.ts:let tools: GenericTool[] = options.tools?.length ? (options.tools as GenericTool[]) : (structuredTools ?? []); if ( (agent.provider === Providers.ANTHROPIC) && options.tools?.length && structuredTools?.length ) { tools = structuredTools.concat(options.tools as GenericTool[]); }
So when both
structuredToolscontains aweb_searchtool (LibreChat's) ANDoptions.toolscontains the Anthropic-nativeweb_search, we get a duplicate nameweb_searchin the tools array sent to Anthropic.Now, the issue says:
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.So we need to deduplicate
web_searchfrom Anthropic-native tools when the LibreChatweb_searchis present.Let me look at the test file to understand the expected behavior:
Assistant
Now let me look at the broader agent flow to understand the actual merging:
Assistant
Now I see the place where the issue exists. Let me check the agent.initialize test:
Assistant
Now let me search for what other tests exist related to the ANTHROPIC provider path:
Assistant
Let me look at the place where
web_searchis registered withTools.web_search:Assistant
OK, so
Tools.web_search = 'web_search'. Now let me also confirm where in the initialize.ts the agent's tools list containsweb_search:Assistant
Now let me trace
WebSearchToolDefinition:Assistant
Confirmed - both have the name
web_search. Now let me find where in initialize.ts the fix should happen. The most appropriate place is the concatenation logic that handles Anthropic specifically.Now let me think about the proper fix. The issue says:
When LibreChat web search and Anthropic-native web search are both enabled, expose one
web_searchtool and prefer LibreChat's definition.So we need to:
- Detect when the LibreChat-native
web_searchtool is instructuredToolsAND Anthropic-nativeweb_searchtool is inoptions.tools - When both are present, prefer LibreChat's definition (which is in
structuredTools) - The
options.tools(Anthropic-native) should be filtered to remove theweb_searchtool before concatenation
Let me also consider where else this might happen. Let me check the OPENAI/Azure branch too:
- Detect when the LibreChat-native
Assistant
OK so the same
web_searchissue exists for OpenAI as well. But the issue description specifically says "Anthropic", so let me focus on Anthropic first. Let me also look at what other providers might have similar issues:Actually, the issue is:
Do not affect unrelated tools or other providers.
So the fix should be specific to Anthropic. The deduplication needs to happen only when both LibreChat's
web_searchand Anthropic-nativeweb_searchare present.Let me check the existing tests to understand what's expected:
Assistant
Let me look at the tests for initialize:
Assistant
Now let me create a script to reproduce the issue:
Assistant
Now I have enough understanding. Let me write a quick reproduce script. Let me trace what would happen:
- The agent has
web_searchin itstoolslist loadToolsResultis called, and through the registry, this becomesstructuredToolswith[{ name: 'web_search', description: ..., parameters: ... }]options.tools(frominitializeAnthropic) contains[{ type: 'web_search_20250305', name: 'web_search' }]- The concatenation in
initialize.tsproduces:[{ name: 'web_search', ... }, { type: 'web_search_20250305', name: 'web_search' }] - Anthropic rejects this duplicate.
The fix needs to be in
initialize.ts:- When the agent's provider is Anthropic
- And
structuredToolsalready contains a tool namedweb_search - And
options.toolscontains a tool withname === 'web_search'(ortype === 'web_search_20250305') - Then filter out the Anthropic-native one from
options.toolsbefore concatenation
Let me also check the issue more carefully. The
structuredToolsareGenericToolinstances. Let me check what their structure is:- The agent has
Assistant
Now let me also check the structure of
structuredToolsmore carefully. Looking atloadToolsinloadAgentTools(legacy path) it returns objects that have anameproperty:Assistant
Let me look at the legacy loadTools to understand how structuredTools gets formed:
Assistant
Let me look at
loadTools:Assistant
Now I need to look at the structuredTools shape:
Assistant
So
createSearchToolreturns a tool withname: 'web_search'. Now let me look at what the structuredTools array contains:Assistant
Now let me look at the definitions path (event-driven) to see what tools have what names:
Assistant
Now I need to look at how
options.toolsgets bound to the LLM. Let me also look at how this is sent to Anthropic API:Assistant
Let me search for where the actual LLM instance is created from
llmConfig:Assistant
Let me look at the librechat agents package's
createAgent(which is used in run.ts):Assistant
So
initializeModelcallsmodel.bindTools(tools). This is where the tools end up bound. So the fix needs to deduplicate before this happens.Let me look at where this is called:
Assistant
Now let me look at how the model is created:
Assistant
OK, so
toolsForBindingcomes fromagentContext.getToolsForBinding()which gets the merged tools. Let me look at that:Assistant
Now let me see
graphTools:Assistant
Now let me look at where
graphToolsgets populated in ourinitialize.ts:Assistant
So
toolsarray gets passed astoolsto the initializedAgent, which becomesagentContext.toolsin the Graph, and thengetToolsForBinding()includes them in the binding to the LLM.Now I need to fix the issue. The best place is in the initialize.ts where we determine the final
toolsarray. Let me check the spec for tests:Assistant
Now I have enough context. Let me think about the fix.
The cleanest place to fix this is in
initialize.tswhere the concatenation happens. When both LibreChat'sweb_searchand Anthropic's nativeweb_searchare present, we should:- Keep LibreChat's definition (the structuredTools entry)
- Remove the Anthropic-native
web_searchtool fromoptions.tools
Let me also check what the
options.toolsstructure looks like for Anthropic:Looking at
llm.ts:const tools = []; if (enableWebSearch) { tools.push({ type: 'web_search_20250305', name: 'web_search', }); ... }And what
structuredToolslooks like - it's aGenericTool[]where each has a.nameproperty.Let me now check how tools are filtered in the legacy vs new path:
Assistant
OK. Now let me look at the existing tests for initialize to understand what's being verified:
Assistant
Let me look at the broader pattern of how
web_searchissue should be handled. Looking at OpenAI as comparison, OpenAI also has the same issue (it has{ type: 'web_search' }for native web search):Looking at the code:
} else if (enableWebSearch) { /** Standard OpenAI web search uses tools API */ llmConfig.useResponsesApi = true; tools.push({ type: 'web_search' }); }But OpenAI's
web_searchtool type doesn't have anameproperty — it's just{ type: 'web_search' }. So the issue may also exist for OpenAI but the issue is specifically about Anthropic.Let me make sure by re-reading the issue:
Anthropic rejects duplicate web_search tool names When LibreChat web search and Anthropic-native web search are both enabled, expose one web_search tool and prefer LibreChat's definition. Do not affect unrelated tools or other providers.
OK so the fix is specifically for Anthropic. The check should look at the tool name
web_searchspecifically.Now let me think about the approach. I think the best fix is to:
- In
initialize.ts, when the provider is Anthropic and we're about to concatoptions.toolswithstructuredTools:- Check if
structuredToolsalready has a tool namedweb_search - If yes, filter out Anthropic-native
web_searchtool (which hastype: 'web_search_20250305'andname: 'web_search') fromoptions.toolsbefore concatenation
- Check if
Let me write a test script first to reproduce:
- In
Assistant
Now let me write a script to reproduce the issue:
Assistant
Good, I've reproduced the issue. Now let me look at the actual fix. I'll modify
initialize.tsto add the deduplication logic specifically for Anthropic when both LibreChat'sweb_searchand Anthropic'sweb_searchare present:Assistant
Now let me look at the existing structure and write the fix:
Assistant
Now I'll implement the fix. The key insight is that we need to:
- When the agent is on Anthropic
- AND
structuredToolsalready has a tool namedweb_search - AND
options.toolshas the Anthropic-native web_search (which hastype: 'web_search_20250305') - Filter out the Anthropic-native one before concatenation
Let me look at the structure carefully. The
options.toolsarray elements havetypefield (e.g.,web_search_20250305). ThestructuredToolselements havenamefield.Let me write the fix:
Assistant
Now let me make the edit:
Assistant
Now I will implement the fix in
initialize.ts:Assistant
Wait, there's a subtle issue. The original code does
tools = structuredTools.concat(options.tools as GenericTool[])only whenoptions.tools?.length && structuredTools?.length. But my refactoredproviderToolsonly filters Anthropic's web_search, and otherwise matches the original behavior. Let me re-check carefully.Actually, looking at this more carefully, I think we have a simpler approach: just filter the options.tools before concatenation, only when needed for Anthropic.
Let me refactor:
Assistant
Hmm, that's getting complicated. Let me think again. The simplest approach is to:
- Compute
providerToolsfromoptions.tools, but for Anthropic when LibreChat web_search is present, filter out the Anthropic-native web_search entry. - Then use the existing logic for concatenation.
Let me simplify:
- Compute
Assistant
Wait, I notice there's a subtle bug in my refactor. If
providerToolsbecomes empty after filtering, the existing concatenation won't run because of theproviderTools.length > 0check, andtoolsremains asoptions.tools. But we need it to remain asstructuredTools.Actually, looking more carefully, when the existing logic runs:
- It uses
options.tools?.length(which is unchanged), butproviderTools?.length(which may be 0 after filter) - If
providerTools?.length > 0is false, thentoolsstays asoptions.tools(the value set in the let assignment), which is WRONG.
I need to be more careful. Let me reconsider:
- It uses
Assistant
Now let me think again. The existing concatenation logic does:
tools = structuredTools.concat(options.tools as GenericTool[])
For Anthropic with the filter applied,
options.toolsmay have had a web_search removed. But the existing logic usesoptions.tools?.length(which is the ORIGINAL length, unchanged) as a guard.If
options.tools.length > 0originally butproviderTools.length === 0after filtering, we'd concat an empty array with structuredTools, which is fine.So my approach is fine. Let me think about the edge case:
options.tools = [{ type: 'web_search_20250305', name: 'web_search' }](only Anthropic native)- After filter:
providerTools = [] - The concatenation:
structuredTools.concat([]) = structuredTools
That should work. Let me also check the case when
options.tools?.length > 0is the only condition for going through the concat branch:Looking at the code:
if ( (agent.provider === Providers.ANTHROPIC) && options.tools?.length && structuredTools?.length ) { tools = structuredTools.concat(options.tools as GenericTool[]); }OK, so the concat only happens when both arrays are non-empty. This is correct for our case.
Now let me think about the case where:
options.tools = [](no Anthropic native tools)structuredTools = [{ name: 'web_search', ... }](LibreChat web_search)
In this case,
options.tools?.lengthis 0 (falsy), so we don't concat.toolsstays asstructuredToolsfrom the let assignment. Good.What about:
options.tools = [{ type: 'web_search_20250305', name: 'web_search' }](Anthropic native)structuredTools = [{ name: 'web_search', ... }](LibreChat web_search)- After filter:
providerTools = [] - The concat:
structuredTools.concat([]) = structuredTools(LibreChat's web_search only)
What about:
options.tools = [{ type: 'web_search_20250305', name: 'web_search' }, { type: 'something_else', ... }]structuredTools = [{ name: 'web_search', ... }](LibreChat web_search)- After filter:
providerTools = [{ type: 'something_else', ... }] - The concat:
structuredTools.concat([{ type: 'something_else', ... }])
What about edge case where
options.toolshas multipleweb_search_20250305entries (unlikely but possible):- After filter:
providerTools = [](all web_search entries removed) - Concat with empty array:
structuredTools
Now let me actually apply the edit. Let me also make sure the existing test still passes - the case where
options.toolsmay beundefined:Assistant
Let me check the type of
options.tools:Assistant
OK,
tools?: unknown[]- so it's optional and typed asunknown[]. My handling of(options.tools ?? [])will work fine.Now let me apply the edit:
Assistant
Now let me apply the fix:
Assistant
Now let me think about whether this change affects other providers:
- OPENAI:
providerTools = optionsToolsList(no filter), so behavior unchanged - AZURE: Same as OpenAI, unchanged
- GOOGLE/VERTEXAI: The first if branch checks
options.tools?.length(original), still triggers GOOGLE_TOOL_CONFLICT - ANTHROPIC: Filter applies only when LibreChat's web_search is in structuredTools
Let me think about an edge case where
providerToolscould end up as[]after filter butoptions.tools?.length > 0:options.tools = [{ type: 'web_search_20250305', name: 'web_search' }],structuredTools = [{ name: 'web_search', ... }]providerTools = [](filtered)let tools = providerTools.length > 0 ? providerTools : (structuredTools ?? []);→tools = structuredTools✓- Then the ANTHROPIC branch:
options.tools?.length && structuredTools?.length→ both truthy, sotools = structuredTools.concat([])=structuredTools✓
Now let me also test the case where
options.toolsis empty:options.tools = [],structuredTools = [{ name: 'web_search', ... }]providerTools = [](becauseoptionsToolsList.length > 0is false, so we use the original which is empty)let tools = structuredTools(sinceproviderTools.length === 0)- Then ANTHROPIC branch:
options.tools?.lengthis 0 → falsy, so no concat - Result:
tools = structuredTools✓
Now another case:
options.tools = [],structuredTools = []:providerTools = []let tools = (structuredTools ?? []) = []- No branches match (all require some non-empty conditions)
- Result:
tools = []✓
Let me also test:
options.tools = [{ type: 'web_search_20250305', name: 'web_search' }],structuredTools = []:providerTools = optionsToolsList(the filter check requiresstructuredTools?.some(...)which is false)let tools = providerTools = [{ type: 'web_search_20250305', name: 'web_search' }]- Then ANTHROPIC branch:
structuredTools?.lengthis 0 → falsy, so no concat - Result:
tools = options.tools(the Anthropic native one) ✓
And:
options.tools = [],structuredTools = [{ name: 'calculator' }]:providerTools = []let tools = structuredTools- No branches match
- Result:
tools = structuredTools✓
And OpenAI:
options.tools = [{ type: 'web_search' }],structuredTools = [{ name: 'web_search' }]:providerTools = optionsToolsList = [{ type: 'web_search' }](no filter for OpenAI)let tools = providerTools- Then OPENAI branch:
options.tools?.length && structuredTools?.length→ both truthy, sotools = structuredTools.concat(optionsToolsList)=[{ name: 'web_search' }, { type: 'web_search' }] - Result: Both LibreChat and OpenAI-native are present (current behavior preserved)
This is the right behavior - we don't want to change OpenAI behavior since the issue is specific to Anthropic.
Now let me also confirm my understanding of how structuredTools works for Anthropic. Let me look more carefully at the
Tools.web_searchcheck:- OPENAI:
Assistant
Good. Let me now look at the existing tests for initialize to understand the test patterns and check if there's a test that should fail before my fix:
Assistant
Now let me run the existing tests for initialize to make sure my fix doesn't break anything:
Assistant
Let me see how to run these tests from the project root:
Assistant
The data-provider package hasn't been built yet. Let me build it:
Assistant
Now I need to install the package locally. Let me also build the api package deps:
Assistant
Good - it's a symlink to packages/data-provider. Now let me run the tests:
Assistant
All 34 tests pass with my fix. Let me also run other related tests:
Assistant
All 742 tests pass with my fix. Now let me also write a small reproduction script to verify the fix works:
Assistant
I see two issues:
- Test 2: When Anthropic native web_search is present but no LibreChat web_search, the
options.toolsshould be used (not filtered). But my logic givescalculatoronly. Let me trace this... Wait, the test 2 hasoptions.tools = [{ type: 'web_search_20250305' }]andstructuredTools = [{ name: 'calculator' }]. Since LibreChat doesn't have web_search, the filter doesn't apply. ThenproviderTools = optionsToolsList=[{ type: 'web_search_20250305' }]. Sotoolsstarts withproviderTools = [{ type: 'web_search_20250305' }]. Then ANTHROPIC branch checksoptions.tools?.length && structuredTools?.length- both true, sotools = structuredTools.concat(providerTools)=[{ name: 'calculator' }, { type: 'web_search_20250305' }]. So the test result should be 2, not 1. Let me check my test script logic more carefully... Oh, I see, in my test I usedtool.namewhich doesn't exist for the Anthropic-native one. Let me fix the test:
- Test 2: When Anthropic native web_search is present but no LibreChat web_search, the
Assistant
Hmm, my test script has bugs - my fake function
computeFinalToolsisn't quite the same as the original. Let me trace through. Wait, my test reproduction has a bug - I hadproviderTools.length > 0 ? providerTools : (structuredTools ?? [])but the original code had:let tools: GenericTool[] = options.tools?.length ? (options.tools as GenericTool[]) : (structuredTools ?? []);So in the original, when
options.tools?.length > 0,toolsis set tooptions.tools. In my version,toolsis set toproviderToolswhich is the filtered version. So if filter removes everything (providerTools becomes empty), thentools = structuredTools. That changes behavior from the original.Let me re-look at the original logic:
Original:
let tools: GenericTool[] = options.tools?.length ? (options.tools as GenericTool[]) : (structuredTools ?? []); if ( ... ) { tools = structuredTools.concat(options.tools as GenericTool[]); }In the original:
- If
options.toolshas 1 element andstructuredToolshas 1 element, the final tools arestructuredTools.concat(options.tools)= 2 elements - If
options.toolshas 1 element andstructuredToolsis empty, the final tools areoptions.tools= 1 element
In my modified version:
- If
options.toolshas 1 element (Anthropic native web_search) andstructuredToolshas LibreChat web_search:providerTools = [](filtered)tools = structuredTools(LibreChat web_search)- Then ANTHROPIC branch:
options.tools?.length && structuredTools?.length→ both truthy →tools = structuredTools.concat(providerTools)=structuredTools.concat([])=structuredTools✓
- If
options.toolshas 1 element (Anthropic native web_search) andstructuredToolshas calculator:providerTools = optionsToolsList(no filter)tools = providerTools- Then ANTHROPIC branch:
options.tools?.length && structuredTools?.length→ both truthy →tools = structuredTools.concat(providerTools)✓
The behavior is correct. My test had an error. Let me re-test:
- If
Assistant
I see several issues in my test logic, but more importantly there's a real issue with the original code AND my fix:
Let me trace through Case 2 carefully:
options.tools = [{ type: 'web_search_20250305', name: 'web_search' }](length = 1)structuredTools = [{ name: 'calculator', description: 'calc' }](length = 1)
In the original code:
let tools = options.tools?.length ? options.tools : structuredTools; // tools = options.tools = [{ type: 'web_search_20250305', name: 'web_search' }]Then in the ANTHROPIC branch:
} else if (options.tools?.length && structuredTools?.length) { // both truthy, so: tools = structuredTools.concat(options.tools); // tools = [{name: 'calculator'}, {type: 'web_search_20250305', name: 'web_search'}] }So in the ORIGINAL, the result is 2 tools. But my test got 1. Let me check my fake logic again:
let tools = providerTools.length > 0 ? providerTools : (structuredTools ?? []);If
providerToolshas 1 element (the Anthropic native web_search), thentools = providerTools=[{ type: 'web_search_20250305' }].Then the ANTHROPIC branch:
} else if (options.tools?.length && structuredTools?.length) { tools = structuredTools.concat(providerTools); // = [{name: 'calculator'}].concat([{type: 'web_search_20250305'}]) // = [{name: 'calculator'}, {type: 'web_search_20250305'}] }So my test result should be 2 tools. But I got 1. Let me check my test again... oh I see, my test had
tools = providerTools.length > 0 ? providerTools : (structuredTools ?? []);then in the Anthropic branch:} else if ( (agent.provider === 'openai' || agent.provider === 'azure' || agent.provider === 'anthropic') && optionsTools?.length && structuredTools?.length ) { tools = structuredTools.concat(providerTools); }Wait, I need to check
optionsTools?.lengthnotproviderTools.length. Let me re-read my code...Actually my fake function in repro_fix3.js DOES use
optionsTools?.lengthnotproviderTools.length. So the Anthropic branch should run. But the test result shows only 1 tool. Let me debug:Assistant
I see the issue!
optionsToolsList = (optionsTools ?? [])returnsoptionsToolsas is, which is the full object{ tools: [...] }not just the array! My test code was wrong.Let me fix the test:
Assistant
Now the test logic is correct. Let me re-run the comprehensive tests:
Assistant
All tests pass. Now let me verify that the actual code in
initialize.tsmatches my test logic. Let me re-look at the code I changed:Assistant
Good. Now let me run the actual tests with the TypeScript code to make sure everything is working:
Assistant
All 34 tests still pass. Let me also run more agent tests to make sure nothing else broke:
Assistant
All 1051 tests pass. Now let me also test other related test files:
Assistant
All tests pass. Now let me write a temporary reproduction script that simulates the exact code path:
Assistant
No dist. The Jest test was already sufficient. Let me also check that the change doesn't affect edge cases. Let me re-read the final code:
Assistant
Let me look at the diff to make sure the change is clean:
Assistant
The diff looks clean. Now let me think about edge cases more carefully:
-
What if
options.toolscontains an entry that is not strictly{ type: 'web_search_20250305', name: 'web_search' }but has type with different casing or whitespace? - We filter by exact type match, so any other tool type is unaffected. -
What if
structuredToolscontains a tool withname: 'web_search'but is NOT actually LibreChat's web_search (e.g., from an MCP server)? - The filter would still apply. But this is consistent with the issue requirement: "When LibreChat web search and Anthropic-native web search are both enabled, expose oneweb_searchtool and prefer LibreChat's definition." Actually, MCP tools don't typically use the nameweb_search, so this is unlikely. And the issue specifically says "LibreChat's web search" — if MCP were to expose a tool with the same name, dropping the Anthropic-native is still safe. -
What about the
structuredToolscontent for Anthropic? - Looking at the code,structuredToolscomes fromloadToolsResult.toolswhich is the full tool instance. Its.namewould be'web_search'for LibreChat's web_search tool. -
What if there are multiple
web_search_20250305entries inoptions.tools? - The filter removes all of them, which is fine (better safe than sorry).
Let me also check the original behavior when
options.toolsis not an array but an empty array:The original code uses
options.tools?.length. Withoptions.tools = undefined,options.tools?.lengthisundefined. The let assignment becomesstructuredTools. My new code uses(options.tools ?? []) as GenericTool[]. Withoptions.tools = undefined, this is[].optionsToolsList.length > 0is false, soproviderTools = optionsToolsList = []. Thenlet tools = (structuredTools ?? []). Good, this matches.- What about when
options.toolsis not undefined but not an array? - The type saysunknown[], so we should assume it's always an array. My code uses.filter()which is safe for arrays.
Let me also check what happens with
as GenericTool[]cast on(options.tools ?? []):Looking at the original code, it uses
(options.tools as GenericTool[]). My code does(options.tools ?? []) as GenericTool[]. The cast might fail at runtime ifoptions.toolsis not actually an array. Let me verify by looking at the type:-
Assistant
It's
unknown[], so it could be any array of unknown values. The cast toGenericTool[]is unsafe in general but it matches the existing code pattern.Now let me also write a test that verifies the actual code change. Let me look at the existing test pattern:
Assistant
Now let me run a broader test suite to confirm nothing breaks:
Assistant
Hmm, many test failures. Let me check what's happening:
Assistant
Wait, these tests are running from
packages/apidirectory but the path ispackages/api/src/agents/.... Let me check what's happening:Assistant
Oh, my pwd changed to /LibreChat somehow. Let me cd to packages/api:
Assistant
Some failures. Let me check if they're related to my change:
Assistant
All failures are related to integration tests (Redis, cache, etc.) - those require external services and are not related to my change. Let me verify this by checking the specific test files to confirm none of them are related to my change:
Assistant
All failures are in integration test files (
.cache_integration.,.stream_integration.) which require external services like Redis. These are unrelated to my fix.Let me check that the tests I care about (agents, tools, anthropic, openai) all pass:
Assistant
1129 tests pass, 0 failures. My fix is correct.
Now let me look at the final code one more time to make sure it's clean:
Assistant
Wait, I just noticed something. Looking at the issue more carefully:
When LibreChat web search and Anthropic-native web search are both enabled, expose one web_search tool and prefer LibreChat's definition.
Looking at my fix, the
let tools = providerTools.length > 0 ? providerTools : (structuredTools ?? []);change might cause a subtle issue. Let me re-examine.The original code:
let tools: GenericTool[] = options.tools?.length ? (options.tools as GenericTool[]) : (structuredTools ?? []);This means:
- If
options.toolsis non-empty,tools = options.tools - Otherwise,
tools = structuredTools
My new code:
let tools: GenericTool[] = providerTools.length > 0 ? providerTools : (structuredTools ?? []);If filter applies and removes all entries from
options.tools,providerTools.length === 0, sotools = structuredTools. Good. Otherwise (no filter or partial filter),providerTools.length > 0, sotools = providerTools. Good.Hmm wait, this is actually different from the original behavior. In the original, if
options.tools?.length > 0, thentools = options.toolsregardless ofstructuredTools. In my new code,providerToolscould be empty even ifoptions.tools?.length > 0(when filter applies and removes everything). In that case,tools = structuredToolsinstead ofoptions.tools.This is the correct behavior because:
- When filter applies, all entries in
options.toolswere Anthropic-nativeweb_searchtools - We want to drop them and keep
structuredTools
Let me trace once more for edge cases:
options.tools = [{ type: 'web_search_20250305', name: 'web_search' }],structuredTools = [{ name: 'web_search' }]providerTools = [](filter removes web_search)tools = structuredTools(sinceproviderTools.length === 0)- ANTHROPIC branch:
options.tools?.length && structuredTools?.length→ both truthy →tools = structuredTools.concat([])=structuredTools✓
Now an edge case where
structuredTools = []:options.tools = [{ type: 'web_search_20250305', name: 'web_search' }],structuredTools = []- The filter check
structuredTools?.some(...)is false (since structuredTools is empty) - So
providerTools = optionsToolsList = [{ type: 'web_search_20250305' }] tools = providerTools = [{ type: 'web_search_20250305' }]- ANTHROPIC branch:
structuredTools?.lengthis 0 → falsy → no concat - Result:
tools = [{ type: 'web_search_20250305' }](Anthropic native) ✓
Now another edge case:
options.tools = [],structuredTools = []:optionsToolsList = []providerTools = [](filter check requiresoptionsToolsList.length > 0)tools = (structuredTools ?? []) = []- No branches match
- Result:
tools = []✓
Now:
options.tools = undefined,structuredTools = []:optionsToolsList = [](since options.tools is undefined)providerTools = []tools = []- Result:
tools = []✓
OK my logic is sound. Let me also test the case where the agent's provider is anthropic and structuredTools is undefined:
Wait, looking at the code,
structuredToolscomes fromloadToolsResult.tools, which is destructured with a default of[]. So it's always an array. Good.Now let me think about whether I should also fix the issue in the agent.js (librechat code) or elsewhere. Let me look at how this is wired up:
- If
Assistant
Good -
loadToolsis the function (passed as a parameter). Looking at how it's wired inapi/server/services/ToolService.js:Assistant
Good. The wiring is correct -
loadAgentToolsis called frominitialize.jswhich loads LibreChat's tools. My fix is in the right place.Now let me also consider: are there other places where this issue might arise?
Assistant
Good, only one place. Now let me also look at
summarizationto ensure the same issue isn't elsewhere:Assistant
No relevant issue. Now let me also test the new test for my fix:
Assistant
All scenarios pass. Let me also check one more edge case: when
options.toolshas multiple entries of which some areweb_search_20250305:Assistant
All edge cases pass. The fix handles all the scenarios correctly:
- Multiple
web_search_20250305entries: all filtered out, LC kept - Empty structuredTools + Anthropic native: native kept
- Mixed tools with LC web_search: only
web_search_20250305filtered, other tools kept - Vertex AI works (uses Anthropic format)
- Empty/undefined structuredTools: native kept as is
Let me also clean up the temporary scripts:
- Multiple
Assistant
Let me look at the final diff:
Assistant
I notice that there are also some package-lock.json and package.json changes. These are not from my changes. They seem to have been changed by
npm install. Let me restore them:Assistant
Only my change remains. Let me re-verify by running the tests once more:
Assistant
All 34 tests still pass.
Let me also check that the linting passes:
Assistant
ESLint passes. Let me also check TypeScript compilation:
Assistant
TypeScript compiles cleanly. Let me also do a final review of the diff:
Assistant
The diff looks clean and focused. Let me do a final sanity check by re-reading the issue:
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.✅ When both are enabled: only LibreChat's
web_searchis in the final tools array (filtered out Anthropic-nativeweb_search_20250305).✅ Unrelated tools: The filter only removes entries with
type === 'web_search_20250305', all other tools are preserved.✅ Other providers: The filter is gated on
agent.provider === Providers.ANTHROPIC. For OpenAI, Azure, Google, Vertex AI — behavior is unchanged.Now let me address the checklist:
Checklist
- If an error was reported in the issue description, I have successfully reproduced it.
- Yes, I reproduced the duplicate
web_searchissue in/tmp/repro/repro.jsand then verified the fix works in subsequent test scripts.
- Yes, I reproduced the duplicate
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- Yes, the fix deduplicates the
web_searchtool when both LibreChat's native and Anthropic's native are enabled.
- Yes, the fix deduplicates the
- I have thought about edge cases and made sure my fix handles them as well.
- Yes, I tested:
- Both enabled (the main issue)
- Only Anthropic native
- Only LibreChat
- Empty structuredTools
- Multiple Anthropic native
web_searchentries - Mixed tools (Anthropic native + LibreChat + other tools)
- Vertex AI
- OpenAI (unchanged behavior)
- Yes, I tested:
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
- Yes, 1129 tests pass in
src/agents/,src/tools/,src/endpoints/anthropic/,src/endpoints/openai/test directories.
- Yes, 1129 tests pass in
Let me also check that the issue description states:
Interface
No new public interfaces are introduced.
✅ My fix is purely internal logic changes - no new public types, classes, or functions are introduced.
- If an error was reported in the issue description, I have successfully reproduced it.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →