Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Streaming: introduce opt-in unified StreamEvent (+ adapters, example, tests) #2650

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

Conversation

Copy link

@lucasalencarxisto-stack lucasalencarxisto-stack commented Sep 20, 2025

Summary
This PR introduces an opt-in unified streaming event shape for both responses.stream() and chat.completions.stream():

  • Added internal StreamEvent + extract_text helper for consistent DX.
  • Created adapters for Responses and Chat Completions (map text deltas + completion events).
  • Added a minimal async example under examples/async_stream_unified.py.
  • Added basic unit tests for mapping and helper behavior.

Why
Currently the two streaming APIs emit different shapes. A unified, typed surface:

  • Simplifies printing incremental text (output_text.delta)
  • Standardizes completion signaling (response.completed)
  • Sets a foundation to later add tool-calling / error events without changing usage pattern.

Notes

  • Backward compatible: existing .stream() behavior is unchanged.
  • Example requires a valid API key to run; unit tests run offline.

Follow-ups (planned)

  • Wire-in: unified=True flag in .stream() to yield StreamEvent directly.
  • Add tool-calling and error events.
  • Improve retry/timeout semantics for streaming in a separate PR.

Copy link
Author

Hi team 👋

I believe this PR is valuable because it addresses a real developer experience gap: today responses.stream() and chat.completions.stream() emit different event shapes, which makes code harder to reuse.

By introducing a unified StreamEvent with a simple helper (extract_text), we get:

  • Consistent handling of text deltas and completion signals
  • A foundation to later support tool-calling and error events
  • A smoother DX without breaking existing behavior

This is opt-in and fully backward-compatible, with basic tests and an example included. I think it can really help developers adopt streaming more easily. 🚀

Copy link
Collaborator

Thanks for the PR but this is not something we want to do. The APIs are sufficiently different that it does not make sense to coalesce them.

Copy link
Author

Thanks for taking the time to review this, Robert — I completely understand your reasoning here.

When you have a chance, could you also take a look at my other open PRs (#2596, #2597, #2615, #2616, #2645)? They’re smaller in scope and more focused on tests/examples. I’d really appreciate your feedback on those.

Thanks again for your guidance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /