翻篇收录说明
翻篇网页快照

Black Forest Labs | AI SDK

https://ai-sdk.dev/providers/ai-sdk-providers/black-forest-labs

最近抓取:2026-12-06 14:47:51(北京时间)

最近内容变化:2026-12-04 03:45:19(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Learn how to use Black Forest Labs with AI Gateway and the direct provider.

正文摘录

AI SDK ProvidersMenuBlack Forest LabsCopy for LLMView MarkdownAsk AI about this pageBlack Forest Labs provides a generative media platform for developers with FLUX-based models. Their platform offers fast, high quality, and in-context image generation and editing, plus video generation with FLUX 3. You can use Black Forest Labs models through AI Gateway (recommended) or connect directly with @ai-sdk/black-forest-labs. GatewayBlack Forest Labsbfl/flux-2-proimport { generateImage } from 'ai'; const { image } = await generateImage({ model: "bfl/flux-2-pro", prompt: 'A sunlit mountain village in watercolor.', }); console.log(image.base64); Using AI Gateway The AI SDK uses AI Gateway by default for model strings. Gateway provides access to models across providers through a single API key, with usage and cost tracking. Install ai: pnpmnpmyarnbun$ pnpm add ai Set an AI Gateway API key in your environment: export AI_GATEWAY_API_KEY=your_api_key_here Vercel deployments can also use OIDC authentication. See the AI Gateway provider documentation for configuration and the model catalog for available models. Gateway feature and provider-option support depends on the model and the provider serving the request. See Gateway provider options. Using @ai-sdk/black-forest-labs For direct access to Black Forest Labs, install ai and @ai-sdk/black-forest-labs: pnpmnpmyarnbun$ pnpm add ai @ai-sdk/black-forest-labs Set your Black Forest Labs API credential in the BFL_API_KEY environment variable: export BFL_API_KEY=your_api_key_here AI Gateway does not expose every API in @ai-sdk/black-forest-labs. Use the direct provider for provider-specific APIs or custom connection settings. The following sections document the direct @ai-sdk/black-forest-labs provider, including its model factories and settings. Gateway model and feature availability can differ. Provider Instance You can import the default provider instance blackForestLabs from @ai-sdk/black-forest-labs: import { blackForestLabs } from '@ai-sdk/black-forest-labs'; If you need a customized setup, you can import createBlackForestLabs and create a provider instance with your settings: import { createBlackForestLabs } from '@ai-sdk/black-forest-labs'; const blackForestLabs = createBlackForestLabs({ apiKey: 'your-api-key', // optional, defaults to BFL_API_KEY environment variable baseURL: 'custom-url', // optional headers: { /* custom headers */ }, // optional }); You can use the following optional settings to customize the Black Forest Labs provider instance: baseURL string Use a different URL prefix for API calls, e.g. to use a regional endpoint. The default prefix is https://api.bfl.ai/v1. apiKey string | PromiseLike<string> | (() => string | PromiseLike<string>) Pass a function to resolve credentials before each request, including when keys rotate. The function can return a string or a promise. Credential caching and refresh are managed by the function. API key that is being sent using the x-key header. It defaults to the BFL_API_KEY environment variable. headers Record<string,string> Custom headers to include in the requests. fetch (input: RequestInfo, init?: RequestInit) => Promise<Response> Custom fetch implementation. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing. pollIntervalMillis number Interval in milliseconds between polling attempts when waiting for generation to complete. Defaults to 500ms for image models and 2000ms for video models. pollTimeoutMillis number Overall timeout in milliseconds for polling before giving up. Defaults to 60000ms (60 seconds) for image models and 600000ms (10 minutes) for video models. Image Models You can create Black Forest Labs image models using the .image() factory method. For more on image generation with the AI SDK see generateImage(). Basic Usage import { writeFileSync } from 'node:fs'; import { blackForestLabs } from '@ai-sdk/black-forest-labs'; import { generateImage } from 'ai'; const { image, providerMetadata } = await generateImage({ model: blackForestLabs.image('flux-pro-1.1'), prompt: 'A serene mountain landscape at sunset', }); const filename = `image-${Date.now()}.png`; writeFileSync(filename, image.uint8Array); console.log(`Image saved to ${filename}`); Model Capabilities Use blackForestLabsModelCapabilities(modelId) from @ai-sdk/black-forest-labs to look up a model's declared capabilities and provider options, grouped by endpoint. Black Forest Labs offers many models optimized for different use cases. Here are a few popular examples. For a full list of models, see the Black Forest Labs Models Page. ModelDescriptionSizesImage EditingMask InputMax Images Per Callflux-3.1-imageFLUX 3.1 Image for text-to-image and editing with up to ten reference images, in fast or quality mode, up to 4KAccepted size or aspect ratio depends on the model and operation.1flux-3-imageFLUX 3 for text-to-image and editing with up to ten reference images and optional web groundingAccepted size or aspect ratio depends on the model and operation.1flux-3-image-kleinFLUX 3 Image Klein for fast generation and editing, with open weights and exact output dimensionsAccepted size or aspect ratio depends on the model and operation.1flux-kontext-proFLUX.1 Kontext [pro] handles both text and reference images as inputs, enabling targeted edits and complex transformationsAccepted size or aspect ratio depends on the model and operation.1flux-kontext-maxFLUX.1 Kontext [max] with improved prompt adherence and typography generationAccepted size or aspect ratio depends on the model and operation.1flux-pro-1.1-ultraUltra-fast, ultra high-resolution image creationAccepted size or aspect ratio depends on the model and operation.1flux-pro-1.1Fast, high-quality image generation from text.Accepted size or aspect ratio depends on the model and operation.1flux-pro-1.0-fillInpainting model for filling masked regions of images with new contentAccepted size or aspect ratio depends on the model and operation.1 Unknown image model IDs use the latest supported image API, currently FLUX 3.1 Image's request format and options. Known older families (flux-pro-*, flux-kontext-*, flux-1-*, and flux-2-*) retain the legacy format, and flux-3-image-klein IDs retain Klein's format. The original flux-3-image retains its distinct grounding and version controls. FLUX.1 and FLUX.2 models support aspect ratios from 3:7 (portrait) to 7:3 (landscape). FLUX 3 accepts only 21:9, 2:1, 16:9, 3:2, 7:5, 4:3, 5:4, 1:1, 4:5, 3:4, 5:7, 2:3, 9:16, 1:2, and 9:21. Omit aspectRatio to use the endpoint's auto default. Unsupported ratios are omitted with a warning. FLUX.2 models take reference images on the same prompt.images field as the Kontext models, so a single model ID covers both text-to-image and editing. See Image Editing below. FLUX.2 generations can take longer than the 60 second default polling timeout, especially with reference images. Raise pollTimeoutMillis when a request reports Black Forest Labs generation timed out. FLUX 3 Image Generation and Editing The flux-3-image and flux-3-image-klein models generate images from text and edit reference images through their respective endpoints. Pass reference images through prompt.images and select the output resolution with providerOptions.blackForestLabs.resolution: import { blackForestLabs, type BlackForestLabsImageModelOptions, } from '@ai-sdk/black-forest-labs'; import { generateImage } from 'ai'; const { image } = await generateImage({ model: blackForestLabs.image('flux-3-image'), prompt: 'A watercolor illustration of a coastal village at dawn', aspectRatio: '16:9', providerOptions: { blackForestLabs: { resolution: '2k', grounding: false, version: 'latest', pollTimeoutMillis: 240000, } satisfies BlackForestLabsImageModelOptions, }, }); For editing, use a prompt such as: const { image } = await generateImage({ model: blackForestLabs.image('flux-3-image'), prompt: { text: 'Change the sky to sunset while preserving the buildings', images: ['https://example.com/village.png'], }, }); Both FLUX 3 Image models accept up to ten reference images as URLs or base64-encoded data. For flux-3-image, each reference image must be at least 256 × 256 pixels and no larger than 16 megapixels. Omitting aspectRatio lets the endpoint choose the ratio from the prompt or the first reference image. Text-to-image generation defaults to 1k. Klein edits default to the first reference image's size. Web grounding is available only on flux-3-image and is enabled by default. The flux-3-image model does not accept seed, masks, explicit width/height, or the legacy image options such as outputFormat, steps, and promptUpsampling. A size is used only to derive a supported aspect ratio; ratios outside the list above are omitted with a warning. Use resolution to select the pixel-area tier. Unsupported options are omitted with warnings; masks are rejected. FLUX 3 Image Klein Klein uses the same resolution, aspectRatio, and prompt.images inputs as flux-3-image. It also accepts seed, outputFormat ('jpeg' or 'png'), and steps (1–16, default 16): const { image } = await generateImage({ model: blackForestLabs.image('flux-3-image-klein'), prompt: { text: 'Change the sky to sunset while preserving the buildings', images: ['https://example.com/village.png'], }, aspectRatio: '16:9', seed: 42, providerOptions: { blackForestLabs: { resolution: '2k', outputFormat: 'png', steps: 16, pollTimeoutMillis: 240000, } satisfies BlackForestLabsImageModelOptions, }, }); To request exact dimensions, pass size (for example, '2048x2048') or set both providerOptions.blackForestLabs.width and height. Each dimension must be an integer from 64 to 6256, and their product must not exceed 16,822,272 pixels. Provider dimensions override size; exact dimensions override resolution and aspectRatio. Klein does not accept masks, grounding, or version. FLUX 3.1 Image flux-3.1-image generates and edits images up to 4K, and unknown model IDs use the same request for