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 ChatCompletionRunner

From Leeroopedia
Knowledge Sources
Domains SDK, Chat Completions
Last Updated 2026-02-15 12:00 GMT

Overview

ChatCompletionRunner is a non-streaming runner class that manages the lifecycle of chat completion requests with automatic tool call execution.

Description

The ChatCompletionRunner class extends AbstractChatCompletionRunner and provides a concrete implementation for non-streaming chat completion workflows that involve tool calls. It adds a content event that fires whenever an assistant message with text content is received, making it easy to observe final responses.

The primary entry point is the static runTools method, which creates a new runner instance, configures it with the provided client, parameters, and options, and kicks off the asynchronous tool-calling loop. The runner automatically sets the X-Stainless-Helper-Method: runTools header for telemetry purposes. The ChatCompletionToolRunnerParams type extends the standard non-streaming create parameters but replaces the tools field to accept RunnableTools or AutoParseableTool arrays, which include executable function implementations alongside their schemas.

When the runner receives an assistant message with content, it emits the content event with the message text, allowing consumers to listen for final text responses independently of the tool-calling machinery.

Usage

Use ChatCompletionRunner when you want to execute non-streaming chat completions with automatic tool call handling. This is typically accessed through the higher-level client.beta.chat.completions.runTools() API rather than being instantiated directly.

Code Reference

Source Location

Signature

export interface ChatCompletionRunnerEvents extends AbstractChatCompletionRunnerEvents {
  content: (content: string) => void;
}

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

export class ChatCompletionRunner<ParsedT = null> extends AbstractChatCompletionRunner<
  ChatCompletionRunnerEvents,
  ParsedT
> {
  static runTools<ParsedT>(
    client: OpenAI,
    params: ChatCompletionToolRunnerParams<any[]>,
    options?: RunnerOptions,
  ): ChatCompletionRunner<ParsedT>;
}

Import

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

I/O Contract

Inputs

Name Type Required Description
client OpenAI Yes The OpenAI client instance used to make API requests.
params ChatCompletionToolRunnerParams Yes Non-streaming chat completion parameters with runnable tool definitions that include executable function implementations.
options RunnerOptions No Additional runner options such as custom headers.

Outputs

Name Type Description
runner ChatCompletionRunner<ParsedT> An event-driven runner instance that emits content, message, error, end, and other lifecycle events.

Usage Examples

import OpenAI from 'openai';

const client = new OpenAI();

const runner = client.beta.chat.completions.runTools({
  model: 'gpt-4o',
  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', (content) => {
  console.log('Assistant:', content);
});

const finalCompletion = await runner.finalChatCompletion();

Related Pages

Page Connections

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