|
2 | 2 | title: Grok (xAI) |
3 | 3 | id: grok-adapter |
4 | 4 | order: 5 |
5 | | -description: "Use xAI Grok Responses models with TanStack AI — Grok 4.3 and Grok Build 0.1 via @tanstack/ai-grok." |
| 5 | +description: "Use xAI Grok models with TanStack AI — Grok 4.3, Grok Build 0.1, Grok Imagine image generation, and Grok Imagine video generation via @tanstack/ai-grok." |
6 | 6 | keywords: |
7 | 7 | - tanstack ai |
8 | 8 | - grok |
9 | 9 | - xai |
10 | 10 | - grok 4.3 |
11 | 11 | - grok build |
| 12 | + - image generation |
| 13 | + - video generation |
| 14 | + - grok imagine |
12 | 15 | - adapter |
13 | 16 | --- |
14 | 17 |
|
15 | | -The Grok text and summarization adapters provide access to xAI's Responses API for `grok-4.3` and `grok-build-0.1`. |
| 18 | +The Grok text and summarization adapters provide access to xAI's Responses API for `grok-4.3` and `grok-build-0.1`, plus Grok Imagine image generation and Grok Imagine video generation. |
16 | 19 |
|
17 | 20 | ## Installation |
18 | 21 |
|
@@ -203,6 +206,67 @@ reachable; use a `data` source for private images. `grok-2-image-1212` is |
203 | 206 | text-to-image only — image prompt parts are a compile-time type error and |
204 | 207 | throw at runtime. |
205 | 208 |
|
| 209 | +## Video Generation (Experimental) |
| 210 | + |
| 211 | +Generate short video clips (1–15 seconds, with audio) with the Grok Imagine video models via xAI's asynchronous jobs/polling API. |
| 212 | + |
| 213 | +Available models: |
| 214 | + |
| 215 | +- `grok-imagine-video` (v1.0) — text-to-video and image-to-video, $0.05 per second of video. |
| 216 | +- `grok-imagine-video-1.5` — **image-to-video only**, $0.08 per second of video. A text-only prompt is rejected by the API; the adapter fails fast with a clear error telling you to add a starting-frame image or use `grok-imagine-video`. |
| 217 | + |
| 218 | +Text-to-video with the base `grok-imagine-video` model: |
| 219 | + |
| 220 | +```typescript |
| 221 | +import { generateVideo, getVideoJobStatus } from "@tanstack/ai"; |
| 222 | +import { grokVideo } from "@tanstack/ai-grok"; |
| 223 | + |
| 224 | +const adapter = grokVideo("grok-imagine-video"); |
| 225 | + |
| 226 | +// 1. Create the job |
| 227 | +const { jobId } = await generateVideo({ |
| 228 | + adapter, |
| 229 | + prompt: "A red panda balancing on a bamboo stalk in the rain", |
| 230 | + size: "16:9_720p", // "aspectRatio" or "aspectRatio_resolution" |
| 231 | + duration: 5, // integer seconds, 1–15 |
| 232 | +}); |
| 233 | + |
| 234 | +// 2. Poll until complete, then read the video URL |
| 235 | +let status = await getVideoJobStatus({ adapter, jobId }); |
| 236 | +while (status.status !== "completed" && status.status !== "failed") { |
| 237 | + await new Promise((r) => setTimeout(r, 5000)); |
| 238 | + status = await getVideoJobStatus({ adapter, jobId }); |
| 239 | +} |
| 240 | + |
| 241 | +console.log(status.url); // hosted .mp4 URL |
| 242 | +``` |
| 243 | + |
| 244 | +For image-to-video (required for `grok-imagine-video-1.5`, optional for `grok-imagine-video`), include an `image` prompt part as the starting frame and describe the desired motion in the text part. URL sources are fetched by xAI's servers (so they must be publicly reachable); use a `data` source for a base64 starting frame: |
| 245 | + |
| 246 | +```typescript |
| 247 | +const { jobId } = await generateVideo({ |
| 248 | + adapter: grokVideo("grok-imagine-video-1.5"), |
| 249 | + prompt: [ |
| 250 | + { |
| 251 | + type: "text", |
| 252 | + content: "Make the waterfall crash down and slowly pan out the camera", |
| 253 | + }, |
| 254 | + { |
| 255 | + type: "image", |
| 256 | + source: { type: "url", value: "https://example.com/waterfall-still.png" }, |
| 257 | + }, |
| 258 | + ], |
| 259 | + size: "16:9_720p", |
| 260 | + duration: 10, |
| 261 | +}); |
| 262 | +``` |
| 263 | + |
| 264 | +Like the Grok Imagine image models, sizing is aspect-ratio based: the `size` option takes an `aspectRatio_resolution` template. Supported aspect ratios are `1:1`, `16:9`, `9:16`, `4:3`, `3:4`, `3:2`, and `2:3`; supported resolutions are `480p`, `720p`, and `1080p` (e.g. `"9:16_1080p"`). The resolution suffix is optional. |
| 265 | + |
| 266 | +When the job completes, the adapter reports usage on the result: `usage.unitsBilled` carries the billed seconds of video and `usage.cost` the exact cost in USD, both as returned by the xAI API. |
| 267 | + |
| 268 | +See [Video Generation](../media/video-generation) for the full jobs/polling flow, streaming mode, and the `useGenerateVideo` hook. |
| 269 | + |
206 | 270 | ## Text-to-Speech |
207 | 271 |
|
208 | 272 | Generate speech with Grok TTS: |
@@ -298,6 +362,10 @@ Creates a Grok summarization adapter with an explicit API key. |
298 | 362 |
|
299 | 363 | Creates a Grok image generation adapter. |
300 | 364 |
|
| 365 | +### `grokVideo(model, config?)` / `createGrokVideo(model, apiKey, config?)` |
| 366 | + |
| 367 | +Creates a Grok video generation adapter (experimental) for the Grok Imagine video models (`'grok-imagine-video'`, `'grok-imagine-video-1.5'`). |
| 368 | + |
301 | 369 | ### `grokSpeech(model, config?)` / `createGrokSpeech(model, apiKey, config?)` |
302 | 370 |
|
303 | 371 | Creates a Grok text-to-speech adapter. |
|
0 commit comments