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.

JavaScript

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

adapterTagString
openai-responses. The identifier the transport adds to its channel's params.agent.
encodeFunction
Encode one event, as the Codec contract describes.
decodeFunction
Decode one Ably message, as the Codec contract describes.

The codec writes each event type in one of three ways:

  • Streams: the first *.delta event 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_progress and response.queued are not published, because they do not carry any output.
  • Every other event, and the input event, is a plain publish with the whole event as the message's data field.

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.

JavaScript

1

2

3

import { createOpenAICodec } from '@ably/ai-transport/openai';

const codec = createOpenAICodec();

Returns

Codec<OpenAIEvent>. The codec, for events of the following type:

ResponseStreamEventResponseStreamEvent
Every stream event the Responses API produces, as the openai package declares it.
inputOpenAIInput
The input items a client sends: a user message to start a turn, or a function_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.

JavaScript

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);
});
JavaScript

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 });