Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai-grok): accept 21:9, 5:2, 1.5k and quality auto on grok-imagine-image-2.0 #2288 (GitHub #2287)

MergedJaaneek(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/2287/headmerged by AlemTuzlak View on GitHub

Jaaneek(GitHub profile, opens in a new tab)commented

generateImage() 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.0 gets its own aspect-ratio, resolution and size lists and types: the shared ones plus 21:9, 5:2 and 1.5k.
  • quality is 'low' | 'medium' | 'auto'. The documented default is 'auto'.
  • grok-imagine-image and grok-imagine-image-quality take 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:

✅ 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.0 for quality, 21:9 and 5:2 (migration guide), and they list 1.5K prices 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 tsc reports 3 errors.
  • With this change: 192 tests pass, and tsc reports no error.
  • oxfmt --check passes on the changed files. oxlint reports nothing new on packages/ai-grok/src.
  • Not run: pnpm run test:pr as a whole, and the new Playwright spec on this base.

Manual test.

  1. On @tanstack/ai-grok 1.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
  1. 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' with resolution: '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:9 and 5:2 aspect ratios, 1.5k resolution, and automatic quality selection.
    • Existing Grok Imagine models retain their previous supported image sizes.
  • Documentation
    • Updated the Grok image-generation documentation to include automatic quality selection as the default.
    1. 🦋 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
      NameType
      @tanstack/ai-grokPatch

      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

    2. AlemTuzlak approved these changes

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      Grok Image 2.0 adds support for aspect ratios 21:9 and 5:2, resolution 1.5k, and quality auto. 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.json
      Image 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 and auto as the default quality.
      Adapter option coverage
      packages/ai-grok/tests/grok-adapter.test.ts
      Tests 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.ts
      A 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 error

      Suggested reviewers: tombeckenham, alemtuzlak

      Merge Risk: Minimal · up to 102a9

      This change lets grok-imagine-image-2.0 accept the 21:9 and 5:2 aspect ratios, the 1.5k resolution and quality: '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: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe 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 checkPassedThe 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 checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck 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.

      Share

      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.md
      • docs/adapters/grok.md
      • docs/config.json
      • packages/ai-grok/src/image/image-provider-options.ts
      • packages/ai-grok/tests/grok-adapter.test.ts
      • testing/e2e/src/routeTree.gen.ts
      • testing/e2e/src/routes/api.grok-image-2-options.ts
      • testing/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.

    4. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 102a96f6eef3f1c81c8be194c05c7f77657adeb9

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded1m 47sView ↗
      nx run-many --targets=test:coverage --projects=...✅ Succeeded<1sView ↗
      nx run-many --targets=build --exclude=examples/...✅ Succeeded16sView ↗

      💡 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:45 UTC

    5. AlemTuzlak merged this2d49b19

    6. AlemTuzlak closed this

Sign in to join this conversation.