codex-sdk-go
CategoryArtificial Intelligence
SubcategoryArtificial Intelligence
Stars25
Go SDK for the OpenAI Codex app-server with streaming, approval callbacks, and structured output
About codex-sdk-go
This large README is shown as a plain-text preview. Read the full README at the source
# Codex Go SDK
[](https://pkg.go.dev/github.com/pmenglund/codex-sdk-go)
[](https://codecov.io/gh/pmenglund/codex-sdk-go)
Embed the Codex app-server in Go workflows.
This SDK speaks JSON-RPC to the `codex app-server` process. By default it spawns the CLI and communicates over stdio.
## Requirements
- Go 1.26.11 or newer (use 1.27.4 or newer on the Go 1.27 branch)
- `codex` available on your `PATH`
## Install
```bash
go get github.com/pmenglund/codex-sdk-go
```
## Quickstart
```go
package main
import (
"context"
"fmt"
"log/slog"
"os"
"github.com/pmenglund/codex-sdk-go"
)
func main() {
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo}))
prompt := "Diagnose the test failure and propose a fix"
client, err := codex.New(ctx, codex.Options{Logger: logger})
if err != nil {
panic(err)
}
defer client.Close()
thread, err := client.StartThread(ctx, codex.ThreadStartOptions{})
if err != nil {
panic(err)
}
result, err := thread.Run(ctx, prompt, nil)
if err != nil {
panic(err)
}
fmt.Println(result.FinalResponse)
}
```
`New` uses its `context.Context` for initialization requests (`initialize`/`initialized`).
After `New` returns successfully, the spawned app-server lifetime is managed by `Close`, so canceling the constructor context later does not terminate the process.
## Streaming
Use `RunStreamed` to receive notifications as the turn progresses.
```go
prompt := "Inspect the repo"
stream, err := thread.RunStreamed(ctx, []codex.Input{codex.TextInput(prompt)}, nil)
if err != nil {
panic(err)
}
defer stream.Close()
for {
note, err := stream.Next(ctx)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", note.Method)
if note.Method == "turn/failed" {
panic("turn failed")
}
if note.Method == "turn/completed" {
if completed, ok := note.Params.(protocol.TurnCompletedNotification); ok &&
completed.Turn != nil && completed.Turn.Status == protocol.TurnStatusFailed {
panic(fmt.Errorf("turn failed: %v", completed.Turn.Error))
}
break
}
}
```
`RunStreamed` returns thread-scoped events plus notifications that omit `threadId` (for example account/session updates) so global events are not silently dropped.
## Turn handles
Use `StartTurn` when you need to steer or interrupt a running turn.
```go
handle, err := thread.StartTurn(ctx, []codex.Input{codex.TextInput("Inspect the repo")}, nil)
if err != nil {
panic(err)
}
if _, err := handle.Steer(ctx, []codex.Input{codex.TextInput("Focus on tests")}); err != nil {
panic(err)
}
result, err := handle.Run(ctx)
if err != nil {
panic(err)
}
fmt.Println(result.FinalResponse)
```
`TurnHandle` owns its notification subscription. Call `Close` if you stop before `Run` returns.
Choose exactly one consumption style per handle: `Run`, repeated `Next` calls,
or the stream returned by `Stream`. Mixing styles or consuming concurrently
returns `codex.ErrTurnConsumptionMode` immediately.
If a turn fails, `Run` returns both the partial result and a `*codex.TurnError`.
Use `errors.Is(err, codex.ErrTurnFailed)` to classify the failure and
`errors.As` to inspect its structured details, retry metadata, and raw payload:
```go
result, err := handle.Run(ctx)
if errors.Is(err, codex.ErrTurnFailed) {
var turnErr *codex.TurnError
if errors.As(err, &turnErr) {
fmt.Printf("received %d items before failure: %v\n", len(result.Items), turnErr.Detail)
}
}
```
`FinalResponse` is populated only by completed `agentMessage` items. Plan,
reasoning, and commentary items remain available in `Items` but are never
reported as the final assistant answer.
Canceling or expiring the context passed to `Run`, `RunInputs`, or
`TurnHandle.Run` best-effort interrupts the remote turn before returning the
original context error. Cleanup is bounded to two seconds. To detach without
interrupting remote work, use `RunStreamed` (or `StartTurn` plus manual `Next`)
and close only the local stream or handle.
Low-level `rpc.Client.SubscribeNotifications` buffers at most the requested number
of pending notifications (64 by default). If a consumer falls behind, only that
iterator closes and `Next` returns an `rpc.NotificationOverflowError`; JSON-RPC
responses and other subscribers continue normally.
```go
note, err := iterator.Next(ctx)
var overflow *rpc.NotificationOverflowError
if errors.As(err, &overflow) {
// Events were lost. Increase the capacity, drain faster, and subscribe
// again; overflow.Capacity reports the exhausted hard limit.
}
```
## Account, models, and threads
High-level helpers wrap common app-server operations without requiring direct JSON-RPC calls.
```go
account, err := client.Account(ctx, codex.AccountOptions{})
models, err := client.ListModels(ctx, codex.ListModelsOptions{})
threads, err := client.ListThreads(ctx, codex.ThreadListOptions{})
```
Thread list, read, fork, and unarchive helpers return concrete protocol values.
List sorting uses `protocol.SortDirection` and `protocol.ThreadSortKey` rather
than arbitrary strings.
Thread values also expose lifecycle helpers:
```go
if _, err := thread.SetName(ctx, "Investigation"); err != nil {
panic(err)
}
forked, _, err := thread.Fork(ctx, codex.ThreadForkOptions{})
if err != nil {
panic(err)
}
_ = forked
```
For lower-level or less stable protocol features, use `client.Client()` and the generated `rpc` package.
### Low-level union migration
Discriminated protocol unions are concrete generated wrapper types rather than
`interface{}`. Construct values with functions such as
`protocol.NewUserInput`, inspect the typed `Kind`, and use `RawJSON` when a
variant-specific payload is needed:
```go
input, err := protocol.NewUserInput(map[string]any{
"type": "text",
"text": "Inspect the repository",
})
if err != nil {
return err
}
if input.Kind() == protocol.UserInputKindText {
var payload struct {
Text string `json:"text"`
}
if err := json.Unmarshal(input.RawJSON(), &payload); err != nil {
return err
}
}
```
Constructors validate JSON encoding, the required non-empty discriminator, and
required-field presence for known variants. Field value types and other schema
constraints remain server-validated. Known variants have generated kind
constants; a well-formed future discriminator remains round-trippable and
reports `IsKnown() == false`. Existing opaque schemas remain `interface{}` only
through reviewed generator allowlists, and generation fails when a new weak
fallback appears. The 31 affected wrappers—including `UserInput`,
`ResponseItem`, `SandboxPolicy`, `ThreadStatus`, and `LoginAccountParams`—are
listed in `protocol/unions_gen.go`.
Generated protocol declarations use Go initialisms consistently, including
`MCP`, `OAuth`, `ID`, `URL`, and `JSON`. Older `Mcp...`/`Oauth...` spellings and
`Sanitized...JSON` implementation names remain deprecated aliases for a
migration window. Prefer canonical declarations such as
`protocol.MCPServerOAuthLoginParams` and canonical methods such as
`rpc.Client.MCPServerOAuthLogin`.
When migrating from older SDK APIs, account for the typed unions and canonical
names above, `Options.CompatibilityPolicy`, and `StartLogin`'s JSON-marshalable
login parameters. Update code that uses unkeyed `Options` literals, affected
interface method sets, or assigned `StartLogin` method values.
`protocol.GeneratedCodexVersion` identifies the upstream wire schema; the Go
module tag identifies the SDK release.
## Approvals
Configure app-server requests with optional callbacks. Unset callbacks return
`rpc.ErrServerRequestUnsupported`, so adding a future server request does not
break existing applications at compile time.
```go
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo}))
client, err := codex.New(ctx, codex.Options{
Logger: logger,
RequestHandler: codex.RejectingApprovalCallbacks(),
})
```
For custom logic, set only the callbacks your application supports:
```go
callbacks := &codex.ServerRequestCallbacks{
ApproveFileChange: func(ctx context.Context, request protocol.FileChangeRequestApprovalParams) (*protocol.FileChangeRequestApprovalResponse, error) {
return &protocol.FileChangeRequestApprovalResponse{
Decision: protocol.FileChangeApprovalDecisionDecline,
}, nil
},
}
client, err := codex.New(ctx, codex.Options{RequestHandler: callbacks})
```
`Options.ApprovalHandler` and the broad generated `rpc.ServerRequestHandler`
remain as deprecated compatibility surfaces. Low-level handlers that must
implement only selected methods can embed `rpc.UnimplementedServerRequestHandler`.
For methods whose historical Go spelling used non-canonical initialisms, such
as `McpServerElicitationRequest`, implement the exported canonical capability
interface (for example, `rpc.MCPServerElicitationRequestHandler`); dispatch
prefers that method over the legacy spelling. The client may invoke different
server-request callbacks concurrently, so callbacks that share state must be
concurrency-safe.
`AutoApproveHandler` accepts command, file-change, and permission requests and
must only be used in a trusted environment. Its default logs are redacted. If
sensitive command and path logging is explicitly required, construct
`NewUnsafeLoggingAutoApproveHandler(logger)` and protect those logs accordingly.
Use `codex.AutoApproveCallbacks(codex.AutoApproveHandler{Logger: logger})` to
attach a safe auto-approver through the preferred callback API. The unsafe
handler can be passed to the same adapter when explicitly required.
## Codex CLI compatibility
When the SDK spawns `codex`, it requires the CLI major/minor version to match
the generated protocol version; patch differences are accepted. A mismatch,
missing binary, or unparseable version returns `*codex.CodexCompatibilityError`
before the process starts. Set `CompatibilityPolicy: codex.Warn` only after
validating compatibility (and provide a logger to see the warning), or
`codex.Ignore` to skip the probe. Custom transports are never probed.
### Updating to protocol 0.167.2
This SDK targets Codex CLI 0.167.x. `protocol.UserInput` accepts audio with
either `url` or an uploaded `fileId`; `protocol.ContentItem` uses `audio_url`
or `file_id`. The raw-preserving wrappers retain both forms and unknown fields.
Existing `codex.AudioInput` and `codex.LocalAudioInput` constructors keep their
current wire format. Use the low-level protocol API for uploaded audio.
`thread/section/changed` is a typed notification. Its `sectionId` is a string
when a thread moves into a section and null when it leaves. Managed OTEL
requirements expose `LogSSEDeltas` as `log_sse_deltas`, including explicit false.
`CommandExecutionApprovalKindHumanCommand` requires a fresh human decision.
Both automatic approval handlers decline these requests. Low-level clients
opting into `InitializeCapabilities.RequiredHumanApproval` must also enable
`ExperimentalApi` and provide a custom callback that consults a person and
returns a one-time `accept`, `decline`, or `cancel`. The high-level client does
not advertise this capability. The experimental `thread/guardianReview` RPC
is outside this stable protocol export.
The minimum Go patch versions are now 1.26.11 and 1.27.4 to include the
standard-library security fix for [GO-2026-6859](https://pkg.go.dev/vuln/GO-2026-6859).
### Updating to protocol 0.166.0
The 0.166.0 update made thread-item raw wrappers preserve optional
`startedAtMs`/`completedAtMs` lifecycle timestamps and image `generationId`.
Process-exit notifications expose optional `OutputDrainTimedOut`, and thread
usage exposes optional `NativeUsageUsdMicros`, preserving an explicit zero.
Hook listing accepts `ThreadID` to inspect a loaded thread's environment hooks;
omit `Cwds` in that mode. Hook metadata retains `environmentId`. Hook run
notifications now have generated structs with a typed `HookRunSummary`, including
native `SourcePath` strings. Code assigning arbitrary values to the former
`HookStartedNotification` or `HookCompletedNotification` interfaces must use the
new structs. Existing handler signatures are unchanged.
Managed requirements include OTEL settings and network `ReviewDomains`. The
mixed string/object `OtelExporter` is `json.RawMessage`; other OTEL fields remain
typed. Exporter header values are redacted by the server. Account payloads
recognize GCP and retain ChatGPT `workspaceName`; `ModelProviderError` preserves
structured provider errors and unknown future variants. Additional context may
include an optional `Name`.
`review/start` now accepts inline delivery only. To review on another thread,
start that thread first and pass its ID. `ReviewDeliveryDetached` remains as a
deprecated source compatibility constant; Codex 0.166.x rejects its wire value.
### Updating to protocol 0.164.0
The 0.164.0 update added `rpc.Client.FeedbackSave`, which saves a feedback
ZIP to a previously unused absolute path on the app-server host and returns
that path. The caller owns the archive, including after a disconnected request.
Application requirements expose optional storage paths. MCP server status
exposes `ToolListMeta` as `json.RawMessage`, preserving opaque metadata and exact
numbers. Hook notification payloads can report `preModelRequest` and
`postModelRequest`, and plan types include `sci` and `sci_plus`.
Image-generation thread items accept inline `result` data, a durable `fileId`,
or both during the upstream migration. Their raw union wrappers preserve
`inputImages` provenance and unknown fields.
### Updating to protocol 0.163.0
The 0.163.0 update added `ThreadListOptions.ExcludedThreadIDs` and
`protocol.ThreadListParams.ExcludedThreadIDs` to exclude up to 100 thread IDs before
the server applies the page limit. Send the same exclusions on each page; nil
or empty means no exclusions. Invalid IDs or more than 100 entries are rejected
by the server.
Codex 0.163.0's default scan-and-repair listing can return an empty page for
same-second archived threads when `Limit` is 1 and the first thread is excluded.
The indexed path (`UseStateDBOnly: &enabled`, with `enabled := true`) returns the
remaining thread in this case; it skips rollout scanning and metadata repair.
`TurnToolOutput.Retain` requests retention of supplied output in model history.
The low-level notification parser recognizes `thread/readState/changed`, with
revision tokens and tagged unread positions. Response-item status, encrypted
content, and output-text annotations/logprobs remain available through the
raw-preserving union wrappers. Generated realtime voice constants include the
new upstream values.
### Updating to protocol 0.162.1
The 0.162.1 update added
`ThreadAttachmentOwnerList` for reverse attachment lookup and
`thread/prediction/updated` notifications with raw-preserving completed/failed
results. `TurnStartParams.ParentTurnID` and `RootTurnID` attribute delegated work;
leave them unset for work started directly by the user. Returned `Turn` values
expose nullable `RootTurnID`.
Goal set/clear parameters now accept optional `Origin` (`user` or `automatic`).
Missing origin does not supply user authorization. MCP OAuth responses and
completion notifications expose optional `LoginID` for matching login attempts.
Assistant message phases now include `partial_answer`, which may precede more
output or tools.
`CodexErrorInfo` is now an alias of `json.RawMessage`, preserving arbitrary
future error objects and large integers. Callers assigning strings or maps must
marshal those values first; use `json.Unmarshal` to inspect the received JSON.
`ResponseItem` recognizes `additional_tools` and preserves its arbitrary tool
payloads. Upstream removed `ModelProviderCapabilitiesReadResponse.NamespaceTools`;
that field is removed from the generated SDK type.
### Updating to protocol 0.160.0
The 0.160.0 update added gateway OAuth status,
login, and cancellation, plus thread attachment add/list/remove calls and their
notifications. Attachment payloads use `json.RawMessage` to preserve arbitrary JSON
numbers and future fields. Upstream removed `thread/rollback`; its generated method and
types are removed. Use `ThreadRevert` with an explicit `BeforeTurnID` instead.
History pagination still accepts `ThreadItemsListParams.Cursor` as a string
pointer. For an exclusive item position, construct a
`protocol.ThreadItemsListAnchor` with `protocol.NewThreadItemsListAnchor`, set
`CursorAnchor`, and provide a nonempty `TurnID`. Supplying both cursor forms is
an error. History entries expose optional `StartedAtMs` and `CompletedAtMs`.
Image inputs and function outputs accept either a URL or an uploaded file ID
through the existing raw-preserving union constructors. At least one image
source is required. Lifecycle responses retain `DisabledPluginIDs`, and resume
responses expose `CollaborationMode`. In low-level `TurnStartParams`, a nil
`DisabledPluginIDs` preserves the saved list; a pointer to an empty slice clears
it. These saved plugin IDs do not yet filter plugin capabilities upstream.
MCP tool calls expose descriptor UI metadata, and resource reads can select an
explicit app/account target. Its nullable `LinkID` is required on the wire;
null requests no-auth access subject to the app's policy. Personality selection
is deprecated upstream: `friendly` and `pragmatic` no longer select a style.
`ConfigRequirements.WindowsSandboxPrivateDesktop` and its helper type are
removed upstream. `AllowedWindowsSandboxImplementations` now uses
`WindowsSandboxImplementation`, which includes `mxc`, instead of
`WindowsSandboxSetupMode`.
### Updating to protocol 0.154.0
Protocol 0.154.0 introduced the following additions. `AccountRateLimitsRead(ctx)` remains
available; low-level `AccountRateLimitsReadWithParams` accepts optional
`protocol.GetAccountRateLimitsParams` capabilities for Luna Reserve and
background reads that exclude reset-credit details. Rate-limit responses expose
nullable `OrdinaryUsageAllowed` and quota `NormalModelSlug` values. A missing
ordinary-usage permission does not imply that usage has recovered.
`protocol.Thread.Originator` identifies the client that created a thread.
`ThreadListOptions.Originators` filters by exact originator on hosted backends;
the local app-server rejects nonempty originator filters. Nil or empty filters
leave originators unrestricted.
The protocol also adds the `configuration_update` response-item variant,
application network requirement types, and MCP tool-discovery errors. Detached
review delivery is deprecated upstream; start a separate thread and request an
inline review instead.
### Updating to protocol 0.153.4
Protocol 0.153.4 introduced low-level RPC for paginated turn
and item history, thread revert, plugin reconciliation, and new realtime,
project, queue, and authentication-recovery notifications.
`AccountUsageRead(ctx)` remains available. Use
`AccountUsageReadWithParams(ctx, &protocol.GetAccountTokenUsageParams{ThreadID: &id})`
for thread-specific usage. `ThreadForkOptions.ExcludeTurns` is supported again.
Low-level resume and turn-start parameters expose the new history and per-turn
options. `Thread.ProjectID` is nullable and always serializes as `projectId`, as
required by the new protocol. Approval requests that omit `kind` decode as
`command`, matching the upstream default for older servers.
Section appearance updates distinguish omission, clearing, and replacement:
leave `ThreadSectionUpdateParams.Appearance` nil to preserve it, assign a pointer
to a nil `*protocol.ThreadSectionAppearance` to clear it, or point to an appearanceFrequently Asked Questions
What is codex-sdk-go?
codex-sdk-go is a Artificial Intelligence library for the Go programming language. Go SDK for the OpenAI Codex app-server with streaming, approval callbacks, and structured output
How do I install codex-sdk-go?
Install codex-sdk-go with the Go module system using `go get pmenglund/codex-sdk-go`. Check the repository for the current installation instructions.
What category does codex-sdk-go belong to?
codex-sdk-go is listed under Artificial Intelligence, specifically Artificial Intelligence.