Stability AI provider for the Vercel AI SDK
npm install stability-ai-providerA custom Vercel AI SDK provider for Stability AI image generation models.
``bash`
npm install stability-ai-provider aior
bun add stability-ai-provider aior
pnpm add stability-ai-provider ai
Set your Stability AI API key as an environment variable:
`bash`
export STABILITYAI_API_KEY="your-api-key"
Or pass it directly when creating the provider:
`typescript
import { createStabilityAI } from "stability-ai-provider";
const stabilityai = createStabilityAI({
apiKey: "your-api-key",
});
`
`typescript
import { stabilityai } from "stability-ai-provider";
import { generateImage } from "ai";
const { image } = await generateImage({
model: stabilityai.image("sd3.5-large"),
prompt: "A serene mountain landscape at sunset with vibrant colors",
});
console.log(image.base64); // Base64-encoded image data
`
`typescript
import { stabilityai } from "stability-ai-provider";
import { generateImage } from "ai";
const { images } = await generateImage({
model: stabilityai.image("sd3.5-large"),
prompt: "A cyberpunk city at night",
n: 2, // Generate 2 images
aspectRatio: "16:9",
seed: 12345, // For reproducible results
providerOptions: {
"stability-ai": {
stylePreset: "neon-punk",
cfgScale: 8,
steps: 50,
negativePrompt: "blurry, low quality",
},
},
});
`
`typescript
import { createStabilityAI } from "stability-ai-provider";
import { generateImage } from "ai";
const stabilityai = createStabilityAI({
apiKey: process.env.MY_STABILITY_KEY,
headers: {
"X-Custom-Header": "value",
},
});
const { image } = await generateImage({
model: stabilityai.image("core"),
prompt: "A beautiful forest",
});
`
| Model ID | Description |
|----------|-------------|
| sd3.5-large | Stable Diffusion 3.5 Large - Highest quality |sd3.5-large-turbo
| | Stable Diffusion 3.5 Large Turbo - Fast generation |sd3.5-medium
| | Stable Diffusion 3.5 Medium - Balanced |sd3-large
| | Stable Diffusion 3 Large |sd3-large-turbo
| | Stable Diffusion 3 Large Turbo |sd3-medium
| | Stable Diffusion 3 Medium |core
| | Stable Image Core - General purpose |
Pass these in providerOptions["stability-ai"]:
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| stylePreset | string | - | Style preset (see below) |cfgScale
| | number | 7 | Prompt adherence (1-35) |steps
| | number | 50 | Inference steps (10-150) |outputFormat
| | string | "png" | Output format: "png", "jpeg", "webp" |negativePrompt
| | string | - | What to avoid in generation |
Available style presets:
- enhance - General enhancementanime
- - Anime stylephotographic
- - Photorealisticdigital-art
- - Digital art stylecomic-book
- - Comic book stylefantasy-art
- - Fantasy artline-art
- - Line artanalog-film
- - Film photography lookneon-punk
- - Neon/cyberpunk styleisometric
- - Isometric viewlow-poly
- - Low polygon styleorigami
- - Paper craft stylemodeling-compound
- - Clay/modeling lookcinematic
- - Cinematic style3d-model
- - 3D rendered lookpixel-art
- - Pixel art styletile-texture
- - Seamless texture
Creates a new Stability AI provider instance.
Options:
- apiKey?: string - API key (defaults to STABILITYAI_API_KEY env var)baseURL?: string
- - Custom API base URLheaders?: Record
- - Custom headersfetch?: typeof fetch
- - Custom fetch implementation
Creates an image model instance.
Parameters:
- modelId: string - The model ID to usesettings?: StabilityAIImageSettings
- - Default settings for this model
- Node.js 18+
- Stability AI API key
- ai` package (peer dependency)
MIT