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 Chat Resource

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

Overview

The Chat resource class serves as a namespace that groups the completions sub-resource for the OpenAI Chat Completions API.

Description

The Chat class extends APIResource and acts as a container for the Completions sub-resource. It does not define any API methods of its own; instead, it instantiates the Completions class and exposes it as a property so that callers access chat functionality through client.chat.completions.

The class defines a ChatModel type alias that references Shared.ChatModel, providing the enumeration of all supported chat model identifiers across the SDK. The file re-exports an extensive set of types from the completions sub-module, including ChatCompletion, ChatCompletionChunk, ChatCompletionMessage, various message parameter types (developer, system, user, assistant, tool, function), tool-related types, audio parameters, streaming options, and pagination types.

This is one of the most commonly used entry points in the SDK, as it provides access to the core chat completions functionality that powers conversational AI interactions.

Usage

Use the Chat resource to access the Chat Completions API. Access it through client.chat.completions to create chat completions, stream responses, or manage stored completions. The ChatModel type can be used for type-safe model selection.

Code Reference

Source Location

Signature

export class Chat extends APIResource {
  completions: CompletionsAPI.Completions;
}

export type ChatModel = Shared.ChatModel;

Import

import OpenAI from 'openai';

I/O Contract

Inputs

The Chat class itself does not accept direct inputs. It delegates to its sub-resource:

Sub-Resource Access Path Description
Completions client.chat.completions Create, retrieve, update, list, and delete chat completions

Outputs

Name Type Description
ChatModel string union Type alias for all supported chat model identifiers
ChatCompletion ChatCompletion Re-exported completion result type
ChatCompletionChunk ChatCompletionChunk Re-exported streaming chunk type
ChatCompletionMessage ChatCompletionMessage Re-exported message type
ChatCompletionCreateParams ChatCompletionCreateParams Re-exported creation parameters

Usage Examples

import OpenAI from 'openai';

const client = new OpenAI();

// Create a chat completion
const completion = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'user', content: 'Hello, how are you?' },
  ],
});
console.log(completion.choices[0].message.content);

// Stream a chat completion
const stream = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'user', content: 'Write a haiku about programming.' },
  ],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

Related Pages

Page Connections

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