Skip to content

Python: keep an in-progress continuation_token on the aggregated response - #8889

Closed
kuse (liwenjie200543) wants to merge 1 commit into
microsoft:mainfrom
liwenjie200543:fix/aggregated-continuation-token
Closed

kuse (liwenjie200543) wants to merge 1 commit into
microsoft:mainfrom
liwenjie200543:fix/aggregated-continuation-token

Conversation

@liwenjie200543

Copy link
Copy Markdown

Motivation & Context

Fixes #8826. A streamed background response that is still in progress reports itself as complete: the aggregated response loses its continuation_token on the next event that does not repeat it, and consumers that read the aggregate to decide whether to poll or re-POST act on the wrong reading.

Description & Review Guide

  • What are the major changes? _process_update — the single aggregation point behind ChatResponse.from_updates / AgentResponse.from_updates — copied update.continuation_token unconditionally, the only field among its siblings without an is not None guard. The copy is now guarded: the aggregate keeps the last token the provider actually sent, and an update that carries finish_reason (a terminal update) withdraws it.
  • What is the impact of these changes? A token the provider has not withdrawn survives later streaming events, so the OpenAI Responses client's completion checks (_chat_client.py :964, :4420) no longer see "complete" while the background operation is running, and polling/function-loop continuation stops dropping the token early. A bare guard alone would have left the stale token on the aggregate after response.completed and broken the documented "presence means in progress, None means complete" contract in the other direction — hence the paired withdrawal on finish_reason.
  • What do you want reviewers to focus on? Whether finish_reason is the right terminal marker for all providers (the OpenAI Responses parser sets it exactly on response.completed/incomplete/failed; stdio/Linux behaviour of the repro path is unchanged).

Related Issue

Fixes #8826

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Some terminal OpenAI and A2A updates lack a finish reason, causing stale continuation tokens to survive completion.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Fixes streamed response aggregation so in-progress continuation tokens survive unrelated updates and clear on terminal updates.

Changes:

  • Preserves the last non-None continuation token.
  • Clears tokens when an update has a finish reason.
  • Adds aggregation tests for both paths.
File Description
python/​packages/​core/​agent_framework/​_types.py Updates continuation-token aggregation.
python/​packages/​core/​tests/​core/​test_types.py Tests token preservation and clearing.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +2241 to +2244
elif update.finish_reason is not None:
# A terminal update withdraws the token: its presence on the aggregate
# means the operation is still in progress.
response.continuation_token = None
@eavanvalkenburg

Copy link
Copy Markdown
Member

As mentioned in the issue, the core team will fix this.

This branch was previously deployed

1 inactive deployment
github-app-auth — b97b66cd Deployed Sep 30, 2026 by liwenjie200543 via add_label #24091
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Streaming a response that is still in progress drops its continuation_token

3 participants