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
24 changes: 20 additions & 4 deletions src/ai/providers/ai_gateway/protocol/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ai/providers/ai_gateway/protocol/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ai/providers/ai_gateway/protocol/v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 53 additions & 6 deletions tests/providers/ai_gateway/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
),
],
)
Expand Down Expand Up @@ -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] = {}

Expand Down
Loading