Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,14 @@ def _prepare_options(self, messages: Sequence[Message], options: Mapping[str, An
"type": "function",
"function": {"name": func_name},
}
elif mode in ("auto", "required") and tool_mode.get("allowed_tools") is not None:
logger.warning(
"allowed_tools is not supported by the Chat Completions API; "
"the setting will be ignored. Use OpenAIChatClient (Responses API) instead."
)
run_options["tool_choice"] = mode
elif mode in ("auto", "required") and (allowed := tool_mode.get("allowed_tools")) is not None:
run_options["tool_choice"] = {
"type": "allowed_tools",
"allowed_tools": {
"mode": mode,
"tools": [{"type": "function", "function": {"name": name}} for name in allowed],
},
}
else:
run_options["tool_choice"] = mode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import json
import os
import re
from typing import Any, cast
from importlib import import_module
from typing import Any, Literal, cast
from unittest.mock import MagicMock, patch

import agent_framework._telemetry as telemetry
import pytest
from agent_framework import (
Agent,
ChatOptions,
ChatResponse,
Content,
Message,
Expand All @@ -27,7 +29,7 @@
from agent_framework._telemetry import FeatureIndex as CoreFeatureIndex
from agent_framework._telemetry import mark_feature_used
from agent_framework.exceptions import ChatClientException, SettingNotFoundError
from openai import BadRequestError
from openai import AsyncOpenAI, BadRequestError, DefaultAsyncHttpxClient
from openai.types.chat.chat_completion import ChatCompletion, Choice
from openai.types.chat.chat_completion_message import ChatCompletionMessage
from pydantic import BaseModel
Expand Down Expand Up @@ -1987,39 +1989,80 @@ def test_tool_choice_required_with_function_name(
assert prepared_options["tool_choice"]["function"]["name"] == "get_weather"


def test_tool_choice_allowed_tools_falls_back_to_mode(
@pytest.mark.parametrize("mode", ["auto", "required"])
@pytest.mark.parametrize("streaming", [False, True])
@pytest.mark.parametrize("allowed_tools", [["lookup_order"], ["lookup_order", "issue_refund"], []])
async def test_tool_choice_allowed_tools(
mode: Literal["auto", "required"],
streaming: bool,
allowed_tools: list[str],
openai_unit_test_env: dict[str, str],
) -> None:
"""Test that tool_choice with allowed_tools falls back to plain mode (Chat Completions API unsupported)."""
client = OpenAIChatCompletionClient()

messages = [Message(role="user", contents=["test"])]
options = {
"tools": [get_weather],
"tool_choice": {"mode": "auto", "allowed_tools": ["get_weather"]},
}

prepared_options = client._prepare_options(messages, options)
"""Send allowed tool restrictions through the SDK without widening the choice."""
httpx = import_module(DefaultAsyncHttpxClient.__mro__[1].__module__.partition(".")[0])
requests: list[dict[str, Any]] = []

assert prepared_options["tool_choice"] == "auto"
@tool
def lookup_order(order_id: str) -> str:
"""Look up an order."""
return "delivered"

@tool
def issue_refund(order_id: str, amount: int) -> str:
"""Issue a refund."""
return "refunded"

def test_tool_choice_allowed_tools_required_mode_falls_back(
openai_unit_test_env: dict[str, str],
) -> None:
"""Test that tool_choice with allowed_tools and required mode falls back to 'required'."""
client = OpenAIChatCompletionClient()
def respond(request: Any) -> Any:
requests.append(json.loads(request.content))
response = {
"id": "chatcmpl-test",
"created": 0,
"model": "test-model",
"object": "chat.completion.chunk" if streaming else "chat.completion",
"choices": [
{
"index": 0,
"delta" if streaming else "message": {"role": "assistant", "content": "delivered"},
"finish_reason": "stop",
}
],
}
if streaming:
return httpx.Response(
200,
content=f"data: {json.dumps(response)}\n\ndata: [DONE]\n\n",
headers={"content-type": "text/event-stream"},
)
return httpx.Response(200, json=response)

async with AsyncOpenAI(
api_key="test-key", http_client=httpx.AsyncClient(transport=httpx.MockTransport(respond))
) as sdk_client:
client = OpenAIChatCompletionClient(model="test-model", async_client=sdk_client)
messages = [Message(role="user", contents=["Check order ORD-123456"])]
options: ChatOptions = {
"tools": [lookup_order, issue_refund],
"tool_choice": {"mode": mode, "allowed_tools": allowed_tools},
}
if streaming:
response = await client.get_response(messages, stream=True, options=options).get_final_response()
else:
response = await client.get_response(messages, options=options)

messages = [Message(role="user", contents=["test"])]
options = {
"tools": [get_weather],
"tool_choice": {"mode": "required", "allowed_tools": ["get_weather"]},
assert response.text == "delivered"
assert len(requests) == 1
assert [declared_tool["function"]["name"] for declared_tool in requests[0]["tools"]] == [
"lookup_order",
"issue_refund",
]
assert requests[0]["tool_choice"] == {
"type": "allowed_tools",
"allowed_tools": {
"mode": mode,
"tools": [{"type": "function", "function": {"name": name}} for name in allowed_tools],
},
}

prepared_options = client._prepare_options(messages, options)

assert prepared_options["tool_choice"] == "required"


def test_tool_choice_auto_dict_without_allowed_tools(
openai_unit_test_env: dict[str, str],
Expand Down
Loading