Skip to content

contrib/openai_agents: stream model events via Workflow Streams#1497

Open
jssmith wants to merge 4 commits intomainfrom
contrib/openai-agents-streaming
Open

contrib/openai_agents: stream model events via Workflow Streams#1497
jssmith wants to merge 4 commits intomainfrom
contrib/openai-agents-streaming

Conversation

@jssmith
Copy link
Copy Markdown
Contributor

@jssmith jssmith commented Apr 30, 2026

Summary

Adds a streaming integration to temporalio.contrib.openai_agents that publishes raw TResponseStreamEvent values from the model activity to a Workflow Streams topic, so a workflow can fan model output out to clients in real time without losing durability.

  • Opt in via OpenAIAgentsPlugin(model_params=ModelActivityParameters(streaming_topic="...")). With no topic set, behavior is unchanged.
  • Activity calls Runner.run_streamed and publishes each TResponseStreamEvent chunk through a typed topic handle. Final response and tool-call events are still returned to the workflow as before.
  • TResponseStreamEvent is a typing special form rather than a class, so the topic stays untyped (default Any); subscribers pass result_type=TResponseStreamEvent on their own subscribe call.

This was originally split out of PR #1423 (Workflow Streams library) and is now rebuilt as a single commit on top of merged main.

Test plan

  • poe lint clean (ruff, pyright, mypy, basedpyright, pydocstyle)
  • tests/contrib/openai_agents/test_openai_streaming.py — covers streaming opt-in, no-topic fallback, MCP path, tool-call interleaving
  • Manual exercise via streaming-comparisons demos

🤖 Generated with Claude Code

Re-applies the openai_agents streaming integration originally split out
of PR #1423 on commit 59c7582, updated for the post-PR API:
TResponseStreamEvent is a typing-special form, not a class, so the
topic stays untyped (default Any) and subscribers pass
result_type=TResponseStreamEvent on their own subscribe call.

Opt in via `OpenAIAgentsPlugin(model_params=ModelActivityParameters(
streaming_event_topic="..."))`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jssmith jssmith requested review from a team as code owners April 30, 2026 23:52
Copy link
Copy Markdown

Copilot AI left a 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 an opt-in streaming integration for temporalio.contrib.openai_agents that publishes raw model stream events to Workflow Streams so workflows can fan out model output to external clients in real time while retaining durability.

Changes:

  • Add a new streaming model activity (invoke_model_activity_streaming) that consumes Model.stream_response() and publishes each raw TResponseStreamEvent to a Workflow Streams topic.
  • Enable workflow-side Runner.run_streamed() by wiring Model.stream_response() through the Temporal model stub and adding fail-fast validations for required streaming config.
  • Add docs + integration tests covering opt-in streaming, missing-topic failure, and local-activity incompatibility.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/contrib/openai_agents/test_openai_streaming.py New integration tests validating streaming behavior and failure modes.
temporalio/contrib/openai_agents/_temporal_openai_agents.py Registers the new streaming activity with the worker.
temporalio/contrib/openai_agents/_temporal_model_stub.py Implements Model.stream_response() by invoking the streaming activity and yielding returned events.
temporalio/contrib/openai_agents/_openai_runner.py Enables workflow-safe run_streamed() with fail-fast validation and exception rewrapping.
temporalio/contrib/openai_agents/_model_parameters.py Adds streaming_topic and streaming_batch_interval configuration.
temporalio/contrib/openai_agents/_mcp.py Removes unused logging.
temporalio/contrib/openai_agents/_invoke_model_activity.py Adds streaming activity + Workflow Streams publishing and refactors tool/handoff reconstruction + OpenAI status handling.
temporalio/contrib/openai_agents/README.md Documents streaming usage and updates feature-support tables.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread temporalio/contrib/openai_agents/_invoke_model_activity.py
Comment thread temporalio/contrib/openai_agents/_temporal_model_stub.py Outdated
Comment thread temporalio/contrib/openai_agents/README.md
Drop the implicit 30s fallback for streaming activities and document that
heartbeat_timeout should be set lower than start_to_close_timeout so a
stuck model call is detected before the overall activity timeout fires.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants