Skip to content

Images API client and imagegen CLI (gpt-image-2.5) - #41

Merged
odrobnik merged 5 commits into
mainfrom
agent/issue-40
Oct 30, 2026
Merged

odrobnik merged 5 commits into
mainfrom
agent/issue-40

Conversation

@odrobnik

Copy link
Copy Markdown
Contributor

Resolves #40.

What

  • Client (Providers), built on the existing Image Generation code: OpenAI.generateImages(_:) (/v1/images/generations), editImages(_:) (/v1/images/edits, multipart, image[] + optional mask, never input_fidelity) and generateImageBatch(_:concurrency:) (JSONL jobs, bounded concurrency, a result per job).
  • Open ImageModel (gpt-image-2.5-sunburst default, gpt-image-2.5-flare, gpt-image-2; unknown names pass through), ImageSize (auto / WxH, validated: multiples of 16, longest side ≤ 3840, aspect 1:3–3:1, 655,360–8,294,400 px; above 2560×1440 allowed with an "experimental" note), open ImageQuality (xhigh/max only on 2.5), transparency rules (2.5 only, not jpeg), compression (jpeg/webp, 0–100).
  • Decodes b64_json, usage and the echoed size/quality/background/output_format; API errors become ImageGenerationError.api(ImageAPIError) carrying x-request-id.
  • ImageInfo reads real pixel size and alpha from PNG/JPEG/WebP headers (no AppKit); MultipartFormData lives in Providers. Foundation-only, so it builds on every platform.
  • The API key comes from the caller (OpenAI(credential:)); the client never reads files or the environment.
  • The old generateImage(prompt:model:) still compiles and is deprecated in favour of generateImages(_:).
  • CLI: new imagegen executable (Examples/ImageGen, ArgumentParser, Providers only) with generate, edit, generate-batch; --dry-run, --json, --force, --n path suffixes, --api-key-file, size presets (1080p→1920x1088, 1440p, 4k, each -portrait).

Dry run

$ imagegen generate --prompt "A tidy desk" --size 2048x1152 --quality xhigh \
    --output-format webp --output-compression 80 --n 2 --out output/imagegen/desk.png --dry-run
POST /v1/images/generations
{
  "model" : "gpt-image-2.5-sunburst",
  "n" : 2,
  "output_compression" : 80,
  "output_format" : "webp",
  "prompt" : "A tidy desk",
  "quality" : "xhigh",
  "size" : "2048x1152"
}
would write output/imagegen/desk-1.png
would write output/imagegen/desk-2.png

Verification

  • swift build --build-tests and the full swift test pass locally (322 + 8 tests); SwiftLint --strict is clean.
  • New offline tests cover every size rule in both orientations, the model/quality matrix, transparency, request encoding for generate/edit/batch, response decoding with usage, error mapping with x-request-id, batch concurrency, and header parsing.
  • Live tests were skipped: no OPENAI_API_KEY in this environment, so ImagesLiveTests (low generate on flare and sunburst at 1536x864, plus a transparent png checking alpha) have not run. The operator needs to run them; they use at most three low images.

🤖 Generated with Claude Code

Added the Images API client (generate, edit, batch, validation, usage, typed errors) to Providers and an imagegen CLI next to Coder. Offline tests and lint pass locally. The live tests were skipped because there was no OPENAI_API_KEY, so the operator needs to run them.

Generate, edit (multipart) and batch via the OpenAI Images API with open model
names (gpt-image-2.5-sunburst/flare, gpt-image-2), size validation, the
model/quality/transparency rules, usage, typed errors with x-request-id, and a
header reader for pixel size and alpha. The imagegen executable mirrors
Codex's image_gen.py.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T07:06:34.324161Z 8146387 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 81463873f6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Examples/ImageGen/Commands.swift Resolved
Comment thread Examples/ImageGen/Commands.swift Resolved
Comment thread Examples/ImageGen/Commands.swift Resolved
@odrobnik

Copy link
Copy Markdown
Contributor Author

I ran the live check on b7077e9, the step the agent left to the operator, using --api-key-file with MissionControl's OpenAI key. Three low-quality images:

Command Result
generate --model gpt-image-2.5-flare --size 1536x864 --quality low 1536×864 PNG ✅
generate --model gpt-image-2.5-sunburst --size 1536x864 --quality low --json 1536×864 PNG ✅; JSON has request_id, resolved settings and usage (139 tokens)
generate --model gpt-image-2.5-flare --size 1024x1024 --background transparent --output-format png 1024×1024 PNG with an alpha channel ✅

I also ran these offline with --dry-run, so no request was sent:

  • 1000x1000 → multiples of 16 ✅
  • 4096x1152 → longest side ≤ 3840 ✅
  • 3840x1008 → aspect ratio 1:3 to 3:1 ✅
  • 512x512 → total-pixel range ✅
  • gpt-image-2 --quality max and gpt-image-2 --background transparent → both refused with a message pointing to the 2.5 models ✅
  • transparent + jpeg → refused ✅
  • 1080p-portrait → 1088x1920, rounded to multiples of 16 ✅

@odrobnik odrobnik left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review of b7077e9. I read the diff only and did not build or run anything. CI is green. I did not have the earlier review's findings, so I could not check each one individually. Instead I reviewed the new head as a whole, and the batch path claims, the 16-image edit cap and the dry-run validation look sound. One small correctness gap remains in size validation.

Findings:

  • 🔵 Low Sources/Providers/API/OpenAI/Image Generation/Images API/ImageSize.swift — ImageSize.dimensions lowercases the text and parses it with Int(...), so validate() accepts 2048X1152 and +1024x+1024. But the request sends rawValue verbatim, because encode(to:) and formFields() do not normalise it. A size that passed client-side validation therefore reaches the API in a non-canonical form. The API may reject it with a server error, which defeats the 'no request is sent on a violation' guarantee. The test 2048X1152 even asserts that the uppercase form parses. Fix: require ASCII digits and a lowercase x in dimensions, or normalise rawValue to WxH on init.

To unblock the merge, address every finding: react with 👍 if it is accurate (👎 if not), reply with a brief comment on what was fixed, and mark the item as resolved.

Reviewed by Claude

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@odrobnik odrobnik left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review of 6806ff5. I read the diff only and did not build or run anything. CI is green, and the operator's live check is on the PR.

The 'canonical WxH sizes' commit closes the gap raised last round. ImageSize.dimensions now accepts only ASCII digits, a lowercase x and digits, with no empty parts. 2048X1152, +1024x+1024 and 1024x1024x2 all return nil, so validate() rejects them and no non-canonical size is sent. A combining-mark character that slips through the "0"..."9" range check still fails Int(...), which returns nil, so it is rejected too. The tests now assert these cases.

This commit does not touch the batch dry-run validation, the path claims from rejected batch plans, --json for batch dry runs or the 16-image edit cap. I did not re-inspect them this round. I found no regressions in what changed, and nothing here loses data or leaves unrecoverable state.

👍 No findings.

Reviewed by Claude

@odrobnik
odrobnik merged commit 68fb59f into main Oct 30, 2026
6 checks passed
@odrobnik
odrobnik deleted the agent/issue-40 branch October 30, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Images API client and an imagegen CLI (Swift port of Codex's image_gen.py), with gpt-image-2.5

1 participant