Repository navigation
fix(openrouter): use dedicated /api/v1/images endpoint for image generation - #101367
Conversation
|
Codex review: needs maintainer review before merge. Reviewed August 15, 2026, 3:11 PM ET / 19:11 UTC. ClawSweeper reviewWhat this changesThe PR sends official OpenRouter image requests to its dedicated images endpoint, retains chat-completions for custom bases, and updates focused tests and user docs. Merge readinessKeep open. Current main still uses OpenRouter chat completions for image generation, while the current PR head routes official OpenRouter bases to Priority: P1 Review scores
Verification
How this fits togetherThe OpenRouter plugin registers an image provider used by the shared image-generation runtime. It turns tool requests into provider HTTP calls and returns generated media assets for saving and delivery. flowchart LR
A[Image generation tool] --> B[Shared image runtime]
B --> C[OpenRouter image provider]
C --> D{Official or custom base}
D -->|Official| E[Dedicated images endpoint]
D -->|Custom proxy| F[Chat-completions endpoint]
E --> G[Generated media assets]
F --> G
Before merge
Agent review detailsSecurityNone. PR surfaceSource +98, Tests +65, Docs +8. Total +171 across 4 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Retain the plugin-local official/custom routing split, land after exact-head CI is green, and confirm the current OpenRouter endpoint contract against the contributor's live evidence. Do we have a high-confidence way to reproduce the issue? Yes—source inspection shows current main uses chat completions and the PR changes that branch; the PR also includes redacted live after-fix evidence. Local execution was unavailable because the read-only environment prevented Corepack from creating its cache. Is this the best way to solve the issue? Yes—the final plugin-local adapter is preferable to widening the shared image-provider API for one provider-specific flat endpoint, provided the external contract is reconfirmed before merge. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against dbc2356abc9a. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (14 earlier review cycles; latest 8 shown)
|
|
Addressed all three P1/P2 items from the review:
One follow-up found during the live run (bcd48866): the API can return JPEG bytes without a @clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. |
|
Addressed the remaining P3 docs finding in 91df3b95: The @clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. |
|
@vincentkoc @RomneyDa @shakkernerd would appreciate a review here! |
|
This pull request has been automatically marked as stale due to inactivity. |
91df3b9 to
8eb5ef9
Compare
8052ad8 to
819e1e5
Compare
819e1e5 to
d5755b5
Compare
d5755b5 to
dd19d81
Compare
dd19d81 to
cb2d825
Compare
cb2d825 to
8f807ae
Compare
|
Fixed: OpenRouter image generation now uses the dedicated images endpoint for official bases while preserving custom proxy compatibility. With thanks to @Robinnnnn. |
What Problem This Solves
OpenClaw's OpenRouter image generation currently goes through
/chat/completions, treating image models as chat models and scraping images out of chat responses with ~300 lines of custom parsing. OpenRouter now ships a dedicated, OpenAI-compatible images endpoint (POST /api/v1/images), which is the supported path for image generation going forward and returns images in the standarddata[].b64_jsonshape.Why This Change Was Made
The OpenRouter provider is rebuilt on the shared
createOpenAiCompatibleImageGenerationProvider()factory — the same one used by xAI, DeepInfra, and LiteLLM — so it inherits the common auth, base-URL/config, timeout, SSRF, and response-size handling instead of maintaining a parallel implementation. Two small, backwards-compatible additions to shared code support this:endpointPathoption on the factory, so a provider can target a flat path like/api/v1/imagesinstead of the default/images/generations+/images/editssplit. The path is appended to the resolved base URL; leading and duplicate slashes are normalized (both"images"and"/images"resolve to<baseUrl>/images), documented on the option and covered by factory tests (string, leading-slash, and per-mode function forms). Providers that don't set it behave exactly as before.media_typeas a fallback whenmime_typeis absent (OpenRouter labels the image MIME typemedia_type).mime_typestill wins when present, so other providers are unaffected.The request body maps
model,prompt,n, plusaspect_ratio/resolutionfor Gemini image models andinput_references(data URLs) for image-to-image. The custom chat-completions parsing (extractOpenRouterImagesFromResponseand friends) is deleted.The OpenRouter provider also enables the factory's magic-byte MIME sniffing: live testing showed the API omits
media_typefor PNG responses but can also return JPEG bytes without it, so sniffing keeps returned assets correctly labeled (an explicitmedia_typestill takes precedence).User Impact
OpenRouter image generation and image editing keep working with the same configuration and models, now against OpenRouter's supported dedicated endpoint. Gemini image models gain aspect-ratio and resolution control, and multi-image references are passed through natively. No config changes are required.
Evidence
Tests
pnpm vitest run extensions/openrouter/image-generation-provider.test.ts— 8 passedpnpm vitest run src/image-generation/— 53 passed (includes the shared factory tests,endpointPathnormalization tests, and amedia_typefallback test)pnpm vitest run extensions/{xai,deepinfra,litellm,openai}/image-generation-provider.test.ts— factory consumers unaffected, all passedpnpm tsgo:extensions:testandpnpm tsgo:test:src— clean (covers thecheck-test-typesCI lane)Live behavior proof (redacted)
Raw endpoint contract —
curl -X POST https://openrouter.ai/api/v1/imageswith{"model":"google/gemini-3.1-flash-image-preview","prompt":"a red bicycle leaning against a brick wall","n":1,"aspect_ratio":"1:1"}(API key redacted).HTTP 200; response withb64_jsonelided:{ "created": 0, "usage": { "total_tokens": 1128 }, "data": [ { "media_type": "(absent — PNG default)", "b64_json_len": 3049104, "b64_json_prefix": "iVBORw0KGgoA" } ] }(
iVBORw0KGgois the base64 of the PNG magic bytes89 50 4E 47.)End-to-end through this PR's provider code — a small driver script imports
buildOpenRouterImageGenerationProvider()from this branch and callsgenerateImage()twice against the live API (text-to-image, then image-to-image feeding the first output back as a reference image):Both calls hit
https://openrouter.ai/api/v1/images(flat path, no/images/generationssuffix), decoded to valid images (filereportsPNG image data, 1024 x 1024/ JPEG), and the reportedmimematches the actual magic bytes — including the JPEG case where the API omittedmedia_type, which is what motivated enablingsniffMimeType.Related Issues
None of these are literally "migrate to
/api/v1/images", but this PR directly advances three open feature requests (referenced as related, not closed by this PR):/v1/images/generations. This PR rebuilds OpenRouter on that shared OpenAI-compatible factory, and the newendpointPathoption is the seam that lets flat-path providers plug into it.aspect_ratio/resolutionandinput_referencesnatively, which is the plumbing that request needs.Lineage: #55066 (the original OpenRouter image provider this PR migrates) and #79535 (OpenRouter media-generation gaps) are the closed history behind the current implementation.