Skip to content

Commit 041489a

Browse files
alanhuangyooclaude
andcommitted
Python: drop the continuation_token before yielding the terminal update
A consumer that stops iterating at the terminal update never resumed the generator past that yield, so the cleanup after the loop didn't run and the options kept the token. Pop it when the terminal event arrives. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 88c734d commit 041489a

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎python/packages/openai/agent_framework_openai/_chat_client.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,6 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
743743
# proxy ``.headers``. Degrade gracefully so the served-model surfacing is
744744
# best-effort instead of crashing the whole call.
745745
served_model = self._extract_served_model(getattr(raw_stream_response, "headers", None))
746-
background_response_finished = False
747746
async with _open_event_stream(raw_stream_response) as stream_response:
748747
async for chunk in stream_response:
749748
update = self._parse_chunk_from_openai(
@@ -754,16 +753,20 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
754753
)
755754
if served_model is not None:
756755
update.model = served_model
757-
if chunk.type in ("response.completed", "response.incomplete", "response.failed"):
758-
background_response_finished = True
756+
if chunk.type in (
757+
"response.completed",
758+
"response.incomplete",
759+
"response.failed",
760+
) and isinstance(options, dict):
761+
# Same as the non-streaming path (issue #5394): once the resumed
762+
# background response has finished, drop the continuation_token
763+
# from the caller's options dict. FunctionInvocationLayer reuses
764+
# that dict, so a leftover token makes the next tool-loop iteration
765+
# retrieve this response again instead of POSTing the tool results,
766+
# and the tools run again each time. Do it before yielding, so a
767+
# consumer that stops at the terminal update doesn't keep it.
768+
options.pop("continuation_token", None)
759769
yield update
760-
# Same as the non-streaming path (issue #5394): once the resumed background
761-
# response has finished, drop the continuation_token from the caller's options
762-
# dict. FunctionInvocationLayer reuses that dict, so a leftover token makes the
763-
# next tool-loop iteration retrieve this response again instead of POSTing the
764-
# tool results, and the tools run again each time.
765-
if background_response_finished and isinstance(options, dict):
766-
options.pop("continuation_token", None)
767770
except Exception as ex:
768771
self._handle_request_error(ex)
769772
else:

‎python/packages/openai/tests/openai/test_openai_chat_client.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7544,6 +7544,25 @@ def send_email(to: str) -> str:
75447544
assert final.text == "Email sent."
75457545

75467546

7547+
async def test_streaming_resume_drops_continuation_token_before_the_terminal_update() -> None:
7548+
"""A consumer that stops at the terminal update must not leave the token in its options."""
7549+
retrieve = AsyncMock(
7550+
side_effect=lambda *args, **kwargs: _FakeAsyncEventStream([_completed_response_event("resp_bg", "message")])
7551+
)
7552+
7553+
client = OpenAIChatClient(model="test-model", api_key="test-key")
7554+
# The tool loop hands its own options dict to _inner_get_response and reuses it.
7555+
options: dict[str, Any] = {"continuation_token": {"response_id": "resp_bg"}}
7556+
with patch.object(client.client.responses.with_raw_response, "retrieve", new=retrieve):
7557+
stream = _as_chat_response_stream(
7558+
client._inner_get_response(messages=[Message(role="user", contents=["hi"])], options=options, stream=True)
7559+
)
7560+
async for _ in stream:
7561+
break
7562+
7563+
assert "continuation_token" not in options
7564+
7565+
75477566
async def test_prepare_options_excludes_continuation_token() -> None:
75487567
"""Test that _prepare_options does not pass continuation_token to OpenAI API."""
75497568
client = OpenAIChatClient(model="test-model", api_key="test-key")

0 commit comments

Comments
 (0)