fix(ai-grok): accept 21:9, 5:2, 1.5k and quality auto on grok-imagine-image-2.0 #2288 (GitHub #2287)
main fromrefs/pull/2287/head·merged by AlemTuzlak View on GitHubgenerateImage() with grok-imagine-image-2.0 throws on the sizes 21:9, 5:2 and every _1.5k size before it sends a request. The types also reject quality: 'auto'. The xAI API accepts all four on this model. This PR adds them for grok-imagine-image-2.0 only.
🎯 Changes#
grok-imagine-image-2.0gets its own aspect-ratio, resolution and size lists and types: the shared ones plus21:9,5:2and1.5k.qualityis'low' | 'medium' | 'auto'. The documented default is'auto'.grok-imagine-imageandgrok-imagine-image-qualitytake the same sizes as before.- Docs (
docs/adapters/grok.md), unit tests, an E2E route and spec, and a patch changeset are included.
Sources in the xAI docs:
- Image generation guide:
21:9,5:2, andqualitywithautoas the default. - Images API reference:
resolutiontakes1k,2kand1.5k. - Model page of
grok-imagine-image-2.0: prices for1.5K. The page ofgrok-imagine-imagelists1Kand2Konly.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Root cause#
Issue. A caller of generateImage() on grok-imagine-image-2.0 gets a local error for size: '21:9', '5:2' or any _1.5k size. TypeScript also rejects quality: 'auto' and resolution: '1.5k' in modelOptions.
Cause. validateImageSize() in packages/ai-grok/src/image/image-provider-options.ts checks every Imagine image model against one shared list. That list has no 21:9, 5:2 or 1.5k. GrokImagineImage2ProviderOptions types quality as 'low' | 'medium'.
Fix. grok-imagine-image-2.0 has its own lists and types with the new values. validateImageSize() uses them for that model only.
Possible alternatives#
- Add the values to the shared lists. Then the two other image models accept them too. The xAI docs name
grok-imagine-image-2.0forquality,21:9and5:2(migration guide), and they list1.5Kprices for that model only. So this PR does not do that. - Remove the local size check. Then the API reports a wrong size. The adapter checks the size before the request for every image model today, and this PR keeps that.
Testing#
Commands run. Unit tests and tsc for @tanstack/ai-grok, on the 1.1.1 source with @tanstack/ai 1.8.0. packages/ai-grok/src on main is identical to that source.
- With only the new tests added: 4 of 192 tests fail, and
tscreports 3 errors. - With this change: 192 tests pass, and
tscreports no error. oxfmt --checkpasses on the changed files.oxlintreports nothing new onpackages/ai-grok/src.- Not run:
pnpm run test:pras a whole, and the new Playwright spec on this base.
Manual test.
- On
@tanstack/ai-grok1.1.1 as published, make the two calls below. Each call throws, and no request reaches the API.
generateImage({ adapter: createGrokImage('grok-imagine-image-2.0', apiKey), prompt, size: '21:9_1.5k', modelOptions: { quality: 'auto' } })
Error: Size "21:9_1.5k" is not supported by model "grok-imagine-image-2.0". Expected an aspect ratio (...)
generateImage({ adapter: createGrokImage('grok-imagine-image-2.0', apiKey), prompt, size: '5:2' })
Error: Size "5:2" is not supported by model "grok-imagine-image-2.0". Expected an aspect ratio (...)
requests that reached api.x.ai: 0
- Apply this change to the same release and make the same calls against the xAI API. Each call returns one image.
generateImage({ adapter: createGrokImage('grok-imagine-image-2.0', apiKey), prompt, size: '21:9_1.5k', modelOptions: { quality: 'auto' } })
POST /v1/images/generations aspect_ratio 21:9, resolution 1.5k, quality auto -> HTTP 200, 1 image
generateImage({ adapter: createGrokImage('grok-imagine-image-2.0', apiKey), prompt, size: '5:2' })
POST /v1/images/generations aspect_ratio 5:2 -> HTTP 200, 1 image
requests that reached api.x.ai: 2, images returned: 2
How this PR makes testing easy.
- New unit tests in
packages/ai-grok/tests/grok-adapter.test.ts: the four new sizes,quality: 'auto'withresolution: '1.5k', the same rejection as before on the two other image models, and the types. - New E2E spec against aimock:
pnpm --filter @tanstack/ai-e2e exec playwright test tests/grok-image-2-options.spec.ts.
Risk / rollback#
Low. A matrix of 3,032 SDK calls ran before and after the change against a local recording mock. It has every chat, image and video model with every size, option and tool list, plus speech, transcription and files. All 1,488 requests that were sent before are byte-identical. The 304 calls that changed are all on grok-imagine-image-2.0: 157 with a new value are now sent, and 147 are still rejected, with the new values in the error message. To undo, revert this PR.
Public API change#
GrokImagineImage2ProviderOptions and the size type of grok-imagine-image-2.0 accept more values. No value is removed.
Before
await generateImage({
adapter: grokImage('grok-imagine-image-2.0'),
prompt: 'A futuristic cityscape at sunset',
threadId: 'thread-1',
size: '16:9_2k',
modelOptions: { quality: 'medium' },
})
After
await generateImage({
adapter: grokImage('grok-imagine-image-2.0'),
prompt: 'A futuristic cityscape at sunset',
threadId: 'thread-1',
size: '21:9_1.5k', // also '21:9', '5:2', and every other ratio with '_1.5k'
modelOptions: { quality: 'auto' },
})
Summary by CodeRabbit#
- New Features
- Grok Imagine Image 2.0 now supports the
21:9and5:2aspect ratios,1.5kresolution, and automatic quality selection. - Existing Grok Imagine models retain their previous supported image sizes.
- Grok Imagine Image 2.0 now supports the
- Documentation
- Updated the Grok image-generation documentation to include automatic quality selection as the default.
- 1
AlemTuzlak approved these changes
No actionable comments were generated in the recent review.
Walkthrough
-
Grok Image 2.0 adds support for aspect ratios
21:9and5:2, resolution1.5k, and qualityauto. Validation, adapter tests, and an end-to-end route cover these options. The documentation and changeset describe the updated options and default quality.Changes: Grok Image 2.0 options
Layer / File(s) Summary Image 2.0 options and validation
packages/ai-grok/src/image/image-provider-options.ts,.changeset/grok-image-2-options.md,docs/adapters/grok.md,docs/config.jsonImage 2.0 now has separate aspect-ratio, resolution, and size types. Validation selects the expanded values only for grok-imagine-image-2.0. The documentation and changeset list the added options andautoas the default quality.Adapter option coverage
packages/ai-grok/tests/grok-adapter.test.tsTests cover Image 2.0 size and option forwarding, rejection of the new sizes by older Imagine models, and model-specific type checks. End-to-end option coverage
testing/e2e/src/routes/api.grok-image-2-options.ts,testing/e2e/src/routeTree.gen.ts,testing/e2e/tests/grok-image-2-options.spec.tsA POST route requests images for three sizes and records each result or error. The route tree registers the endpoint, and Playwright tests check each size’s response. Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Sequence Diagram(s)#
sequenceDiagram participant Playwright participant GrokImage2OptionsRoute participant GrokImage2Adapter participant MockImageAPI Playwright->>GrokImage2OptionsRoute: POST image-size request GrokImage2OptionsRoute->>GrokImage2Adapter: Generate image with size and auto quality GrokImage2Adapter->>MockImageAPI: Send image request MockImageAPI-->>GrokImage2Adapter: Return image result GrokImage2Adapter-->>GrokImage2OptionsRoute: Return generated image GrokImage2OptionsRoute-->>Playwright: Return image count or errorSuggested reviewers:
tombeckenham,alemtuzlak
Merge Risk:
Minimal · up to102a9-
This change lets
grok-imagine-image-2.0accept the21:9and5:2aspect ratios, the1.5kresolution andquality: 'auto'. The older Imagine models keep their existing accepted sizes. No concrete defect was found in the change, and unit and end-to-end tests cover the new options. The remaining risk is minimal.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the package, the models, and the four new values (21:9, 5:2, 1.5k, quality auto), which matches the changeset. It is specific and accurate. The title uses the 'fix' prefix even though … Description check The description covers the problem, the fix, the changed files, the testing done, the risk and rollback plan, and the public API change. It follows the template's Changes and Checklist sections, and i… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Title check
-
Explanation
The title names the package, the models, and the four new values (21:9, 5:2, 1.5k, quality auto), which matches the changeset. It is specific and accurate. The title uses the 'fix' prefix even though the change adds new values; the core subject is still the main change.
Full details: Description check
-
Explanation
The description covers the problem, the fix, the changed files, the testing done, the risk and rollback plan, and the public API change. It follows the template's Changes and Checklist sections, and it adds Release Impact. Two checklist items are unchecked: the author did not run 'pnpm run test:pr' as a whole, and the 'I fully understand the code' item is open. The template asks for tests or a reason they do not apply, and the author gives a partial reason. The description is mostly complete.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/ai
- Branch: main
- File: .coderabbit.yaml
- Commit: f302e8b
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
2e5221c0-c4e0-4b88-a9b0-108824b787fc
Commits-
Reviewing files that changed from the base of the PR and between f302e8b77e5632de82f16eddb79ef4294ab9809e and 102a96f6eef3f1c81c8be194c05c7f77657adeb9.
Files selected for processing: 8-
.changeset/grok-image-2-options.mddocs/adapters/grok.mddocs/config.jsonpackages/ai-grok/src/image/image-provider-options.tspackages/ai-grok/tests/grok-adapter.test.tstesting/e2e/src/routeTree.gen.tstesting/e2e/src/routes/api.grok-image-2-options.tstesting/e2e/tests/grok-image-2-options.spec.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
-
-
View your CI Pipeline Execution ↗ for commit 102a96f6eef3f1c81c8be194c05c7f77657adeb9
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 1m 47s View ↗ nx run-many --targets=test:coverage --projects=...✅ Succeeded <1s View ↗ nx run-many --targets=build --exclude=examples/...✅ Succeeded 16s View ↗ 💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗
☁️ Nx Cloud last updated this comment at
2026-11-30 17:35:45UTCAlemTuzlak merged this2d49b19
AlemTuzlak closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 102a96f6eef3f1c81c8be194c05c7f77657adeb9
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR