Skip to content

OpenAI Provider ​

The OpenAI provider supports the full range of OpenAI's API capabilities, through both Chat Completions and the Responses API.

Installation ​

bash
npm install @aeye/openai openai

Requires Node >= 22 (the openai SDK v7 needs it).

Configuration ​

typescript
import { OpenAIProvider } from '@aeye/openai';

const openai = new OpenAIProvider({
  apiKey: process.env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1',  // optional, default
  organization: 'org-xxx',                // optional
  project: 'proj-xxx',                    // optional
  store: true,                            // optional, allow storage (default false)
  api: 'auto',                            // optional: 'auto' | 'chat' | 'responses'
  retry: {
    maxRetries: 3,
    initialDelay: 1000,
    maxDelay: 30000,
    backoffMultiplier: 2,
    jitter: true,
    retryableStatuses: [429, 500, 502, 503, 504],
    timeout: 60000,
  },
  defaultModels: {
    chat: 'gpt-5.4-mini',
    imageGenerate: 'gpt-image-2',          // default
    imageEdit: 'gpt-image-2',              // default
    imageAnalyze: 'gpt-5.4-mini',          // default
    transcription: 'gpt-transcribe',       // default
    speech: 'gpt-4o-mini-tts',             // default
    embedding: 'text-embedding-3-small',   // default
    decision: 'gpt-6-luna',                // default
  },
  hooks: {
    chat: {
      beforeRequest: (request, params, ctx, metadata) => {},
      afterRequest: (request, params, response, ctx, metadata) => {},
      onError: (request, params, error, ctx, metadata) => {},
    },
  },
});

Hooks exist for chat, responses, imageGenerate, imageEdit, transcribe, speech, embed, and decide. Image analysis runs as chat and reports through the chat (or responses) hooks.

Chat Completions or Responses ​

With api: 'auto' (the default), chat requests use Chat Completions unless they need the Responses API:

  • the model is Responses-only (its registry entry's metadata.responsesOnly, or by id: codex, pre-5.6 pro models, deep-research, computer-use),
  • the request has hostedTools,
  • a tool sets format, defer, or async (see Tool Features),
  • reason sets mode, summary, or context, or
  • the request has tools and a reasoning effort other than none (Chat Completions drops reasoning between tool calls, and gpt-5.4 and later reject function tools with reasoning there).

'chat' or 'responses' forces one API. Through Responses, reasoning comes back encrypted (unless store is on) and is replayed on later turns, so multi-turn reasoning works without stored responses. Only reasoning OpenAI produced (reasoning.provider 'openai', or unset) is replayed: reasoning relayed by OpenRouter or made on Bedrock is left out, since OpenAI can't decrypt it.

Capabilities ​

CapabilityModelsAPI Method
ChatGPT-5.x, GPT-6, GPT-4.1ai.chat
VisionGPT-5.x, GPT-6, GPT-4.1ai.chat with images, ai.image.analyze
ReasoningGPT-5.x, GPT-6, o3ai.chat with reason
ToolsGPT-5.x, GPT-6, GPT-4.1ai.chat with tools
Hosted toolsResponses modelsai.chat with hostedTools
Structured OutputGPT-5.x, GPT-6, GPT-4.1ai.chat with schema
Audio outputgpt-audioai.chat with modalities: ['text', 'audio']
Image GenerationGPT Image (gpt-image-2)ai.image.generate
Image EditingGPT Image (gpt-image-2)ai.image.edit
Speech (TTS)gpt-4o-mini-tts, tts-1ai.speech
Transcriptiongpt-transcribe, gpt-4o-transcribe-diarizeai.transcribe
Embeddingstext-embedding-3-*ai.embed
Decisionsgpt-6-lunaai.decide
BatchesChat and Responses modelsai.batch

Request Features ​

Beyond the common chat fields, OpenAI sends:

Request fieldSent as
reason.effort (none..max)reasoning_effort, clamped to the model's accepted efforts when the registry lists them
seed, verbosity, serviceTier, metadataseed, verbosity, service_tier, metadata (Chat Completions sends metadata only when store is on)
modalities + audioaudio output ({ voice, format }; format defaults to mp3, or PCM when streaming)
moderate: trueinline moderation with omni-moderation-latest; verdicts come back as response.moderation
hostedToolsResponses tools: web_search (allowedDomains; blockedDomains is rejected), file_search, code_interpreter, image_generation, mcp. web_fetch is rejected.
cacheBreakpoint on contenta prompt cache breakpoint
toolChoice: { allowed, required? }allowed_tools (mode required or auto), on both APIs
toolsOneAtATime: trueparallel_tool_calls: false. Otherwise Chat Completions leaves the parameter out (parallel calls are the default, and o3 rejects the parameter); Responses sends it with any tools
responseFormat (schema)json_schema, with strict: true when the schema fits OpenAI's strict dialect. A non-strict schema is marked strict: false on Responses, which treats an unflagged schema as strict
detail / name on contentimage detail / file name

Responses carry citations, images, audio, moderation, and serviceTier when present, and usage.tools counts hosted tool calls.

An assistant turn of only tool calls is sent with no content rather than an empty text part, which some OpenAI-compatible upstreams reject.

Tool Features ​

Tool optionSent as
format: { type: 'text' }a Responses custom tool; the text comes back as { input } arguments
format: { type: 'grammar', syntax, definition }a Responses custom tool constrained by the Lark or regex grammar. On Chat Completions (api: 'chat') it is a one-field function whose description carries the grammar.
defer: truedefer_loading on the tool, plus the hosted tool_search tool
async: truethe Responses async flag

Custom tool calls stream as tool call arguments and replay on later turns as custom_tool_call items. See Tool Calling.

Decisions ​

ai.decide calls /v1/decisions (default model gpt-6-luna). A conversation input is sent as Responses input; choices and levels go as { value, description } and { label } lists, and answers come back keyed by question name. Usage is priced from the model's token pricing. See Decisions.

typescript
const { answers } = await ai.decide.get({
  input: 'Cancel my subscription right now.',
  questions: { churn: { type: 'predicate', instructions: 'Does the user want to cancel?' } },
});

Batches ​

ai.batch.submit uploads the requests as a JSONL file (purpose: 'batch') and creates a batch with a 24 hour completion window, billed at about half the normal price. The requests go to /v1/chat/completions, or to /v1/responses when any of them needs the Responses API. Once the batch completes, ai.batch.get reads its output and error files and returns the results in request order. See Batches.

Usage ​

typescript
const ai = AI.with()
  .providers({ openai })
  .create();

// Chat
const response = await ai.chat.get({
  messages: [{ role: 'user', content: 'Hello' }],
});

// With vision
const response = await ai.chat.get({
  messages: [{
    role: 'user',
    content: [
      { type: 'text', content: 'Describe this image' },
      { type: 'image', content: 'https://example.com/img.png', detail: 'high' },
    ],
  }],
});

// Web search (uses the Responses API)
const answer = await ai.chat.get({
  messages: [{ role: 'user', content: 'What changed in the latest Node release?' }],
  hostedTools: [{ type: 'web_search' }],
});
console.log(answer.citations);

// Image generation
const img = await ai.image.generate.get({
  prompt: 'A sunset over mountains',
  size: '1024x1024',
  quality: 'high',
  outputFormat: 'webp',
  outputCompression: 80,
});

Error Types ​

typescript
import {
  ProviderError,
  ProviderAuthError,
  RateLimitError,
  ProviderRateLimitError,
  ProviderQuotaError,
  ContextWindowError,
} from '@aeye/openai';

As Base Class ​

OpenAIProvider can be extended for OpenAI-compatible APIs:

typescript
class AzureProvider extends OpenAIProvider {
  readonly name = 'azure';
  // customize as needed, e.g. override the protected `defaults` model ids
}

Subclasses can add HTTP headers to Chat Completions requests by overriding the protected chatHeaders(params), and run their own operations through the provider hooks with the protected send and finish; OpenRouterProvider uses both.

See Custom Providers for details.

Released under the GPL-3.0 License.