Skip to main content
codex-image-gen hero image

codex-image-gen

Generate images from Python through Codex OAuth Responses

English · 한국어

Tests Release License Python Model Dependencies


codex-image-gen is a tiny, dependency-clean Python library for generating high-quality images through Codex OAuth and the Responses API image_generation tool.

It uses your existing Codex login from ~/.codex/auth.json, sends a raw Responses payload to the Codex OAuth bridge, and returns Python dataclasses with decoded image bytes and metadata. It does not use OPENAI_API_KEY.

Demo

Generated app icon demo

Generated with codex-image-gen + gpt-image-2.

Quick Start

codex-image-gen quick start code card

uv add codex-image-gen

# or
pip install codex-image-gen
from pathlib import Path

from codex_image_gen import generate_image

result = generate_image(
    "A serene mountain landscape with a lake and pine trees, sunset light",
    size="1024x1024",
    output_format="png",
)

image = result.images[0]
Path("mountain.png").write_bytes(image.data)
print("saved:", image.mime_type, len(image.data), "bytes")

That's it. Your image is saved locally.

Features

codex-image-gen workflow diagram

High-quality images
Powered by gpt-image-2 through Codex OAuth Responses.
Multiple input types
Use local paths, URLs, data URLs, file IDs, and detail mappings.
Robust and reliable
Clear exceptions for OAuth bridge failures, invalid JSON, missing image results, and bad base64.
Focused parameter surface
Forward the gpt-image-2 options used by the Codex bridge.
Typed results
Frozen dataclasses with image bytes, MIME type, response ID, call ID, revised prompt, and partial images.
Zero heavy deps
Runtime uses only the Python standard library.

API Highlights

generate_image(
    prompt: str,
    *,
    images=None,
    model="gpt-5.5",
    size="auto",
    output_format="png",
    output_compression=None,
    background="auto",
    input_image_mask=None,
    moderation=None,
    partial_images=None,
    reasoning_effort=None,
    reasoning_summary=None,
    text_verbosity=None,
    instructions=None,
    timeout=300,
    oauth_base_url="https://chatgpt.com/backend-api/codex",
    auth_file=None,
)
  • Simple, composable function API.
  • Rich result objects with images, metadata, raw response data, and optional partial images.
  • Reference images can be local files, remote URLs, data: URLs, or file IDs.
  • Masked edits use input_image_mask; pass a mask as a local path, URL, data: URL, or file ID. The mask must match the edited image's dimensions and format, stay under 50 MB, and include an alpha channel.
  • Size, output format, output compression, background, moderation, partial images, reasoning settings, text verbosity, timeouts, and custom instructions are first-class options.

Examples

Edit With References

result = generate_image(
    "Edit the reference image into a watercolor postcard",
    images=[
        "reference.png",
        {"file_id": "file_123", "detail": "high"},
    ],
    output_format="webp",
    output_compression=75,
)

Path("postcard.webp").write_bytes(result.images[0].data)

Masked Edits

codex-image-gen masked edit workflow

The diagram is a workflow illustration. For an actual masked edit, use an alpha-channel mask with the same dimensions and format as the image you edit.

result = generate_image(
    "Replace only the masked logo area with a white star",
    images=["product.png"],
    input_image_mask="mask_alpha.png",
    background="opaque",
)

Path("edited.png").write_bytes(result.images[0].data)

Partial Images

result = generate_image(
    "Create a polished app icon of a glass bottle",
    partial_images=2,
)

for partial in result.partial_images:
    Path(f"partial-{partial.index}.png").write_bytes(partial.data)

Partial images are collected after the streaming Responses request completes. This function does not expose live streaming callbacks.

Reasoning Settings

result = generate_image(
    "Create a detailed concept sheet for a modular desk lamp",
    reasoning_effort="high",
    reasoning_summary="auto",
    text_verbosity="low",
)

reasoning_effort supports none, minimal, low, medium, high, and xhigh. text_verbosity supports low, medium, and high.

Compatibility

This library intentionally exposes only the parameters used by the Codex Responses bridge.

  • The default mainline Responses model is gpt-5.5.
  • The image generation tool always sends model="gpt-image-2".
  • input_image_mask is exposed because it is documented for the Responses image_generation tool. The library forwards your mask; it does not synthesize the required alpha channel.
  • background="transparent" is rejected before calling Codex.
  • input_fidelity is not exposed because gpt-image-2 rejects it.
  • previous_response_id and previous image_generation_call item references are not exposed. Use image bytes, local files, URLs, or file IDs for follow-up edits.

Development

git clone https://github.com/smturtle2/codex-image-gen.git
cd codex-image-gen
uv sync --dev
uv run ruff check .
uv run pytest
uv build

Project Links

Link Description
Documentation Full docs and guide
Examples Real-world usage snippets
Contributing Local development workflow
Issues Report bugs and request features
Discussions Ask questions
PyPI Published Python package
Release v0.2.1 Wheel and sdist artifacts

Release

v0.2.1 is available on PyPI and as a GitHub Release with wheel and sdist artifacts.

Install from PyPI:

uv add codex-image-gen

# or
pip install codex-image-gen

License

MIT

Metadata

Release files for codex-image-gen 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for codex-image-gen 0.2.1
File Size Uploaded
codex_image_gen-0.2.1.tar.gz 3.6 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for codex-image-gen 0.2.1
File Interpreter ABI Platform
codex_image_gen-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 3.6 MB

Release files / codex_image_gen-0.2.1.tar.gz

Download URL codex_image_gen-0.2.1.tar.gz
Size 3.6 MB
Tags Source
SHA-256 checksum
How to use checksums
b32a14eab6f945ab87e0d44389b6d4e61136aec5c184378656300337bca3fe80
BLAKE2b-256 checksum
How to use checksums
ff2af85a482cec6ee0a13665ccdc1a6d8c97917eaae32db33f6c00be8cc534e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 4, 2026.

Transparency log

Release files / codex_image_gen-0.2.1-py3-none-any.whl

Download URL codex_image_gen-0.2.1-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6650794e83d5f959b92b0f81c7c0266faf6a566942db00ec2ed2aa22ef63d33d
BLAKE2b-256 checksum
How to use checksums
19fac8398c53a4cac34dc0dbb0c41bb301d81cdb4f78977fc05429020553ba2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 4, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page