Skip to content

Commit 6b2e417

Browse files
he-yufengzouyonghe
andauthored
fix(provider): merge custom server tools with registered tools in Anthropic requests (#10437)
* fix(provider): merge custom server tools with registered tools in Anthropic requests * fix(provider): dedupe tool names and tolerate null custom body Address review findings on the custom-server-tools merge: - A custom tool sharing a name with a registered tool (or another custom tool) produced duplicate definitions that the API rejects. Merge by name instead; the custom entry wins, matching the override authority extra_body already had for every other top-level key. - An explicit null custom_extra_body crashed on .get("tools"). Null or non-dict values now normalize to an empty body. * fix(provider): avoid shadowed id in Anthropic streaming tool merge --------- Co-authored-by: zouyonghe <[email protected]>
1 parent a8bb080 commit 6b2e417

2 files changed

Lines changed: 305 additions & 3 deletions

File tree

‎astrbot/core/provider/sources/anthropic_source.py‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import builtins
23
import json
34
from collections.abc import AsyncGenerator
45
from typing import Any, Literal
@@ -524,7 +525,25 @@ async def _query(
524525
payloads.get("tool_choice", "auto")
525526
)
526527

527-
extra_body = self.provider_config.get("custom_extra_body", {})
528+
extra_body = self.provider_config.get("custom_extra_body") or {}
529+
if not isinstance(extra_body, dict):
530+
# An explicit null or malformed custom body carries nothing to merge.
531+
extra_body = {}
532+
# Server-side tools declared in the custom body (e.g. web search on
533+
# Anthropic-compatible endpoints) must coexist with the registered
534+
# function tools: extra_body replaces same-named top-level keys,
535+
# which would silently drop them. Tool definitions get the same
536+
# override semantics: a custom tool replaces a registered tool of
537+
# the same name instead of duplicating it (the API rejects duplicate
538+
# tool names).
539+
custom_tools = extra_body.get("tools")
540+
if isinstance(custom_tools, list) and isinstance(payloads.get("tools"), list):
541+
merged = {}
542+
for tool in [*payloads["tools"], *custom_tools]:
543+
key = tool.get("name") if isinstance(tool, dict) else None
544+
merged[key if key else ("_", id(tool))] = tool
545+
payloads["tools"] = list(merged.values())
546+
extra_body = {k: v for k, v in extra_body.items() if k != "tools"}
528547

529548
if "max_tokens" not in payloads:
530549
payloads["max_tokens"] = 65536
@@ -628,7 +647,20 @@ async def _query_stream(
628647
final_tool_calls = []
629648
id = None
630649
usage = TokenUsage()
631-
extra_body = self.provider_config.get("custom_extra_body", {})
650+
extra_body = self.provider_config.get("custom_extra_body") or {}
651+
if not isinstance(extra_body, dict):
652+
extra_body = {}
653+
# See _query: custom server-side tools must not replace the
654+
# registered function tools, and a name collision overrides
655+
# instead of duplicating.
656+
custom_tools = extra_body.get("tools")
657+
if isinstance(custom_tools, list) and isinstance(payloads.get("tools"), list):
658+
merged = {}
659+
for tool in [*payloads["tools"], *custom_tools]:
660+
key = tool.get("name") if isinstance(tool, dict) else None
661+
merged[key if key else ("_", builtins.id(tool))] = tool
662+
payloads["tools"] = list(merged.values())
663+
extra_body = {k: v for k, v in extra_body.items() if k != "tools"}
632664
reasoning_content = ""
633665
reasoning_signature = ""
634666

‎tests/test_anthropic_source.py‎

Lines changed: 271 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
2+
from types import SimpleNamespace
23

34
import pytest
5+
from anthropic import BadRequestError
46
from anthropic import _base_client as anthropic_base_client
5-
from anthropic.types import MessageDeltaUsage, Usage
7+
from anthropic.types import Message, MessageDeltaUsage, TextBlock, Usage
68

79
from astrbot.core.agent.tool import FunctionTool, ToolSet
810
from astrbot.core.provider.entities import TokenUsage
@@ -87,6 +89,203 @@ def test_anthropic_update_usage_omitted_fields_are_preserved():
8789
assert token_usage.output == 7
8890

8991

92+
@pytest.mark.asyncio
93+
async def test_query_merges_custom_server_tools_with_registered_tools():
94+
provider = ProviderAnthropic(
95+
provider_config={
96+
"id": "anthropic-test",
97+
"type": "anthropic_chat_completion",
98+
"model": "claude-test",
99+
"key": ["test-key"],
100+
"custom_extra_body": {
101+
"tools": [{"type": "web_search_20250305", "name": "web_search"}],
102+
"custom_flag": True,
103+
},
104+
},
105+
provider_settings={},
106+
)
107+
108+
captured: dict[str, object] = {}
109+
110+
class FakeMessages:
111+
async def create(self, **kwargs):
112+
captured.update(kwargs)
113+
return Message(
114+
id="msg_1",
115+
content=[TextBlock(text="ok", type="text")],
116+
model="claude-test",
117+
role="assistant",
118+
stop_reason="end_turn",
119+
stop_sequence=None,
120+
type="message",
121+
usage=Usage(input_tokens=1, output_tokens=1),
122+
)
123+
124+
provider.client = SimpleNamespace(messages=FakeMessages())
125+
126+
tool_set = ToolSet(
127+
[
128+
FunctionTool(
129+
name="get_time",
130+
description="Get the current time.",
131+
parameters={"type": "object", "properties": {}},
132+
)
133+
]
134+
)
135+
136+
await provider._query({"model": "claude-test", "messages": []}, tool_set)
137+
138+
# Server-side tools from the custom body must coexist with the
139+
# registered function tools instead of replacing them.
140+
assert [tool["name"] for tool in captured["tools"]] == ["get_time", "web_search"]
141+
assert "tools" not in captured["extra_body"]
142+
assert captured["extra_body"]["custom_flag"] is True
143+
144+
145+
@pytest.mark.asyncio
146+
async def test_query_custom_tools_without_registered_tools_pass_through():
147+
provider = ProviderAnthropic(
148+
provider_config={
149+
"id": "anthropic-test",
150+
"type": "anthropic_chat_completion",
151+
"model": "claude-test",
152+
"key": ["test-key"],
153+
"custom_extra_body": {
154+
"tools": [{"type": "web_search_20250305", "name": "web_search"}],
155+
},
156+
},
157+
provider_settings={},
158+
)
159+
160+
captured: dict[str, object] = {}
161+
162+
class FakeMessages:
163+
async def create(self, **kwargs):
164+
captured.update(kwargs)
165+
return Message(
166+
id="msg_1",
167+
content=[TextBlock(text="ok", type="text")],
168+
model="claude-test",
169+
role="assistant",
170+
stop_reason="end_turn",
171+
stop_sequence=None,
172+
type="message",
173+
usage=Usage(input_tokens=1, output_tokens=1),
174+
)
175+
176+
provider.client = SimpleNamespace(messages=FakeMessages())
177+
178+
await provider._query({"model": "claude-test", "messages": []}, None)
179+
180+
# No registered tools: the custom body keeps its previous override
181+
# semantics and goes out via extra_body untouched.
182+
assert "tools" not in captured
183+
assert captured["extra_body"]["tools"] == [
184+
{"type": "web_search_20250305", "name": "web_search"}
185+
]
186+
187+
188+
@pytest.mark.asyncio
189+
async def test_query_custom_tool_overrides_registered_tool_with_same_name():
190+
provider = ProviderAnthropic(
191+
provider_config={
192+
"id": "anthropic-test",
193+
"type": "anthropic_chat_completion",
194+
"model": "claude-test",
195+
"key": ["test-key"],
196+
"custom_extra_body": {
197+
"tools": [
198+
{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}
199+
],
200+
},
201+
},
202+
provider_settings={},
203+
)
204+
205+
captured: dict[str, object] = {}
206+
207+
class FakeMessages:
208+
async def create(self, **kwargs):
209+
captured.update(kwargs)
210+
return Message(
211+
id="msg_1",
212+
content=[TextBlock(text="ok", type="text")],
213+
model="claude-test",
214+
role="assistant",
215+
stop_reason="end_turn",
216+
stop_sequence=None,
217+
type="message",
218+
usage=Usage(input_tokens=1, output_tokens=1),
219+
)
220+
221+
provider.client = SimpleNamespace(messages=FakeMessages())
222+
223+
tool_set = ToolSet(
224+
[
225+
FunctionTool(
226+
name="web_search",
227+
description="Client-side web search.",
228+
parameters={"type": "object", "properties": {}},
229+
)
230+
]
231+
)
232+
233+
await provider._query({"model": "claude-test", "messages": []}, tool_set)
234+
235+
# A name collision must not duplicate the tool (the API rejects
236+
# duplicates); the custom body keeps its override authority.
237+
assert captured["tools"] == [
238+
{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}
239+
]
240+
241+
242+
@pytest.mark.asyncio
243+
async def test_query_null_custom_extra_body_does_not_break_request():
244+
provider = ProviderAnthropic(
245+
provider_config={
246+
"id": "anthropic-test",
247+
"type": "anthropic_chat_completion",
248+
"model": "claude-test",
249+
"key": ["test-key"],
250+
"custom_extra_body": None,
251+
},
252+
provider_settings={},
253+
)
254+
255+
captured: dict[str, object] = {}
256+
257+
class FakeMessages:
258+
async def create(self, **kwargs):
259+
captured.update(kwargs)
260+
return Message(
261+
id="msg_1",
262+
content=[TextBlock(text="ok", type="text")],
263+
model="claude-test",
264+
role="assistant",
265+
stop_reason="end_turn",
266+
stop_sequence=None,
267+
type="message",
268+
usage=Usage(input_tokens=1, output_tokens=1),
269+
)
270+
271+
provider.client = SimpleNamespace(messages=FakeMessages())
272+
273+
tool_set = ToolSet(
274+
[
275+
FunctionTool(
276+
name="get_time",
277+
description="Get the current time.",
278+
parameters={"type": "object", "properties": {}},
279+
)
280+
]
281+
)
282+
283+
await provider._query({"model": "claude-test", "messages": []}, tool_set)
284+
285+
assert [tool["name"] for tool in captured["tools"]] == ["get_time"]
286+
assert captured["extra_body"] == {}
287+
288+
90289
def _tool_use_stream(
91290
partial_json_chunks: list[str], start_input: dict | None = None
92291
) -> bytes:
@@ -196,3 +395,74 @@ async def test_anthropic_stream_keeps_tool_call_with_empty_input(
196395
assert response.tools_call_name == ["get_time"]
197396
assert response.tools_call_args == [expected_args]
198397
assert response.tools_call_ids == ["toolu_01"]
398+
399+
400+
@pytest.mark.asyncio
401+
@pytest.mark.parametrize("streaming", [False, True], ids=["standard", "stream"])
402+
@pytest.mark.parametrize(
403+
"custom_tool",
404+
[{}, {"name": ""}, {"name": None}, None, "invalid", 0, False],
405+
ids=["missing-name", "empty-name", "null-name", "null", "string", "zero", "false"],
406+
)
407+
async def test_malformed_custom_tools_reach_api_validation(
408+
monkeypatch, streaming, custom_tool
409+
):
410+
captured = []
411+
412+
def handle(request):
413+
captured.append(json.loads(request.content))
414+
return sdk_httpx.Response(
415+
400,
416+
json={
417+
"type": "error",
418+
"error": {"type": "invalid_request_error", "message": "Invalid tool"},
419+
},
420+
)
421+
422+
monkeypatch.setattr(
423+
ProviderAnthropic,
424+
"_create_http_client",
425+
lambda self, config: sdk_httpx.AsyncClient(
426+
transport=sdk_httpx.MockTransport(handle)
427+
),
428+
)
429+
custom_tools = [custom_tool, {"type": "unnamed"}]
430+
provider = ProviderAnthropic(
431+
{
432+
"id": "test",
433+
"type": "anthropic_chat_completion",
434+
"key": ["test-key"],
435+
"model": "claude-test",
436+
"api_base": "https://offline.invalid",
437+
"custom_extra_body": {"tools": custom_tools},
438+
},
439+
{},
440+
)
441+
tools = ToolSet(
442+
[
443+
FunctionTool(
444+
name="get_time",
445+
description="Offline tool schema.",
446+
parameters={"type": "object", "properties": {}},
447+
)
448+
]
449+
)
450+
payload = {"model": "claude-test", "messages": [{"role": "user", "content": "hi"}]}
451+
try:
452+
for _ in range(2):
453+
with pytest.raises(BadRequestError):
454+
if streaming:
455+
async for _ in provider._query_stream(
456+
payload, tools, request_max_retries=1
457+
):
458+
pass
459+
else:
460+
await provider._query(payload, tools, request_max_retries=1)
461+
finally:
462+
await provider.terminate()
463+
464+
assert len(captured) == 2
465+
assert captured[0] == captured[1]
466+
assert captured[0]["tools"][0]["name"] == "get_time"
467+
assert captured[0]["tools"][1:] == custom_tools
468+
assert provider.provider_config["custom_extra_body"]["tools"] == custom_tools

0 commit comments

Comments
 (0)