OpenAI API
Every handle of the OpenAI fake: the openai fixture, the scripts a test answers the model's calls with, and the calls it reads and waits for. How to script the model, step by step, is in the OpenAI guide.
Each call is shown in every SDK's language, with its required options (the table under it lists them all). Arguments go first; options are TypeScript's trailing object, Python's keyword arguments and Rust's builder setters. The variable names are the ones the calls return elsewhere on these pages, so user is a OpenAIUser handle.
Besides the errors listed with each method, most methods can fail with action_requires, cross_workspace_credential, fake_actor_in_live_world, internal, invalid_argument, live_misconfigured, live_not_allowlisted, not_found, session_not_found, unauthorized, unsupported_in_live: a bad request, a closed session, or live mode refusing or misconfigured.
OpenAIFixture#
Returned by Session.openai().
| Field | Python / Rust | Type |
|---|---|---|
baseUrl | base_url | string |
respond()#
Script what the model answers: turns used in order, one per matching call, or a responder the test answers. Scripts are tried in the order they were added.
const script = await openai.respond({ when });script = await openai.respond(when=when)let script = openai.respond().when(when).await?;| Parameter | Type | Required | |
|---|---|---|---|
turns | OpenAITurnInput | OpenAITurnInput[] | no | positional |
when | OpenAIMatch | no | option |
times | number | no | option |
timing | OpenAITiming | no | option |
responder | boolean | no | option |
Returns OpenAIScript.
calls()#
Every model call attributed to this session, oldest first.
const sessionCalls = await openai.calls({ endpoint });session_calls = await openai.calls(endpoint=endpoint)let session_calls = openai.calls().endpoint(endpoint).await?;| Parameter | Type | Required | |
|---|---|---|---|
endpoint | OpenAIEndpoint | no | option |
error | true | string | no | option |
Returns OpenAISessionCalls.
toolCalls()#
Every tool call the model made in this session, each with the result the agent sent back.
const sessionToolCalls = await openai.toolCalls({ name: "…" });session_tool_calls = await openai.tool_calls(name="…")let session_tool_calls = openai.tool_calls().name("…").await?;| Parameter | Type | Required | |
|---|---|---|---|
name | string | no | option |
Returns OpenAISessionToolCalls.
problems()#
What went wrong with the model in this session: a call no script answered, a script that ran out or did what the real API can't, a responder that threw. Matchers fail at once on these.
const problems = await openai.problems();problems = await openai.problems()let problems = openai.problems().await?;Returns OpenAIProblems.
sentSecrets()#
The calls that sent a secret channelstack knows to the model, and the kinds they sent (slack.botToken): never the values.
const sentSecrets = await openai.sentSecrets();sent_secrets = await openai.sent_secrets()let sent_secrets = openai.sent_secrets().await?;Returns OpenAISentSecrets.
unattributed()#
Model calls no session could be given (the agent calling the model at startup, for example), oldest first: what a failing matcher names.
const unattributedCalls = await openai.unattributed({ withinMs });unattributed_calls = await openai.unattributed(within_ms=within_ms)let unattributed_calls = openai.unattributed().within_ms(within_ms).await?;| Parameter | Python / Rust | Type | Required | |
|---|---|---|---|---|
withinMs | within_ms | number | undefined | no | option |
Returns OpenAIUnattributedCalls.
rateLimit()#
Limit this session's model calls: requests and tokens per window, with truthful x-ratelimit-* headers.
const rateLimit = await openai.rateLimit({ requests });rate_limit = await openai.rate_limit(requests=requests)let rate_limit = openai.rate_limit().requests(requests).await?;| Parameter | Type | Required | |
|---|---|---|---|
requests | { limit: number; per?: Duration } | no | option |
tokens | { limit: number; per?: Duration } | no | option |
error | "rate_limit" | "slow_down" | no | option |
Returns OpenAIRateLimit.
waitForCall()#
Waits for the next model call from the agent after the last one this handle returned: it polls calls() and never sleeps longer than it must.
const call = await openai.waitForCall({ endpoint });call = await openai.wait_for_call(endpoint=endpoint)let call = openai.wait_for_call().endpoint(endpoint).await?;| Parameter | Type | Required | |
|---|---|---|---|
endpoint | OpenAIEndpoint | no | filter |
error | true | string | no | filter |
timeout | Duration | no | default 10s |
Returns OpenAICall. When the time is up, it fails with what it was waiting for.
latestCall()#
The latest model call from the agent, once the app is quiet. Waits for at least one: it polls calls() and never sleeps longer than it must.
const call = await openai.latestCall({ endpoint });call = await openai.latest_call(endpoint=endpoint)let call = openai.latest_call().endpoint(endpoint).await?;| Parameter | Python / Rust | Type | Required | |
|---|---|---|---|---|
endpoint | endpoint | OpenAIEndpoint | no | filter |
error | error | true | string | no | filter |
timeout | timeout | Duration | no | default 10s |
quietFor | quiet_for | Duration | no | default 750ms |
Returns OpenAICall. When the time is up, it fails with what it was waiting for.
OpenAIScript#
Returned by OpenAIFixture.respond(), OpenAIScript.refresh().
| Field | Type |
|---|---|
id | string |
responder | boolean |
times | number |
used | number |
problems | string[] |
refresh()#
Re-read a script: how many calls it answered, and its problems.
const updated = await script.refresh();updated = await script.refresh()let updated = script.refresh().await?;Returns OpenAIScript.
undo()#
Remove a script. Closing the session does it too.
await script.undo();await script.undo()script.undo().await?;Returns nothing. Disposing the handle calls it.
resume()#
Let the streams this script holds at pauseAfter go on.
await script.resume();await script.resume()script.resume().await?;Returns nothing.
calls()#
The calls this script answered.
const calls = await script.calls();calls = await script.calls()let calls = script.calls().await?;Returns OpenAICalls.
pausedCalls()#
The calls whose streams this script holds at pauseAfter.
const pausedCalls = await script.pausedCalls();paused_calls = await script.paused_calls()let paused_calls = script.paused_calls().await?;Returns OpenAIPausedCalls.
toolCalls()#
The tool calls this script made, each with the result the agent sent back.
const toolCalls = await script.toolCalls({ name: "…" });tool_calls = await script.tool_calls(name="…")let tool_calls = script.tool_calls().name("…").await?;| Parameter | Type | Required | |
|---|---|---|---|
name | string | no | option |
Returns OpenAIToolCalls.
heldCalls()#
For a responder's SDK: the calls held for it since since, waiting up to waitMs for one.
const heldPage = await script.heldCalls({ since: "…" });held_page = await script.held_calls(since="…")let held_page = script.held_calls().since("…").await?;| Parameter | Python / Rust | Type | Required | |
|---|---|---|---|---|
since | since | string | no | option |
waitMs | wait_ms | number | undefined | no | option |
Returns OpenAIHeldPage.
answerHeld()#
For a responder's SDK: answer a held call with a turn, or report that the test's function threw.
await script.answerHeld("…", { turn });await script.answer_held("…", turn=turn)script.answer_held("…").turn(turn).await?;| Parameter | Type | Required | |
|---|---|---|---|
held | string | yes | positional |
turn | OpenAITurnInput | no | option |
error | string | no | option |
Returns nothing.
waitForCall()#
Waits for the next model call this script answers after the last one this handle returned: it polls calls() and never sleeps longer than it must.
const call = await script.waitForCall();call = await script.wait_for_call()let call = script.wait_for_call().await?;| Parameter | Type | Required | |
|---|---|---|---|
timeout | Duration | no | default 10s |
Returns OpenAICall. When the time is up, it fails with what it was waiting for.
waitForPause()#
Waits for the next stream held at pauseAfter after the last one this handle returned: it polls pausedCalls() and never sleeps longer than it must.
const call = await script.waitForPause();call = await script.wait_for_pause()let call = script.wait_for_pause().await?;| Parameter | Type | Required | |
|---|---|---|---|
timeout | Duration | no | default 10s |
Returns OpenAICall. When the time is up, it fails with what it was waiting for.
OpenAICall#
Returned by OpenAICall.refresh().
| Field | Python / Rust | Type |
|---|---|---|
id | id | string |
endpoint | endpoint | OpenAIEndpoint |
model | model | string |
stream | stream | boolean |
at | at | string |
durationMs | duration_ms | number |
attempt | attempt | number |
done | done | boolean |
request | request | OpenAIRequest |
response | response | { status: number; text?: string; finish?: string; error?: string; aborted: boolean; chunksSent?: number; end?: "completed" | "errored" | "dropped" | "stalled" | "aborted" } |
toolCalls | tool_calls | OpenAIToolCall[] |
script | script | string | undefined |
refresh()#
Re-read a model call: whether it is done, how it ended.
const updated = await call.refresh();updated = await call.refresh()let updated = call.refresh().await?;Returns OpenAICall.
OpenAIRateLimit#
Returned by OpenAIFixture.rateLimit(), OpenAIRateLimit.remaining().
| Field | Python / Rust | Type |
|---|---|---|
id | id | string |
requests | requests | number | undefined |
tokens | tokens | number | undefined |
resetsInMs | resets_in_ms | number |
remaining()#
What the rate limit's headers would say now.
const updated = await rateLimit.remaining();updated = await rate_limit.remaining()let updated = rate_limit.remaining().await?;Returns OpenAIRateLimit.
undo()#
Remove the rate limit.
await rateLimit.undo();await rate_limit.undo()rate_limit.undo().await?;Returns nothing. Disposing the handle calls it.
Types#
Plain values the handles return and take.
OpenAITurnInput#
type OpenAITurnInput = string | OpenAITurn;
OpenAITurn#
| Field | Python / Rust | Type |
|---|---|---|
text | text | string | string[] | undefined |
toolCalls | tool_calls | OpenAIToolCallSpec[] | undefined |
citations | citations | Array<{ quote: string; url?: string; title?: string; file?: string }> | undefined |
image | image | OpenAIMediaSource | OpenAIMediaSource[] | undefined |
audio | audio | OpenAIMediaSource | undefined |
reasoning | reasoning | { summary?: string | string[]; tokens?: number } | undefined |
refusal | refusal | string | undefined |
finish | finish | "stop" | "length" | "content_filter" | undefined |
usage | usage | { input?: number; output?: number; reasoning?: number; cached?: number } | undefined |
timing | timing | OpenAITiming | undefined |
failAfter | fail_after | { chunks: number; how?: "error" | "drop" | "stall" } | undefined |
pauseAfter | pause_after | number | undefined |
error | error | "rate_limit" | "slow_down" | "insufficient_quota" | "credit_balance_exhausted" | "organization_spend_limit_exceeded" | "project_spend_limit_exceeded" | "organization_usage_limit_exceeded" | "server_error" | "overloaded" | "timeout" | "context_length_exceeded" | "content_policy" | "invalid_api_key" | "not_in_organization" | "ip_not_authorized" | "unsupported_region" | undefined |
retryAfter | retry_after | number | string | undefined |
shouldRetry | should_retry | boolean | undefined |
message | message | string | undefined |
OpenAIToolCallSpec#
| Field | Python / Rust | Type |
|---|---|---|
name | name | string | undefined |
arguments | arguments | Record<string, unknown> | string | undefined |
input | input | string | undefined |
webSearch | web_search | { query: string; action?: "search" | "open_page" | "find_in_page"; url?: string; pattern?: string; sources?: Array<{ url: string; title?: string }> } | undefined |
fileSearch | file_search | { queries: string[]; results?: Array<{ file: string; text: string; score?: number }> } | undefined |
codeInterpreter | code_interpreter | { code: string; logs?: string; files?: OpenAIMediaSource[] } | undefined |
imageGeneration | image_generation | { image: OpenAIMediaSource; revisedPrompt?: string } | undefined |
mcp | mcp | { server: string; tool: string; arguments?: Record<string, unknown>; output?: string; error?: string; approval?: boolean } | undefined |
shell | shell | { commands: string[]; output?: string } | undefined |
computer | computer | { actions: OpenAIComputerAction[] } | undefined |
applyPatch | apply_patch | { operations: OpenAIPatchOperation[] } | undefined |
OpenAIMediaSource#
type OpenAIMediaSource = MediaSource;
OpenAIComputerAction#
| Field | Python / Rust | Type |
|---|---|---|
type | type · r#type | string |
OpenAIPatchOperation#
| Field | Python / Rust | Type |
|---|---|---|
type | type · r#type | "create_file" | "update_file" | "delete_file" |
path | path | string |
diff | diff | string | undefined |
OpenAITiming#
| Field | Python / Rust | Type |
|---|---|---|
firstToken | first_token | number | string | undefined |
betweenChunks | between_chunks | number | string | undefined |
OpenAIMatch#
| Field | Python / Rust | Type |
|---|---|---|
text | text | string | RegExp | undefined |
prompt | prompt | string | RegExp | undefined |
tools | tools | string[] | undefined |
toolResult | tool_result | boolean | string | undefined |
endpoint | endpoint | "chat.completions" | "responses" | "images.generations" | "images.edits" | "audio.speech" | "audio.transcriptions" | "audio.translations" | undefined |
model | model | string | RegExp | undefined |
stream | stream | boolean | undefined |
from | from_ · from | SlackUser | GitHubUser | LinearUser | NotionUser | ResendUser | GoogleUser | DiscordUser | undefined |
in | in_ · r#in | SlackConversation | GitHubRepo | LinearTeam | NotionPage | NotionDatabase | ResendThread | GoogleThread | DiscordChannel | undefined |
approval | approval | boolean | undefined |
OpenAICalls#
type OpenAICalls = OpenAICall[];
OpenAIEndpoint#
type OpenAIEndpoint = "chat.completions" | "responses" | "images.generations" | "images.edits" | "audio.speech" | "audio.transcriptions" | "audio.translations";
OpenAIRequest#
| Field | Python / Rust | Type |
|---|---|---|
endpoint | endpoint | OpenAIEndpoint |
model | model | string |
stream | stream | boolean |
prompt | prompt | string | undefined |
file | file | OpenAIPart | undefined |
instructions | instructions | string | undefined |
messages | messages | OpenAIMessage[] |
tools | tools | OpenAITool[] |
toolChoice | tool_choice | unknown | undefined |
responseFormat | response_format | unknown | undefined |
lastUserText | last_user_text | string | undefined |
lastToolResults | last_tool_results | Array<{ callId: string; name?: string; text: string }> |
text | text | string |
context | context | OpenAIContext |
body | body | unknown | undefined |
headers | headers | Record<string, string> |
OpenAIPart#
| Field | Python / Rust | Type |
|---|---|---|
type | type · r#type | "image" | "file" | "audio" |
from | from_ · from | "data" | "file_id" | "url" |
url | url | string | undefined |
fileId | file_id | string | undefined |
mediaType | media_type | string | undefined |
bytes | bytes | number | undefined |
sha256 | sha256 | string | undefined |
filename | filename | string | undefined |
OpenAIMessage#
| Field | Python / Rust | Type |
|---|---|---|
role | role | "system" | "developer" | "user" | "assistant" | "tool" |
text | text | string |
toolCalls | tool_calls | Array<{ id: string; name: string; arguments?: unknown }> | undefined |
callId | call_id | string | undefined |
name | name | string | undefined |
parts | parts | OpenAIPart[] | undefined |
OpenAITool#
| Field | Python / Rust | Type |
|---|---|---|
type | type · r#type | string |
name | name | string | undefined |
description | description | string | undefined |
parameters | parameters | unknown | undefined |
strict | strict | boolean | undefined |
settings | settings | Record<string, unknown> | undefined |
OpenAIContext#
| Field | Python / Rust | Type |
|---|---|---|
channel | channel | string | undefined |
from | from_ · from | OpenAIRef | undefined |
conversation | conversation | OpenAIRef | undefined |
thread | thread | OpenAIRef | undefined |
message | message | OpenAIRef | undefined |
attributedBy | attributed_by | "minted_id" | "tag" | "owned_id" | "text" | "order" | undefined |
matched | matched | string | undefined |
tiedWith | tied_with | string[] | undefined |
OpenAIRef#
| Field | Type |
|---|---|
id | string |
OpenAIToolCall#
| Field | Python / Rust | Type |
|---|---|---|
id | id | string |
type | type · r#type | "function" | "custom" | "web_search" | "file_search" | "code_interpreter" | "image_generation" | "mcp" | "shell" | "computer" | "apply_patch" |
hosted | hosted | boolean |
name | name | string |
arguments | arguments | unknown | undefined |
madeIn | made_in | string |
result | result | { text: string; sentIn: string } | undefined |
OpenAIPausedCalls#
type OpenAIPausedCalls = OpenAICall[];
OpenAIToolCalls#
type OpenAIToolCalls = OpenAIToolCall[];
OpenAIHeldPage#
| Field | Type |
|---|---|
held | OpenAIHeldCall[] |
cursor | string |
OpenAIHeldCall#
| Field | Type |
|---|---|
id | string |
request | OpenAIRequest |
OpenAISessionCalls#
type OpenAISessionCalls = OpenAICall[];
OpenAISessionToolCalls#
type OpenAISessionToolCalls = OpenAIToolCall[];
OpenAIProblems#
type OpenAIProblems = string[];
OpenAISentSecrets#
type OpenAISentSecrets = OpenAISentSecret[];
OpenAISentSecret#
| Field | Type |
|---|---|
call | string |
kinds | string[] |
OpenAIUnattributedCalls#
type OpenAIUnattributedCalls = OpenAIUnattributedCall[];
OpenAIUnattributedCall#
| Field | Python / Rust | Type |
|---|---|---|
id | id | string |
endpoint | endpoint | OpenAIEndpoint |
model | model | string |
at | at | string |
lastUserText | last_user_text | string | undefined |
tools | tools | string[] |
sinceMs | since_ms | number |
message | message | string |