@@ -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+
14201496async def test_prepare_options_with_tool_choice_required (
14211497 mock_anthropic_client : MagicMock ,
14221498) -> None :
0 commit comments