Skip to content

feat(xai): add grok imagine image generation, edit, and video download - #40238

Open
HXhlx wants to merge 3 commits into
BerriAI:mainfrom
HXhlx:cursor/xai-grok-imagine-image-video-7688
Open

HXhlx wants to merge 3 commits into
BerriAI:mainfrom
HXhlx:cursor/xai-grok-imagine-image-video-7688

Conversation

@HXhlx

@HXhlx HXhlx commented Sep 8, 2026 •

Copy link
Copy Markdown

TLDR

Problem this solves:

  • Staging cannot generate or edit images on grok-imagine-image
  • Staging cannot create or download grok-imagine-video jobs
  • Image edit rejects an http(s) URL or data:image URI that xAI accepts
  • A created xAI video was logged at $0, so budgets never tripped

How it solves it:

  • Route xAI Imagine image generate and edit through the existing xAI provider
  • Route xAI Imagine video create, status, and CDN download the same way
  • Accept a URL or data:image URI on POST /v1/images/edits
  • Bill video creation for the requested duration and resolution

This supersedes the Imagine slice of #31611 (closed; mixed production branch). SuperGrok multi-account OAuth failover is left for a separate PR. Staging already has single-account use_xai_oauth

User Flow

Before: a developer on the proxy cannot use Grok Imagine at all, and nothing is billed

  1. They send POST https://litellm-domain/v1/images/generations with "model": "xai/grok-imagine-image" and get an unsupported provider error
  2. They send POST https://litellm-domain/v1/videos with "model": "xai/grok-imagine-video" and get an unsupported provider error

After: the same requests work end to end and video spend shows up against the key's budget

  1. POST https://litellm-domain/v1/images/generations returns image URLs or base64 data from xAI
  2. POST https://litellm-domain/v1/images/edits with an image URL or data:image URI returns the edited image
  3. POST https://litellm-domain/v1/videos with "seconds": "10" returns a video id, logged at the 10 second price for the chosen resolution
  4. GET https://litellm-domain/v1/videos/{id} reports progress, and GET https://litellm-domain/v1/videos/{id}/content downloads the mp4

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review)

Type

New Feature

Caveats (if any)

Medium

  • Live xAI curls not run yet: no XAI_API_KEY in the dev environment
  • Video cost uses the requested duration, since xAI's create response has no usage

Low

  • Video remix, list, and delete are not implemented for xAI

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@HXhlx
HXhlx requested a review from a team September 8, 2026 11:16
@HXhlx
HXhlx requested a review from mateo-berri as a code owner September 8, 2026 11:16
@CLAassistant

CLAassistant commented Sep 8, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds xAI Imagine image generation and editing, video creation and retrieval, provider registration, model metadata, proxy request parsing, and focused tests

  • Adds xAI image generation and JSON-based image editing transformations
  • Adds xAI video creation, status polling, and CDN download support
  • Extends proxy image edits to accept HTTP URLs and data-image URIs
  • Adds model/provider discovery and endpoint capability metadata
  • Includes unit and proxy endpoint coverage for the new paths

Confidence Score: 2/5

This PR is not safe to merge until xAI status routing selects the correct provider and CDN downloads validate their destination

Plain xAI status requests can be dispatched through OpenAI despite an xAI query model, and response-controlled CDN URLs bypass destination validation

Files Needing Attention: litellm/proxy/video_endpoints/endpoints.py, litellm/llms/xai/videos/transformation.py

Security Review

The xAI video download path follows a URL supplied by the upstream response without applying the repository's destination-validation helpers, enabling server-side requests to internal destinations when a configured upstream controls that response

Important Files Changed

Filename Overview
litellm/llms/xai/videos/transformation.py Adds xAI video transformations, but the CDN download follows an unvalidated response-controlled URL
litellm/proxy/video_endpoints/endpoints.py Adds model recovery for video status and content, but status retains an OpenAI default that overrides xAI model inference
litellm/proxy/video_endpoints/utils.py Adds model resolution from encoded IDs or query parameters
litellm/llms/xai/image_edit/transformation.py Adds xAI image-edit parameter, authentication, request, and response transformations
litellm/llms/xai/image_generation/transformation.py Adds xAI image-generation parameter, authentication, request, and response transformations
litellm/proxy/image_endpoints/endpoints.py Extends image-edit parsing to support uploaded files, HTTP URLs, and data-image URIs
litellm/videos/main.py Adds provider inference from video IDs and model metadata for status and content operations
litellm/images/main.py Routes xAI image generation through the shared HTTP transformation handler

Comments Outside Diff (1)

  1. litellm/proxy/video_endpoints/endpoints.py, line 261 (link)

    P1 Wrong video provider

    With a plain xAI ID, ?model=grok-imagine-video still selects OpenAI, so status requests use the wrong provider and fail

Reviews (1): Last reviewed commit: "fix(proxy): accept image-edit URL and da..." | Re-trigger Greptile

Comment on lines +330 to +331
httpx_client: Final[HTTPHandler] = _get_httpx_client()
video_response: Final = httpx_client.get(url)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 security Unvalidated CDN destination

A configured upstream can return an internal video.url, which this direct GET follows without destination validation, enabling server-side access to internal networks

How this was verified: The response-controlled video.url reaches an ordinary shared-client GET without the repository's safe-fetch validation

@veria-ai

veria-ai Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 4 · PR risk: 0/10

@codspeed

codspeed Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 35 untouched benchmarks


Comparing HXhlx:cursor/xai-grok-imagine-image-video-7688 (9c3a560) with main (ecdc0f9)

Open in CodSpeed

@codecov

codecov Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

@HXhlx
HXhlx force-pushed the cursor/xai-grok-imagine-image-video-7688 branch from ce8df28 to 1d8942f Compare September 9, 2026 08:21
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 14:39
@HXhlx
HXhlx force-pushed the cursor/xai-grok-imagine-image-video-7688 branch from 24d409d to c510bd1 Compare September 27, 2026 10:54
@HXhlx
HXhlx force-pushed the cursor/xai-grok-imagine-image-video-7688 branch 2 times, most recently from 005b9f1 to 2ff9f1b Compare October 5, 2026 11:13
@HXhlx

HXhlx commented Oct 5, 2026

Copy link
Copy Markdown
Author

rust-wheel fails on the 45 MB native-size guard, main hit the same failure with #44580. No Rust changes in this PR

@HXhlx
HXhlx force-pushed the cursor/xai-grok-imagine-image-video-7688 branch from 2974e8c to f850f2d Compare October 15, 2026 12:39
@HXhlx
HXhlx requested a review from klu-berri as a code owner October 15, 2026 12:39
@greptile-apps

greptile-apps Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Automatic reviews are disabled for this repository.

@HXhlx

HXhlx commented Oct 15, 2026

Copy link
Copy Markdown
Author

@greptileai review

@HXhlx
HXhlx force-pushed the cursor/xai-grok-imagine-image-video-7688 branch 2 times, most recently from 0882d9e to 0c471a9 Compare October 17, 2026 03:08
@HXhlx
HXhlx force-pushed the cursor/xai-grok-imagine-image-video-7688 branch from 0c471a9 to 9c3a560 Compare October 24, 2026 03:24
@HXhlx
HXhlx requested a review from joshua-berri as a code owner October 24, 2026 03:24
HXhlx added 3 commits October 31, 2026 10:51
The xAI create response only carries request_id, so usage was empty and
completion_cost priced every created video at zero seconds. Derive
usage.duration_seconds and usage.video_resolution from the request body
sent to xAI, and forward the resolution param that was being dropped.

Also cover the remaining image edit/generation, video, and ownership
behaviors that had no tests.
…deo downloads

Video content hooks now take the client the HTTP handler passes in, image
responses are parsed through one typed helper that raises a provider error
when no images come back, and video provider resolution no longer rebinds
the custom_llm_provider argument
@HXhlx
HXhlx force-pushed the cursor/xai-grok-imagine-image-video-7688 branch from 9c3a560 to f5f3cce Compare October 31, 2026 02:52
@HXhlx

HXhlx commented Nov 2, 2026

Copy link
Copy Markdown
Author

@ryan-crabbe-berri @mateo-berri @joshua-berri @kerry-berri @klu-berri all five PRs are rebased onto main and merge clean, CI awaits a workflow approval, reviews welcome when you get a chance

This branch has not been deployed

No deployments
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.

4 participants