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 ChatCompletionCreateParams

From Leeroopedia
Knowledge Sources
Domains NLP, API_Design
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete type definition for constructing Chat Completion request bodies provided by the openai-node SDK.

Description

ChatCompletionCreateParams is a TypeScript type (union of streaming and non-streaming variants) that defines the shape of request bodies for the /chat/completions endpoint. It enforces required fields (messages, model) and provides type-safe optional parameters for controlling generation behavior. The type is a discriminated union keyed on the stream property.

Usage

Use this type when constructing the request body for client.chat.completions.create(), client.chat.completions.stream(), or client.chat.completions.parse(). TypeScript will enforce correct parameter shapes at compile time.

Code Reference

Source Location

  • Repository: openai-node
  • File: src/resources/chat/completions/completions.ts
  • Lines: L1484-1950 (type union and base interface)

Signature

export type ChatCompletionCreateParams =
  | ChatCompletionCreateParamsNonStreaming
  | ChatCompletionCreateParamsStreaming;

export interface ChatCompletionCreateParamsBase {
  messages: Array<ChatCompletionMessageParam>;
  model: string | ChatModel;
  stream?: boolean | null;
  temperature?: number | null;
  max_completion_tokens?: number | null;
  top_p?: number | null;
  n?: number | null;
  stop?: string | null | Array<string>;
  presence_penalty?: number | null;
  frequency_penalty?: number | null;
  tools?: Array<ChatCompletionTool>;
  tool_choice?: ChatCompletionToolChoiceOption;
  response_format?: ResponseFormatText | ResponseFormatJSONObject | ResponseFormatJSONSchema;
  seed?: number | null;
  logprobs?: boolean | null;
  // ... additional optional parameters
}

Import

import { type ChatCompletionCreateParams } from 'openai/resources/chat/completions';

I/O Contract

Inputs

Name Type Required Description
messages Array<ChatCompletionMessageParam> Yes Conversation messages with role and content
model ChatModel Yes Model ID (e.g., 'gpt-4o', 'gpt-4o-mini')
stream boolean No Enable streaming response chunks
temperature number No Sampling temperature (0-2, default 1)
max_completion_tokens number No Maximum tokens to generate
tools Array<ChatCompletionTool> No Tool/function definitions for function calling
response_format ResponseFormat No Output format (text, json_object, json_schema)
tool_choice ChatCompletionToolChoiceOption No Tool selection strategy ('none', 'auto', 'required')

Outputs

Name Type Description
(type only) ChatCompletionCreateParams This is a TypeScript type, not a runtime function. It produces a typed request object used as input to Completions.create().

Usage Examples

Non-Streaming Request

import OpenAI from 'openai';

const client = new OpenAI();

const params: OpenAI.ChatCompletionCreateParams = {
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Hello!' },
  ],
  temperature: 0.7,
  max_completion_tokens: 256,
};

const completion = await client.chat.completions.create(params);

Streaming Request

const stream = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Tell me a story.' }],
  stream: true,
});

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

Related Pages

Implements Principle

Page Connections

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