Skip to content

Commit 4a5f382

Browse files
committed
fix: keep stream reserved when checking extra_args duplicates
Codex review noted that the previous duplicate-key filter — which ignores create_kwargs entries whose value is the OpenAI omit sentinel — also lets `extra_args={"stream": True}` slip through on the non-streaming path. `get_response()` pins `create_kwargs["stream"]` to omit when stream=False and then expects a `ChatCompletion`; if extra_args overrode it to True the client would return an async stream that the non-streaming code path cannot consume. Mark `stream` as a reserved key that must always trigger duplicate-key detection regardless of its current sentinel value. Other create_kwargs slots (`reasoning_effort`, `tool_choice`, etc.) keep the omit-aware behavior, so legitimate fall-through via extra_args still works. Add two regression tests: - extra_args={"stream": True} with stream=False must raise TypeError - extra_args={"stream": False} with stream=True must raise TypeError
1 parent 09555d5 commit 4a5f382

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎src/agents/models/openai_chatcompletions.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ def _is_openai_omitted_value(value: Any) -> bool:
4949
return isinstance(value, Omit | NotGiven)
5050

5151

52+
# Keys whose first-class create_kwargs entry is reserved by the SDK and must
53+
# never be overridden via ModelSettings.extra_args, even when the SDK passes
54+
# the value as an OpenAI omit sentinel. ``stream`` is the canonical example:
55+
# get_response() pins it to ``omit`` and then expects a non-streaming
56+
# ChatCompletion, so allowing extra_args to flip it to True would cause the
57+
# OpenAI client to return an async stream that the non-streaming code path
58+
# cannot consume.
59+
_RESERVED_CHAT_COMPLETIONS_KEYS = frozenset({"stream"})
60+
61+
5262
class OpenAIChatCompletionsModel(Model):
5363
_OFFICIAL_OPENAI_SUPPORTED_INPUT_CONTENT_TYPES = frozenset(
5464
{"input_text", "input_image", "input_audio", "input_file"}
@@ -431,7 +441,11 @@ async def _fetch_response(
431441
duplicate_extra_arg_keys = sorted(
432442
k
433443
for k in extra_args
434-
if k in create_kwargs and not _is_openai_omitted_value(create_kwargs[k])
444+
if k in create_kwargs
445+
and (
446+
k in _RESERVED_CHAT_COMPLETIONS_KEYS
447+
or not _is_openai_omitted_value(create_kwargs[k])
448+
)
435449
)
436450
if duplicate_extra_arg_keys:
437451
if len(duplicate_extra_arg_keys) == 1:

‎tests/models/test_openai_chatcompletions.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,3 +848,60 @@ async def test_fetch_response_chat_completions_rejects_duplicate_extra_args_keys
848848
tracing=ModelTracing.DISABLED,
849849
stream=False,
850850
)
851+
852+
853+
@pytest.mark.allow_call_model_methods
854+
@pytest.mark.asyncio
855+
async def test_fetch_response_chat_completions_rejects_stream_via_extra_args_in_non_streaming_call() -> ( # noqa: E501
856+
None
857+
):
858+
"""``stream`` is reserved by the SDK. The non-streaming get_response path
859+
pins create_kwargs["stream"] to the OpenAI omit sentinel and then expects
860+
a ChatCompletion. Allowing ``extra_args={"stream": True}`` to slip
861+
through the duplicate check would make the OpenAI client return an async
862+
stream that the non-streaming code path cannot consume.
863+
"""
864+
865+
_completions, dummy_client = _build_chat_completions_dummy_client()
866+
model = OpenAIChatCompletionsModel(model="gpt-4", openai_client=cast(AsyncOpenAI, dummy_client))
867+
with generation_span(disabled=True) as span:
868+
with pytest.raises(TypeError, match="multiple values.*stream"):
869+
await model._fetch_response(
870+
system_instructions=None,
871+
input="hi",
872+
model_settings=ModelSettings(extra_args={"stream": True}),
873+
tools=[],
874+
output_schema=None,
875+
handoffs=[],
876+
span=span,
877+
tracing=ModelTracing.DISABLED,
878+
stream=False,
879+
)
880+
881+
882+
@pytest.mark.allow_call_model_methods
883+
@pytest.mark.asyncio
884+
async def test_fetch_response_chat_completions_rejects_stream_via_extra_args_in_streaming_call() -> ( # noqa: E501
885+
None
886+
):
887+
"""``stream`` is also reserved on the streaming path. ``stream=True`` sets
888+
create_kwargs["stream"] to ``True`` (a real value), so the original
889+
intersection check would already catch this. Cover it explicitly so the
890+
invariant is enforced for both directions.
891+
"""
892+
893+
_completions, dummy_client = _build_chat_completions_dummy_client()
894+
model = OpenAIChatCompletionsModel(model="gpt-4", openai_client=cast(AsyncOpenAI, dummy_client))
895+
with generation_span(disabled=True) as span:
896+
with pytest.raises(TypeError, match="multiple values.*stream"):
897+
await model._fetch_response(
898+
system_instructions=None,
899+
input="hi",
900+
model_settings=ModelSettings(extra_args={"stream": False}),
901+
tools=[],
902+
output_schema=None,
903+
handoffs=[],
904+
span=span,
905+
tracing=ModelTracing.DISABLED,
906+
stream=True,
907+
)

0 commit comments

Comments
 (0)