-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Python: Add long-running agents and background responses support #3808
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
Python: Add long-running agents and background responses support #3808
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Adds Python support for long-running agent operations via OpenAI Responses “background” runs, including continuation token propagation across core response types, OpenAI client polling/resumption paths, telemetry fixes for streaming spans, plus tests and a sample.
Changes:
- Introduces a
ContinuationTokenTypedDict and propagates it throughChatResponse*andAgentResponse*types and mapping/merge logic. - Extends
OpenAIResponsesClientoptions withbackground+continuation_token, enabling non-streaming polling and streaming resumption via retrieve. - Fixes OpenTelemetry streaming span lifecycle handling and adds unit tests + a usage sample + changelog entry.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| python/packages/core/agent_framework/_types.py | Adds ContinuationToken and plumbs continuation_token through response/update types and update processing. |
| python/packages/core/agent_framework/openai/_responses_client.py | Adds background + continuation token support, including polling and stream resumption behavior. |
| python/packages/core/agent_framework/_agents.py | Propagates continuation_token into AgentResponse (non-streaming path). |
| python/packages/core/agent_framework/observability.py | Fixes streaming span detach errors by avoiding context attachment for spans closed in async cleanup. |
| python/packages/core/tests/openai/test_openai_responses_client.py | Adds unit tests covering token serialization/propagation and background status handling. |
| python/samples/concepts/background_responses.py | Adds a sample demonstrating polling and streaming resumption patterns. |
| python/CHANGELOG.md | Documents the new long-running/background responses feature. |
python/packages/core/tests/openai/test_openai_responses_client.py
Outdated
Show resolved
Hide resolved
7d72bcf to
9571b91
Compare
- Add ContinuationToken TypedDict to core types - Add continuation_token field to ChatResponse, ChatResponseUpdate, AgentResponse, and AgentResponseUpdate - Add background and continuation_token options to OpenAIResponsesOptions - Implement polling via responses.retrieve() and streaming resumption in RawOpenAIResponsesClient - Propagate continuation tokens through agent run() and map_chat_to_agent_update - Fix streaming telemetry 'Failed to detach context' error in both ChatTelemetryLayer and AgentTelemetryLayer by avoiding trace.use_span() context attachment for async-managed spans - Add 14 unit tests for continuation token types and background flows - Add background_responses sample showing polling and stream resumption Fixes microsoft#2478
- Make ContinuationToken provider-agnostic (total=False, optional task_id/context_id fields) - Add background param to A2AAgent.run() controlling token emission - Add poll_task() for single-request task state retrieval - Add resubscribe support via continuation_token param on run() - Extract _updates_from_task() and _map_a2a_stream() for cleaner code - Streamline run()/streaming by removing intermediate _stream_updates wrapper - Update A2A sample to show background=False (default) with link to background_responses sample - Remove stale BareAgent from __all__ - Add 12 new A2A continuation token tests
9571b91 to
80c2c52
Compare
Summary
Adds support for long-running agent operations and background responses, matching the dotnet implementation pattern adapted to Python conventions.
Fixes #2478
Changes
Core Types (
_types.py)ContinuationTokenTypedDict — a simple JSON-serializable dict withresponse_idfieldcontinuation_token: ContinuationToken | NonetoChatResponse,ChatResponseUpdate,AgentResponse, andAgentResponseUpdatemap_chat_to_agent_updateand_process_updateto propagate tokensOpenAI Responses Client (
_responses_client.py)background: boolandcontinuation_token: ContinuationTokentoOpenAIResponsesOptionsresponses.retrieve(response_id)whencontinuation_tokenis providedresponses.retrieve(response_id, stream=True)whencontinuation_tokenis providedcontinuation_tokenon responses when OpenAI status isin_progressorqueuedAgent Layer (
_agents.py)continuation_tokenfromChatResponse→AgentResponsein non-streaming pathTelemetry Fix (
observability.py)Failed to detach contexterror in bothChatTelemetryLayerandAgentTelemetryLayerstreaming pathstrace.use_span()context was attached synchronously but detached in async cleanup hooks (different async context)start_span()+span.end()for streaming, avoiding context attachmentTests
Sample
samples/concepts/background_responses.pydemonstrating non-streaming polling and streaming resumption patternsUsage