diff --git a/src/ai/providers/ai_gateway/protocol/_shared.py b/src/ai/providers/ai_gateway/protocol/_shared.py index a6474ddd..9d27ebc7 100644 --- a/src/ai/providers/ai_gateway/protocol/_shared.py +++ b/src/ai/providers/ai_gateway/protocol/_shared.py @@ -3,7 +3,7 @@ from __future__ import annotations import json -from collections.abc import Callable, Mapping +from collections.abc import Callable, Mapping, Sequence from typing import Any import httpx2 as httpx @@ -138,6 +138,24 @@ def tool_to_wire(tool: types.tools.Tool) -> dict[str, Any]: } +def tools_to_wire( + tools: Sequence[types.tools.Tool], + params: models.InferenceRequestParams | None, +) -> list[dict[str, Any]]: + """Convert tools to the wire format, keeping only a selected subset. + + The gateway has no tool-subset field, so a ``ToolSelection`` drops the + other tools from the request. + """ + tool_calling = params.tool_calling if params is not None else None + if tool_calling is not None and isinstance( + tool_calling.tool_choice, models.ToolSelection + ): + selected = tool_calling.tool_choice.tools + tools = [tool for tool in tools if tool.name in selected] + return [tool_to_wire(tool) for tool in tools] + + def response_format(output_type: type[Any] | None) -> dict[str, Any] | None: """Build the ``responseFormat`` field for a structured-output request.""" if output_type is None or not issubclass(output_type, pydantic.BaseModel): @@ -395,9 +413,7 @@ def request_options( "toolName": str(tool_choice), } else: - body["activeTools"] = sorted( - str(tool) for tool in tool_choice.tools - ) + # The subset itself is applied in `tools_to_wire`. body["toolChoice"] = {"type": tool_choice.mode.value} # provider service diff --git a/src/ai/providers/ai_gateway/protocol/v3.py b/src/ai/providers/ai_gateway/protocol/v3.py index fe4e153d..cf9f1278 100644 --- a/src/ai/providers/ai_gateway/protocol/v3.py +++ b/src/ai/providers/ai_gateway/protocol/v3.py @@ -491,7 +491,7 @@ async def stream( _shared.merge_extra_body(body, params.extra_body) body["prompt"] = await _messages_to_prompt(messages) if tools: - body["tools"] = [_shared.tool_to_wire(tool) for tool in tools] + body["tools"] = _shared.tools_to_wire(tools, params) response_format = _shared.response_format(output_type) if response_format is not None: body["responseFormat"] = response_format diff --git a/src/ai/providers/ai_gateway/protocol/v4.py b/src/ai/providers/ai_gateway/protocol/v4.py index 15233283..02705717 100644 --- a/src/ai/providers/ai_gateway/protocol/v4.py +++ b/src/ai/providers/ai_gateway/protocol/v4.py @@ -488,7 +488,7 @@ async def stream( _shared.merge_extra_body(body, params.extra_body) body["prompt"] = await _messages_to_prompt(messages) if tools: - body["tools"] = [_shared.tool_to_wire(tool) for tool in tools] + body["tools"] = _shared.tools_to_wire(tools, params) response_format = _shared.response_format(output_type) if response_format is not None: body["responseFormat"] = response_format diff --git a/tests/providers/ai_gateway/test_stream.py b/tests/providers/ai_gateway/test_stream.py index 6381bb7f..2aa5ebae 100644 --- a/tests/providers/ai_gateway/test_stream.py +++ b/tests/providers/ai_gateway/test_stream.py @@ -364,16 +364,16 @@ class TestRequest: {"toolChoice": {"type": "tool", "toolName": "a"}}, ), ( - ai.ToolSelection(["b", "a"], mode=ai.ToolChoiceMode.AUTO), - {"activeTools": ["a", "b"], "toolChoice": {"type": "auto"}}, + ai.ToolSelection(["a"], mode=ai.ToolChoiceMode.AUTO), + {"toolChoice": {"type": "auto"}}, ), ( - ai.ToolSelection(["b", "a"], mode=ai.ToolChoiceMode.NONE), - {"activeTools": ["a", "b"], "toolChoice": {"type": "none"}}, + ai.ToolSelection(["a"], mode=ai.ToolChoiceMode.NONE), + {"toolChoice": {"type": "none"}}, ), ( - ai.ToolSelection(["b", "a"], mode=ai.ToolChoiceMode.REQUIRED), - {"activeTools": ["a", "b"], "toolChoice": {"type": "required"}}, + ai.ToolSelection(["a"], mode=ai.ToolChoiceMode.REQUIRED), + {"toolChoice": {"type": "required"}}, ), ], ) @@ -413,6 +413,53 @@ def handler(req: httpx.Request) -> httpx.Response: if key in captured_body } == expected + @pytest.mark.parametrize("protocol", [GatewayV3Protocol, GatewayV4Protocol]) + async def test_tool_selection_sends_only_selected_tools( + self, protocol: type[GatewayV3Protocol | GatewayV4Protocol] + ) -> None: + """The gateway has no tool-subset field, so the SDK filters tools.""" + + @ai.tool + async def lookup(query: str) -> str: + """Search the database.""" + return "result" + + @ai.tool + async def summarize(text: str) -> str: + """Summarize the text.""" + return "summary" + + 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=ai.ToolSelection( + ["lookup"], mode=ai.ToolChoiceMode.REQUIRED + ) + ) + ) + await _collect( + model, + [user_msg("Hi")], + tools=[lookup.tool, summarize.tool], + params=request_params, + ) + + assert [tool["name"] for tool in captured_body["tools"]] == ["lookup"] + assert captured_body["toolChoice"] == {"type": "required"} + async def test_protocol_headers(self) -> None: captured: dict[str, str] = {}