Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai node ResponseStream

From Leeroopedia
Revision as of 13:36, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Openai_Openai_node_ResponseStream.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains SDK, Streaming, Responses_API
Last Updated 2026-02-15 12:00 GMT

Overview

The ResponseStream class provides an event-driven streaming interface for the Responses API, accumulating server-sent events into a Response snapshot and emitting typed events for text deltas, function call arguments, and other response lifecycle events.

Description

ResponseStream<ParsedT> extends EventStream<ResponseEvents> and implements AsyncIterable<ResponseStreamEvent>. It is the streaming abstraction for the Responses API, analogous to what ChatCompletionStream is for Chat Completions.

The class accumulates incoming events into a mutable Response snapshot. The #accumulateResponse method handles event-type-specific logic: response.created initializes the snapshot, response.output_item.added pushes new output items, response.content_part.added adds content parts to message outputs, response.output_text.delta appends text to output text content, response.function_call_arguments.delta appends to function call arguments, and response.completed replaces the snapshot with the final response.

For text delta and function call argument delta events, the stream enriches the emitted event with a snapshot field containing the accumulated text so far, providing consumers with both the incremental delta and the running total. The class supports both creating new responses and retrieving/resuming existing ones via the response_id parameter, with an optional starting_after sequence number to skip already-processed events.

Upon stream completion, finalizeResponse runs maybeParseResponse to apply auto-parsing for structured outputs and tools, producing a ParsedResponse<ParsedT>. The finalResponse() method returns a promise that resolves with this parsed response.

Usage

This class is created by client.responses.stream() or client.responses.stream({ response_id: '...' }) to stream Responses API results. It is not typically instantiated directly.

Code Reference

Source Location

Signature

export class ResponseStream<ParsedT = null>
  extends EventStream<ResponseEvents>
  implements AsyncIterable<ResponseStreamEvent>
{
  constructor(params: ResponseStreamingParams | null);

  static createResponse<ParsedT>(
    client: OpenAI,
    params: ResponseStreamParams,
    options?: RequestOptions,
  ): ResponseStream<ParsedT>;

  [Symbol.asyncIterator](): AsyncIterator<ResponseStreamEvent>;
  finalResponse(): Promise<ParsedResponse<ParsedT>>;
}

Import

import { ResponseStream } from 'openai/lib/responses/ResponseStream';

I/O Contract

Inputs

Name Type Required Description
client OpenAI Yes The OpenAI client instance
params ResponseStreamParams Yes Either creation params (model, input, etc.) or { response_id, starting_after? } for resuming
options RequestOptions No Request configuration (signal, headers, etc.)

ResponseStreamParams is a union of:

Type Description
ResponseCreateAndStreamParams Standard creation params with stream?: true
ResponseStreamByIdParams Params with response_id for resuming and optional starting_after sequence number

Outputs

Name Type Description
ResponseStreamEvent AsyncIterable<ResponseStreamEvent> Async iterable of all streamed events
finalResponse() Promise<ParsedResponse<ParsedT>> The finalized parsed response after the stream completes

Key Events

Event Description
response.created The response object has been created
response.output_item.added A new output item was added
response.content_part.added A new content part was added to an output
response.output_text.delta Text delta with snapshot of accumulated text
response.function_call_arguments.delta Function call arguments delta with snapshot
response.reasoning_text.delta Reasoning text delta
response.completed The response is complete
event Catch-all for every event type

Usage Examples

Basic Usage

import OpenAI from 'openai';

const client = new OpenAI();
const stream = client.responses.stream({
  model: 'gpt-4o',
  input: 'Explain quantum entanglement briefly.',
});

stream.on('response.output_text.delta', (event) => {
  process.stdout.write(event.delta);
});

const response = await stream.finalResponse();
console.log('\nDone. Output items:', response.output.length);

Resuming a Stream

const stream = client.responses.stream({
  response_id: 'resp_abc123',
  starting_after: 42,
});

for await (const event of stream) {
  console.log(event.type);
}

Async Iteration

const stream = client.responses.stream({
  model: 'gpt-4o',
  input: 'Hello',
});

for await (const event of stream) {
  if (event.type === 'response.output_text.delta') {
    process.stdout.write(event.delta);
  }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment