Skip to content

Commit 71cf48c

Browse files
Python: Honor allow_multiple_tool_calls in AnthropicClient when tool_choice is unset (#8980)
* Python: Honor allow_multiple_tool_calls in AnthropicClient when tool_choice is unset * Add regression tests for Anthropic parallel tool use without tool_choice * Assert tool_choice fields individually, like the neighbouring tests
1 parent ea32b6d commit 71cf48c

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

‎python/packages/anthropic/agent_framework_anthropic/_chat_client.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,14 +1162,18 @@ def _prepare_tools_for_anthropic(
11621162
request_state.tool_name_aliases = {}
11631163

11641164
# Process tool choice
1165+
allow_multiple = options.get("allow_multiple_tool_calls")
11651166
if options.get("tool_choice") is None:
1167+
# Anthropic only exposes parallel tool use through tool_choice, so carry the setting
1168+
# on its default "auto" mode (e.g. after the function loop resets tool_choice="required").
1169+
if allow_multiple is not None and result.get("tools"):
1170+
result["tool_choice"] = {"type": "auto", "disable_parallel_tool_use": not allow_multiple}
11661171
return result or None
11671172
tool_mode = validate_tool_mode(options.get("tool_choice"))
11681173
if tool_mode is None:
11691174
return result or None
11701175
if "allowed_tools" in tool_mode:
11711176
logger.warning("allowed_tools is not supported by Anthropic; the setting will be ignored")
1172-
allow_multiple = options.get("allow_multiple_tool_calls")
11731177
match tool_mode.get("mode"):
11741178
case "auto":
11751179
tool_choice: dict[str, Any] = {"type": "auto"}

‎python/packages/anthropic/tests/test_anthropic_client.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,82 @@ async def test_prepare_options_with_tool_choice_auto(
14171417
assert "allow_multiple_tool_calls" not in run_options
14181418

14191419

1420+
async def test_prepare_options_allow_multiple_tool_calls_without_tool_choice(
1421+
mock_anthropic_client: MagicMock,
1422+
) -> None:
1423+
"""allow_multiple_tool_calls=False must disable parallel tool use even when tool_choice is unset."""
1424+
client = create_test_anthropic_client(mock_anthropic_client)
1425+
1426+
@tool(approval_mode="never_require")
1427+
def get_weather(city: str) -> str:
1428+
"""Get the weather."""
1429+
return f"sunny in {city}"
1430+
1431+
messages = [Message(role="user", contents=["Weather in Paris and Rome?"])]
1432+
run_options = client._prepare_options(messages, {"tools": [get_weather], "allow_multiple_tool_calls": False})
1433+
1434+
assert run_options["tool_choice"]["type"] == "auto"
1435+
assert run_options["tool_choice"]["disable_parallel_tool_use"] is True
1436+
1437+
1438+
async def test_function_loop_keeps_parallel_tool_use_disabled_after_required_tool_choice(
1439+
mock_anthropic_client: MagicMock,
1440+
) -> None:
1441+
"""After the loop resets tool_choice='required', later requests must still disable parallel tool use."""
1442+
client = create_test_anthropic_client(mock_anthropic_client)
1443+
1444+
@tool(approval_mode="never_require")
1445+
def get_weather(city: str) -> str:
1446+
"""Get the weather."""
1447+
return f"sunny in {city}"
1448+
1449+
usage = BetaUsage(input_tokens=10, output_tokens=5)
1450+
mock_anthropic_client.beta.messages.create.side_effect = [
1451+
BetaMessage(
1452+
id="msg_1",
1453+
type="message",
1454+
role="assistant",
1455+
model="claude-test",
1456+
content=[BetaToolUseBlock(type="tool_use", id="toolu_1", name="get_weather", input={"city": "Paris"})],
1457+
stop_reason="tool_use",
1458+
usage=usage,
1459+
),
1460+
BetaMessage(
1461+
id="msg_2",
1462+
type="message",
1463+
role="assistant",
1464+
model="claude-test",
1465+
content=[BetaToolUseBlock(type="tool_use", id="toolu_2", name="get_weather", input={"city": "Rome"})],
1466+
stop_reason="tool_use",
1467+
usage=usage,
1468+
),
1469+
BetaMessage(
1470+
id="msg_3",
1471+
type="message",
1472+
role="assistant",
1473+
model="claude-test",
1474+
content=[BetaTextBlock(type="text", text="Sunny in both.")],
1475+
stop_reason="end_turn",
1476+
usage=usage,
1477+
),
1478+
]
1479+
1480+
await client.get_response(
1481+
[Message(role="user", contents=["Weather in Paris and Rome?"])],
1482+
options={"tools": [get_weather], "tool_choice": "required", "allow_multiple_tool_calls": False},
1483+
)
1484+
1485+
calls = mock_anthropic_client.beta.messages.create.call_args_list
1486+
assert len(calls) == 3
1487+
assert calls[0].kwargs["tool_choice"]["type"] == "any"
1488+
assert calls[0].kwargs["tool_choice"]["disable_parallel_tool_use"] is True
1489+
for call in calls[1:]:
1490+
tool_choice = call.kwargs.get("tool_choice")
1491+
assert tool_choice is not None
1492+
assert tool_choice["type"] == "auto"
1493+
assert tool_choice["disable_parallel_tool_use"] is True
1494+
1495+
14201496
async def test_prepare_options_with_tool_choice_required(
14211497
mock_anthropic_client: MagicMock,
14221498
) -> None:

0 commit comments

Comments
 (0)