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..e30f07f9 100644 --- a/tests/providers/ai_gateway/test_stream.py +++ b/tests/providers/ai_gateway/test_stream.py @@ -16,6 +16,7 @@ from __future__ import annotations import json +import socket from typing import Any, cast import httpx2 as httpx @@ -27,9 +28,10 @@ from ai.providers.ai_gateway import ( GatewayParams, GatewayV3Protocol, + GatewayV4Protocol, ProviderTimeoutsParams, ) -from ai.types import events, messages +from ai.types import events, messages, tools from .conftest import mock_client, mock_model, sse, user_msg @@ -350,6 +352,143 @@ def handler(req: httpx.Request) -> httpx.Response: class TestRequest: + @pytest.mark.parametrize("version", [3, 4]) + @pytest.mark.parametrize( + "choice,expected", + [ + (ai.ToolChoiceMode.AUTO, {"type": "auto"}), + (ai.ToolChoiceMode.NONE, {"type": "none"}), + (ai.ToolChoiceMode.REQUIRED, {"type": "required"}), + (ai.ToolRef("lookup"), {"type": "tool", "toolName": "lookup"}), + (None, None), + ( + ai.ToolSelection( + ["summarize", "lookup"], mode=ai.ToolChoiceMode.AUTO + ), + {"type": "auto"}, + ), + ( + ai.ToolSelection( + ["summarize", "lookup"], mode=ai.ToolChoiceMode.NONE + ), + {"type": "none"}, + ), + ( + ai.ToolSelection( + ["summarize", "lookup"], mode=ai.ToolChoiceMode.REQUIRED + ), + {"type": "required"}, + ), + ], + ids=[ + "auto", + "none", + "required", + "named", + "omitted", + "subset-auto", + "subset-none", + "subset-required", + ], + ) + async def test_tool_choice_serialization( + self, + monkeypatch: pytest.MonkeyPatch, + version: int, + choice: ai.ToolChoiceMode | ai.ToolRef | ai.ToolSelection | None, + expected: dict[str, str] | None, + ) -> None: + # Independent protocol definitions; exact objects reject legacy strings: + # https://github.com/vercel/ai/blob/3bea041f28e65f371e640d2c70c302776a46387f/packages/provider/src/language-model/v3/language-model-v3-tool-choice.ts + # https://github.com/vercel/ai/blob/3bea041f28e65f371e640d2c70c302776a46387f/packages/provider/src/language-model/v4/language-model-v4-tool-choice.ts + def reject_network(*args: Any, **kwargs: Any) -> Any: + pytest.fail("unexpected real network operation") + + monkeypatch.setattr(socket, "getaddrinfo", reject_network) + monkeypatch.setattr(socket.socket, "connect", reject_network) + monkeypatch.setattr(socket.socket, "connect_ex", reject_network) + captured: list[dict[str, Any]] = [] + + def handler(request: httpx.Request) -> httpx.Response: + assert request.method == "POST" + assert str(request.url) == ( + f"https://gw.test/v{version}/ai/language-model" + ) + assert request.headers["authorization"] == "Bearer test-key" + assert request.headers[ + "ai-language-model-specification-version" + ] == str(version) + assert not captured, "unexpected additional request" + captured.append(json.loads(request.content)) + return httpx.Response( + 200, + text=sse( + { + "type": "finish", + "finishReason": {"unified": "stop", "raw": "stop"}, + "usage": { + "inputTokens": { + "total": 1, + "noCache": 1, + "cacheRead": 0, + "cacheWrite": 0, + }, + "outputTokens": { + "total": 0, + "text": 0, + "reasoning": 0, + }, + }, + } + ), + ) + + model = mock_model(httpx.MockTransport(handler)).with_protocol( + GatewayV3Protocol() if version == 3 else GatewayV4Protocol() + ) + declared_tools = [ + tools.Tool( + kind="function", + name=name, + spec=tools.ToolSpec( + params={"type": "object", "properties": {}} + ), + ) + for name in ["lookup", "summarize"] + ] + params = ai.InferenceRequestParams( + tool_calling=( + ai.ToolCallingParams(tool_choice=choice) + if choice is not None + else None + ) + ) + async with ai.stream( + model, + [user_msg("Look up a value.")], + tools=declared_tools, + params=params, + ) as stream: + async for _ in stream: + pass + + assert len(captured) == 1 + body = captured[0] + assert "responseFormat" not in body + assert [tool["name"] for tool in body["tools"]] == [ + "lookup", + "summarize", + ] + if expected is None: + assert "toolChoice" not in body + else: + assert body["toolChoice"] == expected + if isinstance(choice, ai.ToolSelection): + # Preserve existing SDK output, not a claim of Gateway support. + assert body["activeTools"] == ["lookup", "summarize"] + else: + assert "activeTools" not in body + async def test_protocol_headers(self) -> None: captured: dict[str, str] = {}