OpenAI codec
The OpenAI codec carries the OpenAI Responses API's stream events over an Ably channel, plus an input event for the input items a client sends. The agent pipes the Responses stream from the openai package directly into the transport, which encodes the stream into Ably messages, and clients decode the messages back into the same stream of LLM events.
Import openai from @ably/ai-transport/openai and pass it to createTransport. The codec works with the openai package at version 6.
1
2
3
4
5
import { createTransport } from '@ably/ai-transport';
import { openai } from '@ably/ai-transport/openai';
// ably is your Ably.Realtime client.
const transport = createTransport({ client: ably, channelName: 'conversations:42', codec: openai });Properties
The codec writes each event type in one of three ways:
- Streams: the first
*.deltaevent of a stream creates a single message, and later deltas append to that same message, accumulating all the deltas into a single Ably message. response.created,response.in_progressandresponse.queuedare not published, because they do not carry any output.- Every other event, and the
inputevent, is a plain publish with the whole event as the message'sdatafield.
The Responses API duplicates content in the *.done events: these events carry the full streamed data a second time. The codec drops the duplicated fields from the done events, preferring the streamed deltas that were already sent over the channel.
Create an OpenAI codec
createOpenAICodec(): Codec<OpenAIEvent>Build an OpenAI codec.
1
2
3
import { createOpenAICodec } from '@ably/ai-transport/openai';
const codec = createOpenAICodec();Returns
Codec<OpenAIEvent>. The codec, for events of the following type:
ResponseStreamEventResponseStreamEventopenai package declares it.inputOpenAIInputfunction_call_output that answers a function call.Example
The client sends the user's message as an input event. The agent pipes the Responses stream, and the client appends each output text delta to the response it renders.
1
2
3
4
5
6
7
8
9
// Client code.
await transport.send({
type: 'input',
items: [{ role: 'user', content: 'What is the weather?' }],
});
transport.subscribe(({ event }) => {
if (event?.type === 'response.output_text.delta') appendText(event.item_id, event.delta);
});1
2
3
4
5
6
7
// Agent code.
import OpenAI from 'openai';
const client = new OpenAI();
const stream = await client.responses.create({ model: 'gpt-4o-mini', input, stream: true });
await transport.pipe(stream, { signal: req.signal });