Skip to content

Fix #980 Chat Completions: fails with function name for tool_choice parameter w/ streaming enabled #1206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions src/agents/models/openai_chatcompletions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import time
from collections.abc import AsyncIterator
from typing import TYPE_CHECKING, Any, Literal, cast, overload
from typing import TYPE_CHECKING, Any, Literal, overload

from openai import NOT_GIVEN, AsyncOpenAI, AsyncStream
from openai.types import ChatModel
Expand All @@ -28,6 +28,7 @@
from .chatcmpl_stream_handler import ChatCmplStreamHandler
from .fake_id import FAKE_RESPONSES_ID
from .interface import Model, ModelTracing
from .openai_responses import Converter as OpenAIResponsesConverter

if TYPE_CHECKING:
from ..model_settings import ModelSettings
Expand Down Expand Up @@ -296,15 +297,27 @@ async def _fetch_response(
if isinstance(ret, ChatCompletion):
return ret

responses_tool_choice = OpenAIResponsesConverter.convert_tool_choice(
model_settings.tool_choice
)
if responses_tool_choice is None or responses_tool_choice == NOT_GIVEN:
Copy link
Member Author

@seratch seratch Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The essential changes here are 1) use Converter.convert_tool_choice 2) cover responses_tool_choice is None pattern. Added more detailed comments about why it's necessary but happy to adjust the style if necessary

# For Responses API data compatibility with Chat Completions patterns,
# we need to set "none" if tool_choice is absent.
# Without this fix, you'll get the following error:
# pydantic_core._pydantic_core.ValidationError: 4 validation errors for Response
# tool_choice.literal['none','auto','required']
# Input should be 'none', 'auto' or 'required'
# [type=literal_error, input_value=NOT_GIVEN, input_type=NotGiven]
# see also: https://github.com/openai/openai-agents-python/issues/980
responses_tool_choice = "auto"

response = Response(
id=FAKE_RESPONSES_ID,
created_at=time.time(),
model=self.model,
object="response",
output=[],
tool_choice=cast(Literal["auto", "required", "none"], tool_choice)
if tool_choice != NOT_GIVEN
else "auto",
tool_choice=responses_tool_choice, # type: ignore[arg-type]
top_p=model_settings.top_p,
temperature=model_settings.temperature,
tools=[],
Expand Down