|
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 |
|
@@ -229,6 +232,67 @@ reachable; use a `data` source for private images. `grok-2-image-1212` is |
229 | 232 | text-to-image only — image prompt parts are a compile-time type error and |
230 | 233 | throw at runtime. |
231 | 234 |
|
| 235 | +## Video Generation (Experimental) |
| 236 | + |
| 237 | +Generate short video clips (1–15 seconds, with audio) with the Grok Imagine video models via xAI's asynchronous jobs/polling API. |
| 238 | + |
| 239 | +Available models: |
| 240 | + |
| 241 | +- `grok-imagine-video` (v1.0) — text-to-video and image-to-video, $0.05 per second of video. |
| 242 | +- `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`. |
| 243 | + |
| 244 | +Text-to-video with the base `grok-imagine-video` model: |
| 245 | + |
| 246 | +```typescript |
| 247 | +import { generateVideo, getVideoJobStatus } from "@tanstack/ai"; |
| 248 | +import { grokVideo } from "@tanstack/ai-grok"; |
| 249 | + |
| 250 | +const adapter = grokVideo("grok-imagine-video"); |
| 251 | + |
| 252 | +// 1. Create the job |
| 253 | +const { jobId } = await generateVideo({ |
| 254 | + adapter, |
| 255 | + prompt: "A red panda balancing on a bamboo stalk in the rain", |
| 256 | + size: "16:9_720p", // "aspectRatio" or "aspectRatio_resolution" |
| 257 | + duration: 5, // integer seconds, 1–15 |
| 258 | +}); |
| 259 | + |
| 260 | +// 2. Poll until complete, then read the video URL |
| 261 | +let status = await getVideoJobStatus({ adapter, jobId }); |
| 262 | +while (status.status !== "completed" && status.status !== "failed") { |
| 263 | + await new Promise((r) => setTimeout(r, 5000)); |
| 264 | + status = await getVideoJobStatus({ adapter, jobId }); |
| 265 | +} |
| 266 | + |
| 267 | +console.log(status.url); // hosted .mp4 URL |
| 268 | +``` |
| 269 | + |
| 270 | +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: |
| 271 | + |
| 272 | +```typescript |
| 273 | +const { jobId } = await generateVideo({ |
| 274 | + adapter: grokVideo("grok-imagine-video-1.5"), |
| 275 | + prompt: [ |
| 276 | + { |
| 277 | + type: "text", |
| 278 | + content: "Make the waterfall crash down and slowly pan out the camera", |
| 279 | + }, |
| 280 | + { |
| 281 | + type: "image", |
| 282 | + source: { type: "url", value: "https://example.com/waterfall-still.png" }, |
| 283 | + }, |
| 284 | + ], |
| 285 | + size: "16:9_720p", |
| 286 | + duration: 10, |
| 287 | +}); |
| 288 | +``` |
| 289 | + |
| 290 | +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. |
| 291 | + |
| 292 | +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. |
| 293 | + |
| 294 | +See [Video Generation](../media/video-generation) for the full jobs/polling flow, streaming mode, and the `useGenerateVideo` hook. |
| 295 | + |
232 | 296 | ## Text-to-Speech |
233 | 297 |
|
234 | 298 | Generate speech with Grok TTS: |
@@ -325,6 +389,10 @@ Creates a Grok summarization adapter with an explicit API key. |
325 | 389 |
|
326 | 390 | Creates a Grok image generation adapter. |
327 | 391 |
|
| 392 | +### `grokVideo(model, config?)` / `createGrokVideo(model, apiKey, config?)` |
| 393 | + |
| 394 | +Creates a Grok video generation adapter (experimental) for the Grok Imagine video models (`'grok-imagine-video'`, `'grok-imagine-video-1.5'`). |
| 395 | + |
328 | 396 | ### `grokSpeech(model, config?)` / `createGrokSpeech(model, apiKey, config?)` |
329 | 397 |
|
330 | 398 | Creates a Grok text-to-speech adapter. |
|
0 commit comments