Finally, a Codex Python SDK — Here’s What It Actually Does
From the publisher’s feed, collected in the ZeroLabs newsroom.
For months, if you wanted to drive OpenAI’s Codex agent from your own code, you had two options: shell out to the CLI and parse text, or use the TypeScript SDK and accept that your stack now spoke Node. If your world is Python — data pipelines, Django backends, ML tooling, CI scripts — neither felt great.That gap just closed. There’s now an official Python SDK for Codex, installable with a boring, beautiful pip install openai-codex.This post walks through what the SDK is, the handful of lines you need to run an agent from Python, and the few things worth understanding before you wire it into something real — threads, sandboxes, streaming, and structured output. If you’ve ever wanted Codex inside a Python job instead of a terminal, this is the on-ramp.What this SDK actually is (and isn’t)A quick mental model first, because the naming around Codex is a maze.This is not a new way to call the OpenAI chat API. It’s a way to control the Codex agent — the same agent you get in the CLI, the IDE extension, and Codex Web — from Python. Under the hood, the Python SDK controls the local Codex app-server over JSON-RPC. You’re not reimplementing the agent loop; you’re remote-controlling the real one.That distinction matters. You get Codex’s actual harness — its planning, its tool use, its sandboxing, its iterate-until-tests-pass behavior — exposed as a Python object. The SDK is the steering wheel, not the engine.Two practical consequences:It requires Python 3.10+, and published builds ship with a pinned Codex CLI runtime, so versions stay in lockstep.It reuses your existing Codex authentication automatically. If you’re already logged in via the CLI, you don’t re-auth in code.The smallest thing that worksHere’s a complete program. It starts Codex, opens a thread, runs one prompt, and prints the answer:pythonfrom openai_codex import Codexwith Codex() as codex: thread = codex.thread_start() result = thread.run("Explain this repository in three bullets.") print(result.final_response)That’s the whole API surface for the simplest case. The with block manages the underlying app-server process for you. thread.run(...) returns a TurnResult — the part most people want is result.final_response, but it also carries the collected items (everything the agent did) and usage (token counts) if you need them.Want to keep the conversation going? Call run() again on the same thread. State persists across turns automatically:pythonwith Codex() as codex: thread = codex.thread_start() thread.run("Make a plan to diagnose and fix the CI failures.") thread.run("Now implement the plan.") # remembers the planThreads are the unit of workThe thread is the core abstraction, and it’s worth a second to appreciate why it’s the right one.A thread is a persistent conversation with the agent. Each run() is one turn. Because the thread holds context, you can build genuine multi-step workflows — plan, then execute, then review — without manually re-feeding history. You can also configure the model and other settings when you open the thread:pythonfrom openai_codex import Codex, Sandboxwith Codex() as codex: thread = codex.thread_start( model="gpt-5.4", sandbox=Sandbox.workspace_write, ) result = thread.run("Make a plan to diagnose and fix the CI failures.") print(result.final_response)If your application is already async — a web server, a worker queue — reach for AsyncCodex instead and await the calls:pythonimport asynciofrom openai_codex import AsyncCodexasync def main() -> None: async with AsyncCodex() as codex: thread = await codex.thread_start(model="gpt-5.4") result = await thread.run("Implement the plan") print(result.final_response)asyncio.run(main())Same shape, just await-ed. That symmetry is a nice touch — you don't learn two different APIs.Sandboxes: deciding how much the agent can touchThis is the part to read carefully, because it’s the difference between “Codex helpfully edits my repo” and “Codex edits something it shouldn’t have.”A coding agent runs shell commands and writes files. The SDK gates that with Sandbox presets you pass when starting a thread — or per turn:Sandbox.read_only — the agent can read files but not write. Good for analysis, review, and Q&A.Sandbox.workspace_write — read, plus write inside the workspace and any configured writable roots. The default working mode.Sandbox.full_access — no filesystem restrictions. Use with care.The clever bit is that the sandbox can change between turns on the same thread. So you can let the agent make changes, then drop it to read-only to review its own diff without the ability to “fix” things further:pythonfrom openai_codex import Codex, Sandboxwith Codex() as codex: thread = codex.thread_start(sandbox=Sandbox.workspace_write) thread.run("Make the requested change.") review = thread.run("Review the diff only.", sandbox=Sandbox.read_only)A sandbox passed to a single run(...) applies to that turn and the turns after it. When you omit it, the app-server uses its configured default.Streaming, when you want to watch the workrun() buffers everything and hands you the result when the turn finishes. That's fine for a script. But if you're showing progress in a UI or a chat channel, you want to see tool calls and partial output as they happen.For that, the SDK exposes a streamed variant that yields structured events instead of one final blob — you iterate over events and react to the ones you care about, like an item finishing or the turn completing with its usage totals. In practice you watch for event types such as item.completed (the agent finished a step) and turn.completed (the whole turn is done, with token usage attached). It's the same pattern most agent SDKs converge on, so if you've used one, this will feel familiar.Structured output for when you need data, not proseA final-response string is great for humans and terrible for the next function in your pipeline. The SDK lets you hand Codex a JSON schema and get back a response that conforms to it — so a CI bot can return {"summary": "...", "status": "ok"} instead of a paragraph you'd have to parse with a regex and a prayer.That’s what turns Codex from “a thing I chat with” into “a step in an automated workflow.” Define the shape you need, and you get typed, predictable data on the other side.Where this fitsStep back and the why is clearer than any single feature. OpenAI’s own framing for the SDK is about automation: putting Codex into CI/CD pipelines, internal tools, and your own agents that engage Codex to do real engineering work.This is also part of a bigger pattern across the whole space. The agent harness — the layer running the loop between reasoning and execution — is increasingly the product, and every vendor is racing to expose it as a clean library you can embed. Codex shipping a first-class Python SDK, alongside its TypeScript one, is OpenAI meeting Python developers where they already live.A couple of honest caveats before you go all-in:It’s beta. The package, docs, and quickstarts are fresh, and the surface may still shift. Pin your versions.There are also community Python wrappers floating around on PyPI (names like codex-sdk-py and similar) that spawn the CLI and exchange JSONL events. They work, but for the supported path, the official openai-codex package is the one to build on.If you’ve been waiting to drive Codex from Python without shelling out to a CLI and scraping stdout, the wait is over. Start with the four-line example above, add a sandbox preset, and grow from there.The official docs are at developers.openai.com/codex/sdk, with the source in the openai/codex repo. If you build something with it — a CI reviewer, an internal tool, an agent that drives Codex — I’d be curious what sandbox setup you land on, since that’s the decision that seems to matter most in practice.Finally, a Codex Python SDK — Here’s What It Actually Does was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.