OffRail
Developers

Image Generation with the AI SDK

Generate images with generateImage or stream image output through chat using the AI SDK

The @ai-sdk/openai package supports image generation both through the AI SDK's dedicated generateImage function and through chat-based image models that stream images as part of a conversation.

generateImage

Use offrail.image() to get an image model:

import { createOpenAI } from "@ai-sdk/openai";
import { generateImage } from "ai";
import { writeFileSync } from "fs";

const offrail = createOpenAI({
	apiKey: process.env.OFFRAIL_API_KEY,
	baseURL: "https://api.offrail.ai/v1",
});

const result = await generateImage({
	model: offrail.image("gemini-3-pro-image-preview"),
	prompt:
		"A cozy cabin in a snowy mountain landscape at night with aurora borealis",
	size: "1024x1024",
	n: 1,
	// aspectRatio and quality are model-specific — only some providers honor them.
	// aspectRatio works on Gemini image models; OpenAI gpt-image-2 ignores it
	// (use a literal WxH `size` instead).
	aspectRatio: "16:9",
	// quality works on OpenAI gpt-image-2 ("low" | "medium" | "high" | "auto").
	// The AI SDK only forwards it through providerOptions.
	providerOptions: {
		offrail: { quality: "high" },
	},
});

result.images.forEach((image, i) => {
	const buf = Buffer.from(image.base64, "base64");
	writeFileSync(`image-${i}.png`, buf);
});

Which sizes, aspect ratios, and quality settings a model accepts depends on the model — see Image Generation for the full parameter reference and per-model behavior.

Chat-based image models

Multimodal models like gemini-3-pro-image-preview can return images inside a chat conversation. Use offrail.chat() with streamText in a route handler:

// app/api/chat/route.ts
import { createOpenAI } from "@ai-sdk/openai";
import { convertToModelMessages, streamText } from "ai";

const offrail = createOpenAI({
	apiKey: process.env.OFFRAIL_API_KEY,
	baseURL: "https://api.offrail.ai/v1",
});

export async function POST(req: Request) {
	const { messages } = await req.json();

	const result = streamText({
		model: offrail.chat("gemini-3-pro-image-preview"),
		messages: convertToModelMessages(messages),
	});

	return result.toUIMessageStreamResponse();
}

On the client, image parts arrive as message file parts that you can render with the AI Elements Image component or a plain <img> tag with a data URL. See Image Generation for the complete useChat frontend example.

Video and audio

The AI SDK does not yet cover the gateway's video and speech endpoints — call them over REST instead:

How is this guide?

On this page

Ready for production?

Ship to production with SSO, audit logs, spend controls, and guardrails your security team will approve.

Explore Enterprise