diff --git a/src/ai/providers/ai_gateway/protocol/_shared.py b/src/ai/providers/ai_gateway/protocol/_shared.py index 1b6f88c6..a6474ddd 100644 --- a/src/ai/providers/ai_gateway/protocol/_shared.py +++ b/src/ai/providers/ai_gateway/protocol/_shared.py @@ -388,7 +388,7 @@ def request_options( body["parallelToolCalls"] = tool_calling.parallel_tool_calls tool_choice = tool_calling.tool_choice if isinstance(tool_choice, models.ToolChoiceMode): - body["toolChoice"] = tool_choice.value + body["toolChoice"] = {"type": tool_choice.value} elif isinstance(tool_choice, models.ToolRef): body["toolChoice"] = { "type": "tool", @@ -398,7 +398,7 @@ def request_options( body["activeTools"] = sorted( str(tool) for tool in tool_choice.tools ) - body["toolChoice"] = tool_choice.mode.value + body["toolChoice"] = {"type": tool_choice.mode.value} # provider service if value.provider_service is not None: diff --git a/tests/providers/ai_gateway/test_stream.py b/tests/providers/ai_gateway/test_stream.py index 7af163e7..6381bb7f 100644 --- a/tests/providers/ai_gateway/test_stream.py +++ b/tests/providers/ai_gateway/test_stream.py @@ -27,6 +27,7 @@ from ai.providers.ai_gateway import ( GatewayParams, GatewayV3Protocol, + GatewayV4Protocol, ProviderTimeoutsParams, ) from ai.types import events, messages @@ -350,6 +351,68 @@ def handler(req: httpx.Request) -> httpx.Response: class TestRequest: + @pytest.mark.parametrize("protocol", [GatewayV3Protocol, GatewayV4Protocol]) + @pytest.mark.parametrize( + ("choice", "expected"), + [ + (None, {}), + (ai.ToolChoiceMode.AUTO, {"toolChoice": {"type": "auto"}}), + (ai.ToolChoiceMode.NONE, {"toolChoice": {"type": "none"}}), + (ai.ToolChoiceMode.REQUIRED, {"toolChoice": {"type": "required"}}), + ( + ai.ToolRef("a"), + {"toolChoice": {"type": "tool", "toolName": "a"}}, + ), + ( + ai.ToolSelection(["b", "a"], mode=ai.ToolChoiceMode.AUTO), + {"activeTools": ["a", "b"], "toolChoice": {"type": "auto"}}, + ), + ( + ai.ToolSelection(["b", "a"], mode=ai.ToolChoiceMode.NONE), + {"activeTools": ["a", "b"], "toolChoice": {"type": "none"}}, + ), + ( + ai.ToolSelection(["b", "a"], mode=ai.ToolChoiceMode.REQUIRED), + {"activeTools": ["a", "b"], "toolChoice": {"type": "required"}}, + ), + ], + ) + async def test_tool_choice( + self, + protocol: type[GatewayV3Protocol | GatewayV4Protocol], + choice: ai.ToolChoiceMode | ai.ToolRef | ai.ToolSelection | None, + expected: dict[str, Any], + ) -> None: + """Tool choice is always an object in the v3 and v4 specs.""" + captured_body: dict[str, Any] = {} + + def handler(req: httpx.Request) -> httpx.Response: + captured_body.update(json.loads(req.content)) + return httpx.Response( + 200, + text=sse( + {"type": "finish", "finishReason": "stop", "usage": {}} + ), + ) + + model = mock_model(httpx.MockTransport(handler)).with_protocol( + protocol() + ) + request_params = ai.InferenceRequestParams( + tool_calling=( + ai.ToolCallingParams(tool_choice=choice) + if choice is not None + else None + ) + ) + await _collect(model, [user_msg("Hi")], params=request_params) + + assert { + key: captured_body[key] + for key in ("toolChoice", "activeTools") + if key in captured_body + } == expected + async def test_protocol_headers(self) -> None: captured: dict[str, str] = {}