Skip to content

feat(image-gen): support reference images in openai-codex backend - #41159

Closed
airmonitor wants to merge 2 commits into
NousResearch:mainfrom
airmonitor:feat/image-gen-reference-images
Closed

feat(image-gen): support reference images in openai-codex backend#41159
airmonitor wants to merge 2 commits into
NousResearch:mainfrom
airmonitor:feat/image-gen-reference-images

Conversation

@airmonitor

Copy link
Copy Markdown

What & why

image_generate only exposed prompt + aspect_ratio, and the openai-codex
backend (gpt-image-2 via the Codex Responses image_generation tool) sent only a
text part — so callers could not pass a reference image to guide/edit generation
(e.g. preserve a brand mascot's identity).

This adds an optional reference_images parameter and honors it in the
openai-codex backend by forwarding each reference as an input_image content part
in the Responses request. gpt-image-2 processes image inputs at high fidelity, so
a single reference is enough to lock identity.

Changes

  • tools/image_generation_tool.py: optional reference_images (array of strings)
    in the agent schema; plumbed through _handle_image_generate and
    _dispatch_to_plugin_provider. Base ImageGenProvider.generate(**kwargs)
    already absorbs it, so backends without image input ignore it.
  • plugins/image_gen/openai-codex: _to_input_image_part normalizes each
    reference (http(s)/data:image/... URLs pass through; absolute file paths are
    realpath-resolved, size-capped, inlined as base64 data URLs), threaded through
    _build_responses_payload/_collect_image_b64, and validated in generate
    (<=4 images, <=10 MiB each) before any network call. No behavior change when
    reference_images is absent (payload byte-identical).

Scope

openai-codex only. Other backends (openai/fal/xai/krea) ignore the kwarg for now.

How to test

Unit:

pytest tests/plugins/image_gen/test_openai_codex_provider.py \
       tests/tools/test_image_generation_plugin_dispatch.py \
       tests/tools/test_image_generation.py -v

Manual (Codex OAuth signed in, image_gen.provider: openai-codex): ask the agent
to image_generate with reference_images=["https://.../mascot.png"]. Verified
against the live Codex backend — the reference image overrode the text prompt and
drove the output character identity.

Platforms

Tested on macOS (Python 3.11). File-path inputs use realpath + binary reads.

Related issues

None.

🤖 Generated with Claude Code

Add an optional `reference_images` parameter to the `image_generate` tool and
honor it in the openai-codex backend (gpt-image-2 via the Codex Responses
`image_generation` tool). Each reference is an http(s) URL, a data:image/... URL,
or an absolute file path; URLs/data-URLs pass through and absolute paths are read
and inlined as base64 data URLs. References are forwarded to the Responses
request as `input_image` parts, letting a caller preserve the identity of, e.g.,
a brand mascot in the generated image.

- tools/image_generation_tool.py: add optional `reference_images` to the agent
  schema; plumb it through `_handle_image_generate` and
  `_dispatch_to_plugin_provider`. Base `ImageGenProvider.generate(**kwargs)`
  already absorbs it, so backends without image-input support ignore it.
- plugins/image_gen/openai-codex: add `_to_input_image_part` normalization
  (realpath + size cap + mime), thread `image_parts` through
  `_build_responses_payload`/`_collect_image_b64`, and validate in `generate`
  (<=4 images, <=10 MiB each) before any network call. No behavior change when
  `reference_images` is absent (payload is byte-identical to today).
- tests: normalization cases, payload threading, dispatch forwarding, schema
  exposure, and limit/validation errors.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@alt-glitch alt-glitch added type/feature New feature or request tool/vision Vision analysis and image generation comp/plugins Plugin system and bundled plugins provider/openai OpenAI / Codex Responses API P3 Low — cosmetic, nice to have labels Jun 7, 2026
Refactor `_extract_image_b64` to return both final and partial image b64
values separately, allowing the caller to always prefer the finished image
and fall back to a partial only when no final result arrives. This prevents
a half-rendered streaming preview from being saved as the final PNG.

- plugins/image_gen/openai-codex: update `_extract_image_b64` to return
  `(final_b64, partial_b64)` tuple and update callers accordingly
- plugins/image_gen/openai-codex: change `partial_images` default from 1
  to 3 for better streaming preview coverage
@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Closing as a duplicate — Codex OAuth image-to-image / reference-image input is now on main via #57017 (merge commit 019950560).

That PR wires source/reference images through the Codex Responses input_image path on the unified image_generate surface, with magic-byte validation, a 25MB cap, the agent.file_safety read guard, and a 16-image clamp. Your PR tackled the same feature — thank you for the contribution and for helping surface how much demand there was for this. Credit to everyone in the cluster who pushed on it.

If you spot a gap in the merged implementation, please open a follow-up. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/plugins Plugin system and bundled plugins P3 Low — cosmetic, nice to have provider/openai OpenAI / Codex Responses API tool/vision Vision analysis and image generation type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants