Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/any_llm/providers/anthropic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def _convert_tool_choice(params: CompletionParams) -> dict[str, Any]:
parallel_tool_calls = params.parallel_tool_calls
if parallel_tool_calls is None:
parallel_tool_calls = True
tool_choice = params.tool_choice or "any"
tool_choice = params.tool_choice or "auto"
if tool_choice == "required":
tool_choice = "any"
elif isinstance(tool_choice, dict):
Expand Down Expand Up @@ -480,7 +480,7 @@ def _convert_params(params: CompletionParams, **kwargs: Any) -> dict[str, Any]:
if params.tools:
params.tools = _convert_tool_spec(params.tools)

if params.tool_choice or params.parallel_tool_calls:
if params.tool_choice is not None or params.parallel_tool_calls is not None:
params.tool_choice = _convert_tool_choice(params)

if params.reasoning_effort is None or params.reasoning_effort == "none":
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/providers/test_anthropic_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,30 @@ async def test_completion_with_tool_choice_and_parallel_tool_calls(parallel_tool
)


@pytest.mark.parametrize("parallel_tool_calls", [True, False])
@pytest.mark.asyncio
async def test_completion_with_parallel_tool_calls_only(parallel_tool_calls: bool) -> None:
"""Test that parallel_tool_calls without tool_choice maps to "auto" instead of forcing tool use."""
api_key = "test-api-key"
model = "model-id"
messages = [{"role": "user", "content": "Hello"}]

with mock_anthropic_provider() as mock_anthropic:
provider = AnthropicProvider(api_key=api_key)
await provider._acompletion(
CompletionParams(model_id=model, messages=messages, parallel_tool_calls=parallel_tool_calls),
)

expected_kwargs = {"tool_choice": {"type": "auto", "disable_parallel_tool_use": not parallel_tool_calls}}

mock_anthropic.return_value.messages.create.assert_called_once_with(
model=model,
messages=[{"role": "user", "content": "Hello"}],
**expected_kwargs,
max_tokens=DEFAULT_MAX_TOKENS,
)


@pytest.mark.asyncio
async def test_completion_inside_agent_loop(agent_loop_messages: list[dict[str, Any]]) -> None:
api_key = "test-api-key"
Expand Down