Codex Rust SDK
Rust SDK for embedding the Codex agent in applications by driving the codex CLI over JSONL (codex exec --experimental-json).
Table of Contents
- Overview
- Status
- Installation
- Authentication and Environment Setup
- Quickstart
- API Selection Guide
- Core API Surface
- Feature Highlights
- Feature Comparison with Official TypeScript SDK
- Compatibility Matrix
- Known Limitations
- Testing and Validation
- Concurrency Model
- Development
- Contributing
- License
Overview
This crate is a parity-focused Rust implementation aligned with the official Codex TypeScript SDK semantics.
It supports:
- Thread-based multi-turn workflows (
start_thread,resume_thread) - Buffered and streamed turn execution (
run,run_streamed) - Structured output via JSON Schema (
--output-schema) - Multimodal input (text + local images)
- Cancellation and thread resume
- CLI config/env forwarding (
--config, API endpoint/key, sandbox/approval/web-search settings)
Status
- Package version:
0.107.0(codex-client-sdk) - Scope: parity-focused SDK implementation for core Codex workflows
- Validation: crate tests pass (
cargo test -p codex-client-sdk) - Rust docs: public API is documented and checked with
missing_docs
Installation
This repository currently uses a workspace/local package layout.
[]
= { = "codex-client-sdk", = "../../crates/codex" }
Runtime prerequisites:
- Rust 1.85+ (edition 2024)
- Codex CLI installed and available (
codex), typically from@openai/codex
Authentication and Environment Setup
The SDK invokes the external Codex CLI process. Authentication/config can be supplied either by environment variables or by CodexOptions.
Option A: environment variables
# Optional: override endpoint
Option B: programmatic overrides
use ;
#
Security note: do not hard-code or commit secrets to source control.
Quickstart
use Codex;
# async
Continue the same conversation
# use Codex;
# async
Stream events during a turn
use ;
use StreamExt;
# async
Structured output
use ;
use json;
# async
API Selection Guide
| Use case | Recommended API | Why |
|---|---|---|
| Need only final answer/items | Thread::run |
Simple call, returns Turn directly |
| Need progress events/tool/file updates | Thread::run_streamed |
Streamed typed events (ThreadEvent) |
| Continue prior thread by ID | Codex::resume_thread + run/run_streamed |
Restores conversation context |
| Need text + images in one turn | Input::Entries(Vec<UserInput>) |
Matches CLI --image flow |
Core API Surface
Codexnewstart_threadresume_thread
Threadidrunrun_streamed
- Input types
Input(Text,Entries)UserInput(Text,LocalImage)
- Options
CodexOptionsThreadOptionsTurnOptions
- Event/item models
ThreadEvent+ typed event payloadsThreadItem+ typed item payloads
- Low-level execution
CodexExecCodexExecArgs
Feature Highlights
- Robust CLI binary discovery (PATH, local
node_modules, vendor, common globals) - Typed event/item deserialization from JSONL stream
- Output schema temp-file lifecycle managed automatically
- Config object flattening to repeated TOML-compatible
--configflags - Explicit precedence behavior for overlapping options (for example
web_search_modeoverweb_search_enabled)
Feature Comparison with Official TypeScript SDK
| Feature | Official TypeScript SDK | This Rust SDK | Notes |
|---|---|---|---|
| Start/resume threads | ✅ | ✅ | startThread / resumeThread vs start_thread / resume_thread |
| Buffered turn API | ✅ (run) |
✅ (run) |
Equivalent high-level behavior |
| Streamed turn API | ✅ (runStreamed) |
✅ (run_streamed) |
Rust returns futures::Stream |
| Structured output schema | ✅ | ✅ | Rust accepts serde_json::Value schema |
| Multimodal input (text + local images) | ✅ | ✅ | Input::Entries + UserInput::LocalImage |
| Cancellation | ✅ (AbortSignal) |
✅ (CancellationToken) |
Rust-idiomatic token |
| CLI env override | ✅ | ✅ | CodexOptions.env |
Config flattening to --config flags |
✅ | ✅ | TOML-compatible serialization |
| All event types (thread/turn/item) | ✅ | ✅ | Full alignment with exec_events.rs |
| All item types (message/reasoning/etc) | ✅ | ✅ | Full alignment with CLI output |
| Schema helper integration | ✅ (Zod ecosystem) | ✅ (via serde_json::Value) |
Rust users pass JSON Schema directly; consider schemars crate for derive-based schemas |
| Core SDK workflow | ✅ | ✅ | Full parity for all core use cases |
Note: This Rust SDK achieves full core parity with the official TypeScript SDK. The only ecosystem differences are:
- TypeScript has Zod integration (
zodToJsonSchema) while Rust users pass JSON Schema directly viaserde_json::Value- For Rust schema generation, consider using the
schemarscrate for derive-based JSON Schema generation
Compatibility Matrix
| Component | Requirement / Notes |
|---|---|
| Rust | 1.85+ |
| Edition | 2024 |
| Codex CLI | Required; install codex (typically via @openai/codex) |
| Runtime | Tokio async runtime |
| OS support | Linux/macOS/Windows expected via CLI support |
Known Limitations
- The SDK wraps an external CLI process; behavior also depends on installed CLI version.
- No built-in JSON-schema builder helper is provided; pass schema as
serde_json::Value. - Test suite is comprehensive at protocol/mock level; real CLI/live-model matrix testing is not part of this crate.
Testing and Validation
Reference alignment coverage (TypeScript -> Rust):
tests/run.test.ts->tests/run_tests.rstests/runStreamed.test.ts->tests/run_streamed_tests.rstests/exec.test.ts->tests/exec_tests.rstests/abort.test.ts->tests/abort_tests.rs
Validation commands:
RUSTDOCFLAGS='-Dwarnings -Dmissing_docs'
Concurrency Model
run_streamed()returns aSendstream ofThreadEventrun()materializes a finalTurnby consuming streamed events- Turn cancellation uses
tokio_util::sync::CancellationToken
Development
Contributing
Pull requests are welcome. Before submitting, run:
License
Licensed under the Apache License, Version 2.0.