From 979dbfc09c6963d7774fcce7557b45c933a37d98 Mon Sep 17 00:00:00 2001 From: Frank Li <53418357+franklbh@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:14:38 -0800 Subject: [PATCH 1/2] fix(gateway): serialize tool choice modes as objects --- .../providers/ai_gateway/protocol/_shared.py | 4 +- tests/providers/ai_gateway/test_stream.py | 141 +++++++++++++++++- 2 files changed, 142 insertions(+), 3 deletions(-) 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] = {} From b77ff4ed8c579f01724e93c9473456542306ed33 Mon Sep 17 00:00:00 2001 From: Andrey Buzin Date: Tue, 17 Nov 2026 09:33:39 -0800 Subject: [PATCH 2/2] Simplify tool choice test Keep the 16 cases. Drop the checks that other tests in this file already cover. --- tests/providers/ai_gateway/test_stream.py | 144 +++++----------------- 1 file changed, 34 insertions(+), 110 deletions(-) diff --git a/tests/providers/ai_gateway/test_stream.py b/tests/providers/ai_gateway/test_stream.py index e30f07f9..6381bb7f 100644 --- a/tests/providers/ai_gateway/test_stream.py +++ b/tests/providers/ai_gateway/test_stream.py @@ -16,7 +16,6 @@ from __future__ import annotations import json -import socket from typing import Any, cast import httpx2 as httpx @@ -31,7 +30,7 @@ GatewayV4Protocol, ProviderTimeoutsParams, ) -from ai.types import events, messages, tools +from ai.types import events, messages from .conftest import mock_client, mock_model, sse, user_msg @@ -352,142 +351,67 @@ def handler(req: httpx.Request) -> httpx.Response: class TestRequest: - @pytest.mark.parametrize("version", [3, 4]) + @pytest.mark.parametrize("protocol", [GatewayV3Protocol, GatewayV4Protocol]) @pytest.mark.parametrize( - "choice,expected", + ("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), + (None, {}), + (ai.ToolChoiceMode.AUTO, {"toolChoice": {"type": "auto"}}), + (ai.ToolChoiceMode.NONE, {"toolChoice": {"type": "none"}}), + (ai.ToolChoiceMode.REQUIRED, {"toolChoice": {"type": "required"}}), ( - ai.ToolSelection( - ["summarize", "lookup"], mode=ai.ToolChoiceMode.AUTO - ), - {"type": "auto"}, + ai.ToolRef("a"), + {"toolChoice": {"type": "tool", "toolName": "a"}}, ), ( - ai.ToolSelection( - ["summarize", "lookup"], mode=ai.ToolChoiceMode.NONE - ), - {"type": "none"}, + ai.ToolSelection(["b", "a"], mode=ai.ToolChoiceMode.AUTO), + {"activeTools": ["a", "b"], "toolChoice": {"type": "auto"}}, ), ( - ai.ToolSelection( - ["summarize", "lookup"], mode=ai.ToolChoiceMode.REQUIRED - ), - {"type": "required"}, + 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"}}, ), - ], - ids=[ - "auto", - "none", - "required", - "named", - "omitted", - "subset-auto", - "subset-none", - "subset-required", ], ) - async def test_tool_choice_serialization( + async def test_tool_choice( self, - monkeypatch: pytest.MonkeyPatch, - version: int, + protocol: type[GatewayV3Protocol | GatewayV4Protocol], choice: ai.ToolChoiceMode | ai.ToolRef | ai.ToolSelection | None, - expected: dict[str, str] | None, + expected: dict[str, Any], ) -> 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)) + """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": {"unified": "stop", "raw": "stop"}, - "usage": { - "inputTokens": { - "total": 1, - "noCache": 1, - "cacheRead": 0, - "cacheWrite": 0, - }, - "outputTokens": { - "total": 0, - "text": 0, - "reasoning": 0, - }, - }, - } + {"type": "finish", "finishReason": "stop", "usage": {}} ), ) model = mock_model(httpx.MockTransport(handler)).with_protocol( - GatewayV3Protocol() if version == 3 else GatewayV4Protocol() + protocol() ) - declared_tools = [ - tools.Tool( - kind="function", - name=name, - spec=tools.ToolSpec( - params={"type": "object", "properties": {}} - ), - ) - for name in ["lookup", "summarize"] - ] - params = ai.InferenceRequestParams( + request_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 + await _collect(model, [user_msg("Hi")], params=request_params) - 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 + 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] = {}