|
| 1 | +--- |
| 2 | +title: '@tanstack/ai-schemas: provider endpoint schemas' |
| 3 | +id: ai-schemas |
| 4 | +--- |
| 5 | + |
| 6 | +`@tanstack/ai-schemas` is a separate package that ships **JSON Schema** and **Zod** schemas for the generation endpoints of every supported provider (OpenAI, Anthropic, Gemini, ElevenLabs, xAI Grok, FAL). The schemas are generated nightly from each provider's official OpenAPI spec, so they track upstream changes automatically. |
| 7 | + |
| 8 | +It complements the `model-meta.ts` shipped with each provider adapter: where `model-meta` describes coarse facts (context window, modalities, pricing), `ai-schemas` describes the rich per-endpoint constraint surface — allowed video durations, image sizes, voice IDs, prompt-length caps, etc. |
| 9 | + |
| 10 | +## When to reach for it |
| 11 | + |
| 12 | +- **Runtime validation before submitting a request** — surface bad inputs client-side instead of paying a round-trip and waiting on a vague upstream error. |
| 13 | +- **Discovering allowed values** — populate dropdowns or pick lists with the exact enum a model accepts. |
| 14 | +- **Feeding an LLM tool API** — each JSON Schema is self-contained (its `$ref` closure is bundled under `$defs`), so it can be passed straight to OpenAI / Anthropic / Gemini tool-use APIs without extra resolution. |
| 15 | +- **Optimising prompts across models** — the schemas expose every model's constraints in a comparable shape, which is the foundation for prompt-portability helpers. |
| 16 | + |
| 17 | +## Install |
| 18 | + |
| 19 | +```bash |
| 20 | +pnpm add @tanstack/ai-schemas |
| 21 | +# Optional: only required if you import from `@tanstack/ai-schemas/{provider}/{activity}/zod` |
| 22 | +pnpm add zod |
| 23 | +``` |
| 24 | + |
| 25 | +## Activity groups |
| 26 | + |
| 27 | +Each provider's endpoints are grouped by **activity**, matching the core library's activities (chat, generateImage, generateVideo, and the audio family — collapsed into a single `audio` group): |
| 28 | + |
| 29 | +| Activity | Covers | |
| 30 | +| ------------ | ----------------------------------------------------------------------- | |
| 31 | +| `chat` | Text generation: chat/completions, responses, messages, generateContent | |
| 32 | +| `image` | Image generation and edits | |
| 33 | +| `video` | Video generation, edits, extensions | |
| 34 | +| `audio` | All audio: TTS, transcription, music, sound effects, voices, dubbing | |
| 35 | +| `embeddings` | Embedding endpoints | |
| 36 | +| `moderation` | Moderation endpoints | |
| 37 | + |
| 38 | +Platform/admin endpoints (projects, invites, certificates, fine-tuning jobs, file stores, …) are excluded from generation entirely — they aren't generation constraint surface. |
| 39 | + |
| 40 | +## Subpath imports |
| 41 | + |
| 42 | +Provider-first — pick the provider, then the activity, then `json-schema` or `zod`: |
| 43 | + |
| 44 | +```ts |
| 45 | +// JSON Schemas (no `zod` peer required). |
| 46 | +import { geminiChatEndpointSchemaMap } from '@tanstack/ai-schemas/gemini/chat/json-schema' |
| 47 | + |
| 48 | +// Zod (requires `zod ^4`). |
| 49 | +import { openaiChatEndpointZodMap } from '@tanstack/ai-schemas/openai/chat/zod' |
| 50 | +import { elevenlabsAudioEndpointZodMap } from '@tanstack/ai-schemas/elevenlabs/audio/zod' |
| 51 | +import { falVideoEndpointZodMap } from '@tanstack/ai-schemas/fal/video/zod' |
| 52 | +``` |
| 53 | + |
| 54 | +There is **no aggregator barrel**. `import … from '@tanstack/ai-schemas/gemini/chat/json-schema'` only ships Gemini's chat JSON Schemas — no other provider's or activity's bytes leak into the consumer's bundle. |
| 55 | + |
| 56 | +Endpoints that stream binary media (e.g. ElevenLabs text-to-speech) map only an `input` schema — there is no JSON output to describe. |
| 57 | + |
| 58 | +## Validate a video-generation request |
| 59 | + |
| 60 | +```ts |
| 61 | +import { falVideoEndpointZodMap } from '@tanstack/ai-schemas/fal/video/zod' |
| 62 | + |
| 63 | +const result = falVideoEndpointZodMap[ |
| 64 | + 'fal-ai/kling-video/o3/pro/text-to-video' |
| 65 | +].input.safeParse({ |
| 66 | + prompt: 'A mecha lands on the ground to save the city, in anime style', |
| 67 | + duration: '8', |
| 68 | + aspect_ratio: '9:16', |
| 69 | +}) |
| 70 | + |
| 71 | +if (!result.success) console.error(result.error.issues) |
| 72 | +``` |
| 73 | + |
| 74 | +## Discover what a model supports |
| 75 | + |
| 76 | +```ts |
| 77 | +import { KlingVideoO3ProTextToVideoInputSchema } from '@tanstack/ai-schemas/fal/video/json-schema' |
| 78 | + |
| 79 | +KlingVideoO3ProTextToVideoInputSchema.properties.duration.enum |
| 80 | +// ['3', '4', …, '15'] |
| 81 | + |
| 82 | +KlingVideoO3ProTextToVideoInputSchema.properties.aspect_ratio.enum |
| 83 | +// ['16:9', '9:16', '1:1'] |
| 84 | +``` |
| 85 | + |
| 86 | +## OpenAI structured-outputs strict mode |
| 87 | + |
| 88 | +```ts |
| 89 | +import { toOpenAIStrict } from '@tanstack/ai-schemas/openai-strict' |
| 90 | +import { Veo3InputSchema } from '@tanstack/ai-schemas/fal/video/json-schema' |
| 91 | + |
| 92 | +await openai.chat.completions.create({ |
| 93 | + model: 'gpt-5', |
| 94 | + messages: [...], |
| 95 | + response_format: { |
| 96 | + type: 'json_schema', |
| 97 | + json_schema: { |
| 98 | + name: 'veo3_input', |
| 99 | + schema: toOpenAIStrict(Veo3InputSchema), |
| 100 | + strict: true, |
| 101 | + }, |
| 102 | + }, |
| 103 | +}) |
| 104 | +``` |
| 105 | + |
| 106 | +## How it stays current |
| 107 | + |
| 108 | +The `.github/workflows/sync-schemas.yml` workflow runs daily and: |
| 109 | + |
| 110 | +1. Fetches upstream OpenAPI specs for every provider. |
| 111 | +2. Re-runs `@hey-api/openapi-ts` to regenerate JSON Schemas + Zod. |
| 112 | +3. Bundles each schema's `$ref` closure, regenerates endpoint maps. |
| 113 | +4. If anything changed: bumps the package patch version, writes a changeset, opens an automated PR. |
| 114 | + |
| 115 | +Provider sources: |
| 116 | + |
| 117 | +| Provider | Source | Notes | |
| 118 | +| ---------- | ------------------------------------------------------------------------------- | ---------------------------------------------------- | |
| 119 | +| OpenAI | `github.com/openai/openai-openapi` | Public, no API key required. | |
| 120 | +| Anthropic | Stainless-generated OpenAPI (resolved via `anthropic-sdk-typescript/.stats.yml`) | Public. | |
| 121 | +| Gemini | `generativelanguage.googleapis.com/$discovery/rest?version=v1beta` | Google Discovery doc converted to OpenAPI in pipeline. | |
| 122 | +| ElevenLabs | `api.elevenlabs.io/openapi.json` | Public. | |
| 123 | +| xAI Grok | `docs.x.ai/openapi.json` | Public. | |
| 124 | +| FAL | `api.fal.ai/v1/models?status=active&expand=openapi-3.0` (per-model) | Needs `FAL_KEY`. Model categories regroup into the shared activities. | |
0 commit comments