Skip to content

add overwrite mechanism for stream_options #465

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

Merged
merged 1 commit into from
Apr 10, 2025
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: 4 additions & 0 deletions src/agents/model_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class ModelSettings:
"""Whether to store the generated model response for later retrieval.
Defaults to True if not provided."""

include_usage: bool | None = None
"""Whether to include usage chunk.
Defaults to True if not provided."""

def resolve(self, override: ModelSettings | None) -> ModelSettings:
"""Produce a new ModelSettings by overlaying any non-None values from the
override on top of this instance."""
Expand Down
21 changes: 19 additions & 2 deletions src/agents/models/openai_chatcompletions.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ async def _fetch_response(
reasoning_effort = model_settings.reasoning.effort if model_settings.reasoning else None
store = _Converter.get_store_param(self._get_client(), model_settings)

stream_options = _Converter.get_stream_options_param(self._get_client(), model_settings)

ret = await self._get_client().chat.completions.create(
model=self.model,
messages=converted_messages,
Expand All @@ -534,7 +536,7 @@ async def _fetch_response(
response_format=response_format,
parallel_tool_calls=parallel_tool_calls,
stream=stream,
stream_options={"include_usage": True} if stream else NOT_GIVEN,
stream_options=self._non_null_or_not_given(stream_options),
store=self._non_null_or_not_given(store),
reasoning_effort=self._non_null_or_not_given(reasoning_effort),
extra_headers=_HEADERS,
Expand Down Expand Up @@ -568,12 +570,27 @@ def _get_client(self) -> AsyncOpenAI:


class _Converter:

@classmethod
def is_openai(cls, client: AsyncOpenAI):
return str(client.base_url).startswith("https://api.openai.com")

@classmethod
def get_store_param(cls, client: AsyncOpenAI, model_settings: ModelSettings) -> bool | None:
# Match the behavior of Responses where store is True when not given
default_store = True if str(client.base_url).startswith("https://api.openai.com") else None
default_store = True if cls.is_openai(client) else None
return model_settings.store if model_settings.store is not None else default_store

@classmethod
def get_stream_options_param(
cls, client: AsyncOpenAI, model_settings: ModelSettings
) -> dict[str, bool] | None:
default_include_usage = True if cls.is_openai(client) else None
include_usage = model_settings.include_usage if model_settings.include_usage is not None \
else default_include_usage
stream_options = {"include_usage": include_usage} if include_usage is not None else None
return stream_options

@classmethod
def convert_tool_choice(
cls, tool_choice: Literal["auto", "required", "none"] | str | None
Expand Down
2 changes: 1 addition & 1 deletion tests/test_openai_chatcompletions.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def __init__(self, completions: DummyCompletions) -> None:
# Check OpenAI client was called for streaming
assert completions.kwargs["stream"] is True
assert completions.kwargs["store"] is NOT_GIVEN
assert completions.kwargs["stream_options"] == {"include_usage": True}
assert completions.kwargs["stream_options"] is NOT_GIVEN
# Response is a proper openai Response
assert isinstance(response, Response)
assert response.id == FAKE_RESPONSES_ID
Expand Down