Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Function

createOpenAI

createOpenAI creates a customized OpenAI provider for the AI SDK. Configure credentials, endpoint, headers, or transport, then use the provider to create OpenAI models.

How it works

createOpenAI builds an OpenAI provider instance from optional settings such as credentials, endpoint, headers, and fetch implementation. The resulting provider is callable with a model ID and also exposes provider-specific model factories such as chat when needed.

Syntax

createOpenAI(options?: { apiKey?: string; baseURL?: string; headers?: Record<string, string>; fetch?: typeof fetch }): OpenAIProvider

Parameters

NameTypeDescription
optionsobjectOptional provider configuration, including baseURL, apiKey, headers, and fetch.

Returns

A configured OpenAI provider instance that can create models from model IDs.

Key points

  • Import createOpenAI from @ai-sdk/openai when the default provider instance is not sufficient.
  • Set baseURL to route requests through a proxy or another OpenAI-compatible endpoint.
  • Without an explicit base URL, resolution falls back first to OPENAI_BASE_URL and then to the documented OpenAI endpoint.
  • The configured API key is transmitted through the Authorization header.
  • If no key is passed in settings, the provider reads OPENAI_API_KEY.
  • Factory settings can include a custom fetch implementation as well as a custom base URL.

Common mistakes

  • The default factory targets OpenAI's Responses API; use the chat factory when the endpoint only supports Chat Completions.
  • A custom endpoint that lacks Responses API support requires openai.chat('model-id') rather than the default model factory.

Example: Create a configured OpenAI provider

import { createOpenAI } from '@ai-sdk/openai';

const openai = createOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

const model = openai('gpt-5');

Creates a configured OpenAI provider and then obtains a model instance by model ID.

When to use it

Custom endpoint routing

Requests must pass through an internal gateway or OpenAI-compatible proxy.

Per-instance authentication

Different deployments need separate credentials or request headers.

Custom transport

A runtime needs instrumentation, retries, or a WebSocket-backed request implementation.

Interview questions

QWhat does createOpenAI provide?

It returns a configured OpenAI provider instance. Configuration can override the API key, base URL, headers, and fetch implementation, while unspecified credentials and the standard endpoint can come from environment variables or defaults.

Follow-up: How would you target a proxy that implements only Chat Completions? Use baseURL for the proxy and create the model through the provider's chat factory.

QHow is a provider created and then used?

The factory is imported from @ai-sdk/openai and accepts an optional settings object. The returned provider can be called with a model ID, for example openai('model-id').

Follow-up: How can the provider use a custom transport? Supply a custom fetch function in the factory settings.

QWhat are the credential and endpoint defaults?

The API key setting is sent in the Authorization header. If omitted, the provider uses OPENAI_API_KEY; the base URL similarly defaults through OPENAI_BASE_URL and then the standard OpenAI v1 endpoint.

Follow-up: What should be checked when a proxy behaves unexpectedly? Verify both the configured base URL and whether it supports the API selected by the model factory.

  • openai
  • openai.chat
  • @ai-sdk/openai
  • streamText
  • generateText

Source: AI SDK Providers: OpenAI. Checked 2026-10-15.