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
4 changes: 2 additions & 2 deletions src/ai/providers/ai_gateway/protocol/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:
Expand Down
63 changes: 63 additions & 0 deletions tests/providers/ai_gateway/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ai.providers.ai_gateway import (
GatewayParams,
GatewayV3Protocol,
GatewayV4Protocol,
ProviderTimeoutsParams,
)
from ai.types import events, messages
Expand Down Expand Up @@ -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] = {}

Expand Down
Loading