AI & LLM
Updated for 2026

OpenAI Node.js & Python API Cheatsheet 2026

Syntaxes and patterns for OpenAI completions, structured outputs, chat streaming, image generation, and model parameters.

Initialization

import OpenAI from 'openai'; const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
Initialize the modern official OpenAI Node.js SDK client with credentials.

When to Use

When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.

Common Mistakes

Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.

Shortcut / Pro-Tip

Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.

Example

import OpenAI from 'openai'; const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

Output Example

Console / Terminal
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ]
}

Chat Completion

const response = await openai.chat.completions.create({ model: 'gpt-4o', messages: [{ role: 'user', content: 'Hello' }] });
Send a standard non-streaming chat request and await the final complete payload.

When to Use

When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.

Common Mistakes

Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.

Shortcut / Pro-Tip

Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.

Example

const response = await openai.chat.completions.create({ model: 'gpt-4o', messages: [{ role: 'user', content: 'Hello' }] });

Output Example

Console / Terminal
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ]
}
temperature: 0.2, max_completion_tokens: 500
Configure core completion parameters (temperature for creativity, tokens for limit).

When to Use

When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.

Common Mistakes

Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.

Shortcut / Pro-Tip

Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.

Example

temperature: 0.2, max_completion_tokens: 500

Output Example

Console / Terminal
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ]
}

Streaming

const stream = await openai.chat.completions.create({ model: 'gpt-4o', messages: [...], stream: true });
Initiate a low-latency streaming chat response from the API server.

When to Use

When engineering conversational user interfaces with immediate, word-by-word streaming rendering.

Common Mistakes

Failing to check if the chunk has choices/deltas before writing content stream segments.

Shortcut / Pro-Tip

Use 'for await...of' loops to iterate naturally through the asynchronous token chunks.

Example

const stream = await openai.chat.completions.create({ model: 'gpt-4o', messages: [...], stream: true });

Output Example

Console / Terminal
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ]
}
for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content || ''); }
Iterate through incoming response stream chunks in real-time.

When to Use

When engineering conversational user interfaces with immediate, word-by-word streaming rendering.

Common Mistakes

Failing to check if the chunk has choices/deltas before writing content stream segments.

Shortcut / Pro-Tip

Use 'for await...of' loops to iterate naturally through the asynchronous token chunks.

Example

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

Output Example

Console / Terminal
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ]
}

Structured Outputs

response_format: { type: 'json_object' }
Enforce a parseable JSON object response (requires mentioning JSON in prompt).

When to Use

When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.

Common Mistakes

Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.

Shortcut / Pro-Tip

Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.

Example

response_format: { type: 'json_object' }

Output Example

Console / Terminal
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ]
}
response_format: zodResponseFormat(MySchema, 'result')
Use Zod schemas to guarantee fully typed, compliant structured outputs (Node.js SDK).

When to Use

When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.

Common Mistakes

Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.

Shortcut / Pro-Tip

Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.

Example

response_format: zodResponseFormat(MySchema, 'result')

Output Example

Console / Terminal
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ]
}

Image & Embeddings

const image = await openai.images.generate({ model: 'dall-e-3', prompt: 'a cute cat' });
Generate vector or realistic art images using the DALL-E-3 system.

When to Use

When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.

Common Mistakes

Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.

Shortcut / Pro-Tip

Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.

Example

const image = await openai.images.generate({ model: 'dall-e-3', prompt: 'a cute cat' });

Output Example

Console / Terminal
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ]
}
const embedding = await openai.embeddings.create({ model: 'text-embedding-3-small', input: 'text' });
Generate a high-dimensional vector representation for search or classification.

When to Use

When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.

Common Mistakes

Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.

Shortcut / Pro-Tip

Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.

Example

const embedding = await openai.embeddings.create({ model: 'text-embedding-3-small', input: 'text' });

Output Example

Console / Terminal
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ]
}

OpenAI API Best Practices

1Use Server-Side Proxies for Keys

Never place your OpenAI API key in client-side code. Always proxy requests through your Express or Node.js server.

2Enforce Structured JSON Outputs

Set the response_format parameter to JSON Object, or leverage Structured Outputs with Zod schemas to ensure perfect parsing compatibility.

3Implement Exponential Backoff Retry Loops

Handle rate limit errors (HTTP 429) gracefully using standard exponential backoff delay retries.

4Keep Track of Token Consumptions

Store and log the usage metrics returned in the API response payload to monitor and budget application costs.

5Leverage System Fingerprints for Cache Verification

Inspect system_fingerprint values to check if backend model updates are altering generation determinism.

Common OpenAI API Errors & Solutions

Error

401 Invalid Authentication

Solution

The API key is incorrect or missing. Check that process.env.OPENAI_API_KEY is defined and loaded.

Error

429 Rate Limit Exceeded

Solution

You have exceeded your request rate or token limits. Implement throttling, use backoff retries, or upgrade your billing tier.

Error

400 Invalid JSON payload

Solution

Your request body contains malformed JSON or invalid parameter structures. Validate schemas against OpenAI API documentation.

Error

Context Length Exceeded

Solution

The combined prompt and max_tokens parameters exceed the model's context window. Prune input history length.

Error

API connection timeout

Solution

The network request to api.openai.com timed out. Increase timeout values in your HTTP client library.

Common OpenAI API Interview Questions

Q1What is the difference between the Chat Completions and Assistants APIs?

Chat Completions is a stateless API for generating text responses from a list of messages. The Assistants API is stateful, maintaining user threads automatically, executing tools (like code interpreter), and reading files.

Q2What are temperature and top_p, and how do they differ?

Both control response creativity. Temperature scales the logits before softmax (higher = more creative). Top_p (nucleus sampling) limits choices to the top cumulative probability fraction (higher = more diverse). Only adjust one at a time.

Q3How do you ensure structured JSON output from OpenAI?

You can set response_format to { type: 'json_object' } (requiring the word 'JSON' in the prompt), or use Structured Outputs (response_format with a strict JSON Schema) to guarantee 100% adherence.

Q4What are token limits and how are they counted?

Token limits apply to both input (prompt) and output (completion). Tokens are word fragments (~4 characters). Every API model has a specific maximum limit (e.g. 128k for gpt-4o).

Q5Why is streaming useful in LLM APIs and how is it implemented?

Streaming returns model output chunk-by-chunk in real-time, reducing perceived latency. It is implemented via Server-Sent Events (SSE) by setting the 'stream' parameter to true in the API call.