zidane/ providers/ openai
The OpenAI provider on the Responses API, with API keys or ChatGPT (Codex) OAuth credentials.
import { … } from 'zidane/providers/openai'src/providers/openai.ts
Use openai() for OpenAI models on the Responses API. Pass a platform key as apiKey. With no key, it runs on OPENAI_CODEX_API_KEY, then on the stored Codex OAuth sign-in. It does not run on OPENAI_API_KEY. The agent's session id is sent as prompt_cache_key, so a conversation keeps hitting the same prompt cache. For the Chat Completions API, use openaiCompat(). See Providers.
import { createAgent } from 'zidane'
import { openai } from 'zidane/providers/openai'
const agent = createAgent({ provider: openai({ defaultModel: 'gpt-6-astra' }) })
const stats = await agent.run({ prompt: 'Summarize README.md in three bullets.' })
console.log(stats.turns)Functions#
classifyOpenAIErrorFunction#
Classify an OpenAI Codex error. pi-ai surfaces errors either as thrown Errors
(wrapping event.error.errorMessage) or via stream event types.
The retryable hint is set on a transient failure — pattern-matched against
message since pi-ai strips structured fields on plain-Error throws. Set behavior.retry.maxAttempts: 1
to disable the loop-level retry.
function classifyOpenAIError(err: unknown): ClassifiedError | nullParameters
| Name | Type | Description |
|---|---|---|
| err | unknown | Error thrown by pi-ai or the HTTP layer. |
Returns
ClassifiedError | nullmapCodexStopReasonFunction#
Map pi-ai's StopReason to the zidane TurnFinishReason union — the same
native-stop-reason fidelity anthropic's mapStopReason / openai-compat's
mapOAIFinishReason provide. length (max-token truncation) maps to
'length', not to a clean stop. hasToolCalls is the fallback for
messages whose stop reason is missing or doesn't carry the tool-call signal.
function mapCodexStopReason(
stopReason: string | undefined,
hasToolCalls: boolean,
): TurnFinishReasonParameters
| Name | Type | Description |
|---|---|---|
| stopReason | string | undefined | pi-ai stop reason, when present. |
| hasToolCalls | boolean | Whether the turn contains tool calls. |
Returns
openaiFunction#
Creates the OpenAI provider on the Responses API.
Picks the backend per request from the credential: a Codex OAuth token streams through the ChatGPT Codex backend, a platform API key (sk-…) through the standard Responses API.
A run reads params.apiKey, then OPENAI_CODEX_API_KEY, then the stored OAuth credentials. OPENAI_API_KEY is read only by the exact token count (countTokens) and by the decisions helper, together with params.apiKey and OPENAI_CODEX_API_KEY.
function openai(params?: OpenAIParams): ProviderParameters
| Name | Type | Description |
|---|---|---|
| params? | OpenAIParams | API key or OAuth credentials, account id, default model, transport and extra headers. |
Returns
Example
import { createAgent } from 'zidane'
import { openai } from 'zidane/providers/openai'
const agent = createAgent({
provider: openai({ apiKey: process.env.OPENAI_API_KEY }),
})
await agent.run({ prompt: 'Say hello.' })toPiMessagesFunction#
Converts session messages to pi-ai messages for the Responses wire, pairing tool calls with their results.
function toPiMessages(messages: SessionMessage[], modelId: string): Message[]Parameters
| Name | Type | Description |
|---|---|---|
| messages | SessionMessage[] | Session messages to convert. |
| modelId | string | Model id stamped on converted assistant messages. |
Returns
Message[]Interfaces#
OpenAIParamsInterface#
Options for openai: an API key or ChatGPT OAuth credentials, plus model and request settings.
interface OpenAIParams extends OAuthParamsProperties
| Name | Type | Default | Description |
|---|---|---|---|
access?from OAuthParams | string | OAuth access token supplied at runtime. Used together with | |
account?from OAuthParams | string | Run on ONE stored account of the provider instead of its active one: the
credential's stable id ( | |
| accountId? | string | ChatGPT account id sent with runtime OAuth credentials. Normally derived from the access token. | |
apiKey?from OAuthParams | string | Static API key or OAuth access token sent as is, bypassing credential lookup and refresh. | |
| defaultModel? | string | 'gpt-6-astra' | Model used when a request does not set one. |
expires?from OAuthParams | number | Expiry of | |
| extraHeaders? | Record<string, string> | Extra HTTP headers merged into every request (the streaming Responses call
and the token-count endpoint). Use for routing/attribution headers a proxy
in front of the OpenAI Codex endpoint consumes. Merged with provider
defaults; pi-ai lets these override defaults, so avoid | |
refresh?from OAuthParams | string | OAuth refresh token supplied at runtime. | |
| transport? | 'auto' | 'sse' | 'websocket' | Transport for the Codex backend: |