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 ChatCompletionStreamingRunner

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

Overview

ChatCompletionStreamingRunner is a streaming runner class that manages streamed chat completion requests with automatic tool call execution.

Description

The ChatCompletionStreamingRunner class extends ChatCompletionStream and implements AsyncIterable<ChatCompletionChunk>, combining streaming response processing with an automatic tool-calling loop. It provides two static factory methods: fromReadableStream for wrapping an existing readable stream and runTools for initiating a full streaming tool-calling workflow.

The ChatCompletionStreamEvents interface extends the base runner events with two streaming-specific events: content, which fires with both the incremental delta and accumulated snapshot of text content, and chunk, which fires for every raw ChatCompletionChunk alongside the current snapshot. The ChatCompletionStreamingToolRunnerParams type mirrors the non-streaming runner params but uses ChatCompletionCreateParamsStreaming as its base, ensuring the stream: true parameter is set.

Like its non-streaming counterpart, the runner sets the X-Stainless-Helper-Method: runTools header for telemetry when using the runTools factory method.

Usage

Use ChatCompletionStreamingRunner when you need streaming chat completions with automatic tool call handling. This is typically accessed through the higher-level client.beta.chat.completions.runTools({ stream: true }) API. It is also useful when you already have a ReadableStream of chat completion chunks and want to process them as an async iterable.

Code Reference

Source Location

Signature

export interface ChatCompletionStreamEvents extends AbstractChatCompletionRunnerEvents {
  content: (contentDelta: string, contentSnapshot: string) => void;
  chunk: (chunk: ChatCompletionChunk, snapshot: ChatCompletionSnapshot) => void;
}

export type ChatCompletionStreamingToolRunnerParams<FunctionsArgs extends BaseFunctionsArgs> = Omit<
  ChatCompletionCreateParamsStreaming,
  'tools'
> & {
  tools: RunnableTools<FunctionsArgs> | AutoParseableTool<any, true>[];
};

export class ChatCompletionStreamingRunner<ParsedT = null>
  extends ChatCompletionStream<ParsedT>
  implements AsyncIterable<ChatCompletionChunk>
{
  static fromReadableStream(stream: ReadableStream): ChatCompletionStreamingRunner<null>;
  static runTools<T extends (string | object)[], ParsedT = null>(
    client: OpenAI,
    params: ChatCompletionStreamingToolRunnerParams<T>,
    options?: RunnerOptions,
  ): ChatCompletionStreamingRunner<ParsedT>;
}

Import

import { ChatCompletionStreamingRunner } from 'openai/lib/ChatCompletionStreamingRunner';

I/O Contract

Inputs

Name Type Required Description
client OpenAI Yes (for runTools) The OpenAI client instance used to make streaming API requests.
params ChatCompletionStreamingToolRunnerParams Yes (for runTools) Streaming chat completion parameters with runnable tool definitions.
stream ReadableStream Yes (for fromReadableStream) An existing readable stream of chat completion chunks.
options RunnerOptions No Additional runner options such as custom headers.

Outputs

Name Type Description
runner ChatCompletionStreamingRunner<ParsedT> An async-iterable, event-driven runner that emits content, chunk, error, end, and other lifecycle events as streaming data arrives.

Usage Examples

import OpenAI from 'openai';

const client = new OpenAI();

const runner = client.beta.chat.completions.runTools({
  model: 'gpt-4o',
  stream: true,
  messages: [{ role: 'user', content: 'What is the weather in NYC?' }],
  tools: [
    {
      type: 'function',
      function: {
        name: 'get_weather',
        description: 'Get weather for a location',
        parameters: { type: 'object', properties: { location: { type: 'string' } }, required: ['location'] },
        function: async (args: { location: string }) => {
          return JSON.stringify({ temperature: 72, condition: 'sunny' });
        },
        parse: JSON.parse,
      },
    },
  ],
});

runner.on('content', (delta, snapshot) => {
  process.stdout.write(delta);
});

for await (const chunk of runner) {
  // Process each chunk as it arrives
}

const finalCompletion = await runner.finalChatCompletion();

Related Pages

Page Connections

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