Skip to content

Conversation

@mschristensen
Copy link
Contributor

@mschristensen mschristensen commented Jan 5, 2026

Description

This PR refactors the OpenAI SDK guide for the message-per-token streaming pattern.

  • Renames the guide title and nav item
  • Restructures content into clear step-by-step progression
  • Streamlines code and copy for readability

Checklist

@coderabbitai
Copy link

coderabbitai bot commented Jan 5, 2026

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mschristensen mschristensen force-pushed the mike/AIT-token-streaming-OpenAI-SDK branch from a06f5d1 to 52b2e41 Compare January 5, 2026 18:20
@mschristensen mschristensen force-pushed the mike/AIT-token-streaming-OpenAI-SDK branch from 52b2e41 to 3411525 Compare January 6, 2026 10:33
@mschristensen mschristensen force-pushed the mike/AIT-token-streaming-OpenAI-SDK branch from 3411525 to b7116ee Compare January 6, 2026 10:34
Uses numbered steps in a tutorial-like format.
Simplifies the code and descriptions of the event streaming model,
restricting to the relevant context for this tutorial.
Improves copy and code for readability.
Indicates use of the message per token pattern.
@mschristensen mschristensen marked this pull request as ready for review January 6, 2026 17:57
@mschristensen mschristensen added the review-app Create a Heroku review app label Jan 6, 2026
@ably-ci ably-ci temporarily deployed to ably-docs-mike-ait-toke-p1zano January 6, 2026 18:02 Inactive
@ably-ci ably-ci temporarily deployed to ably-docs-mike-ait-toke-p1zano January 6, 2026 18:07 Inactive
@ably-ci ably-ci temporarily deployed to ably-docs-mike-ait-toke-p1zano January 6, 2026 18:44 Inactive
Copy link
Member

@paddybyers paddybyers left a comment

Choose a reason for hiding this comment

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

lgtm

</Code>

<Aside data-type="note">
This guide uses version 4.x of the OpenAI SDK. Some details of interacting with the OpenAI SDK may diverge from those given here if using a different major version.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
This guide uses version 4.x of the OpenAI SDK. Some details of interacting with the OpenAI SDK may diverge from those given here if using a different major version.
This guide uses version 4.x of the OpenAI SDK. Some details of interacting with the OpenAI SDK may differ from those given here if using a different major version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

</Code>

<Aside data-type="note">
This is only a representative example for a simple "text in, text out" use case and may not reflect the exact sequence of events that you observe from the OpenAI API. It also does not describe response generation errors or refusals. For complete details on all event types and their properties, see [OpenAI Streaming events](https://platform.openai.com/docs/api-reference/responses-streaming/response).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
This is only a representative example for a simple "text in, text out" use case and may not reflect the exact sequence of events that you observe from the OpenAI API. It also does not describe response generation errors or refusals. For complete details on all event types and their properties, see [OpenAI Streaming events](https://platform.openai.com/docs/api-reference/responses-streaming/response).
This is only an illustrative example for a simple "text in, text out" use case and may not reflect the exact sequence of events that you observe from the OpenAI API. It also does not describe response generation errors or refusals. For complete details on all event types and their properties, see [OpenAI Streaming events](https://platform.openai.com/docs/api-reference/responses-streaming/response).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

- Publishes a `stop` event when the response completes

<Aside data-type="note">
Ably messages are published without `await` to maximize throughput. Ably maintains message ordering even without awaiting each publish. For more information, see [Publishing tokens](/docs/ai-transport/features/token-streaming/message-per-token#publishing).
Copy link
Member

Choose a reason for hiding this comment

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

How do we recommend checking for error responses from the publish?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a separate ticket to address this: https://ably.atlassian.net/browse/AIT-238


### Publishing concurrent responses <a id="multiple-publishers"/>

The implementation uses `responseId` in message extras to correlate tokens with their originating response. This enables multiple publishers to stream different responses concurrently on the same channel, with each subscriber correctly tracking all responses independently.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
The implementation uses `responseId` in message extras to correlate tokens with their originating response. This enables multiple publishers to stream different responses concurrently on the same channel, with each subscriber correctly tracking all responses independently.
The implementation uses `responseId` in message `extras` to correlate tokens with their originating response. This enables multiple publishers to stream different responses concurrently on the same channel, with each subscriber correctly tracking all responses independently.

(Should we be quoting extras, name etc with backticks?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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


This guide shows you how to stream AI responses from OpenAI's [Responses API](https://platform.openai.com/docs/api-reference/responses) over Ably using the [message-per-token pattern](/docs/ai-transport/features/token-streaming/message-per-token). Specifically, it implements the [explicit start/stop events approach](/docs/ai-transport/features/token-streaming/message-per-token#explicit-events), which publishes each response token as an individual message, along with explicit lifecycle events to signal when responses begin and end.

Using Ably to distribute tokens from the OpenAI SDK enables you to broadcast AI responses to thousands of concurrent subscribers with reliable message delivery and ordering guarantees. This approach decouples your AI inference from client connections, enabling you to scale independently and handle reconnections gracefully while ensuring each client receives the complete response stream with all tokens delivered in order.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Using Ably to distribute tokens from the OpenAI SDK enables you to broadcast AI responses to thousands of concurrent subscribers with reliable message delivery and ordering guarantees. This approach decouples your AI inference from client connections, enabling you to scale independently and handle reconnections gracefully while ensuring each client receives the complete response stream with all tokens delivered in order.
Using Ably to distribute tokens from the OpenAI SDK enables you to broadcast AI responses to thousands of concurrent subscribers with reliable message delivery and ordering guarantees, ensuring that each client receives the complete response stream with all tokens delivered in order. This approach decouples your AI inference from client connections, enabling you to scale each independently and handle reconnections gracefully.

Copy link
Contributor Author

Choose a reason for hiding this comment

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


- [`response.created`](https://platform.openai.com/docs/api-reference/responses-streaming/response/created): Signals the start of a response. Contains `response.id` to correlate subsequent events.

- [`response.output_item.added`](https://platform.openai.com/docs/api-reference/responses-streaming/response/output_item/added): Indicates a new output item. If `item.type === "message"` the item contains model response text; other types may be specified, such as `"reasoning"` for internal reasoning tokens. The `item.id` can be used to filter which tokens to stream. The `output_index` indicates the position of this item in the response's output array.
Copy link
Contributor

Choose a reason for hiding this comment

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

The item.id can be used to filter which tokens to stream.
I don't quite get this line. I think you're saying "The item.id will be present on on all events relating to this item, so you can use it to filter which tokens are streamed to the clients" but there's probably a better way of saying it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ably-ci ably-ci temporarily deployed to ably-docs-mike-ait-toke-p1zano January 7, 2026 08:50 Inactive
@ably-ci ably-ci temporarily deployed to ably-docs-mike-ait-toke-p1zano January 7, 2026 08:57 Inactive
@mschristensen mschristensen merged commit 2c085fd into AIT-token-streaming-OpenAI-SDK Jan 7, 2026
7 checks passed
@mschristensen mschristensen deleted the mike/AIT-token-streaming-OpenAI-SDK branch January 7, 2026 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-app Create a Heroku review app

Development

Successfully merging this pull request may close these issues.

5 participants