shadcn.io is an independent project built on shadcn/ui
On this page11 sections
Generated Image shows a picture a model made, from the base64 or the bytes the AI SDK's generateImage returns, in a frame of the aspect ratio you asked for. While it generates, the frame shimmers and says so, and if it fails, the frame says that instead. Because the frame has the picture's shape from the start, the reply doesn't jump when the image lands.
For a picture you already have at a URL, a plain img is enough.
Installation
Anatomy

Usage
import { GeneratedImage } from "@/components/ui/generated-image";Pass the image's base64 and mediaType. The frame fills the width it's given and takes its height from aspectRatio, square by default, so set the width with a class.
// `image` is what the AI SDK's `generateImage` returned.
const Result = (props: { image: { base64: string; mediaType: string } }) => (
<GeneratedImage
alt="Soft waves of violet and cyan light"
base64={props.image.base64}
className="max-w-64"
mediaType={props.image.mediaType}
/>
);Accessibility
The alt text is yours to write. Without one, the image renders with an empty alt, and screen readers skip it as decoration. The prompt is a fair start, though a line that says what the picture shows reads better than the instruction that made it. While it generates, the frame sets aria-busy and shows the words Generating image, and a failed one says The image couldn't be generated. The frame is a polite live region, so what replaces those words once generating ends is announced: the error's message, or the image by its alt. With an empty alt, a finished image adds nothing to announce.
Demos
States
Three frames side by side, one for each state. Generating draws the placeholder with a sweep of light and shimmering words, error tints the frame red with a broken-image icon and its own message, and done shows the image. Leave state out and the frame follows the data instead, generating until base64 or uint8Array arrives. Set it yourself when a tool call's status decides the frame.
Aspect ratio
A wide 16:9 image, a square placeholder and a tall 9:16 image, each in a narrower column than the last. aspectRatio takes the ratio in the form generateImage takes it, so passing the one you requested gives the placeholder the image's shape before the image exists. An aspect-video class on the frame works as well.
In a chat
The image inside an agent's reply, built on Message. It generates for three seconds with no state set, then comes into focus as soon as its data arrives, and Regenerate, disabled while it works, runs it again. The focus effect plays only for an image someone saw generating, so one already there on page load just appears.
AI SDK
generateImage runs on the server, and the image it returns is a class whose base64 is a getter, which JSON.stringify leaves out. Send base64 and mediaType back as plain fields.
import { openai } from "@ai-sdk/openai";
import { generateImage } from "ai";
export async function POST(request: Request) {
const { prompt } = await request.json();
const { image } = await generateImage({
model: openai.image("gpt-image-1"),
prompt,
size: "1536x1024",
});
return Response.json({ base64: image.base64, mediaType: image.mediaType });
}<GeneratedImage
alt={prompt}
aspectRatio="3:2"
base64={image?.base64}
mediaType={image?.mediaType}
/>In a chat, make image generation a tool. Its output goes back to the model on the next turn, so toModelOutput sends the model one line instead of the whole base64.
import { openai } from "@ai-sdk/openai";
import { generateImage, tool } from "ai";
import { z } from "zod";
const tools = {
generateImage: tool({
description: "Draw an image from a prompt",
inputSchema: z.object({ prompt: z.string() }),
execute: async ({ prompt }) => {
const { image } = await generateImage({
model: openai.image("gpt-image-1"),
prompt,
});
return { base64: image.base64, mediaType: image.mediaType };
},
toModelOutput: () => ({ type: "text", value: "The image is shown." }),
}),
};const { messages } = useChat();
const stateOf = (state: string) => {
if (state === "output-available") {
return "done";
}
return state === "output-error" ? "error" : "generating";
};
// In each message's parts:
if (part.type === "tool-generateImage") {
return (
<GeneratedImage
alt={part.input?.prompt}
base64={part.output?.base64}
key={part.toolCallId}
mediaType={part.output?.mediaType}
state={stateOf(part.state)}
>
{part.state === "output-error" ? part.errorText : null}
</GeneratedImage>
);
}The tool part's state maps to the frame's:
| Tool part state | state |
|---|---|
input-streaming, input-available | generating |
output-available | done |
output-error | error, with errorText inside it as the message |
Migrating from AI SDK Elements
AI SDK Elements' Image is GeneratedImage here, since a bare Image export would shadow the browser's Image constructor. Install this one, then change the import and keep the code that uses it:
import { GeneratedImage as Image } from "@/components/ui/generated-image";It takes the same props, so <Image {...image} alt="…" /> keeps working. What changes:
classNamelands on the frame, adiv, and theimgfills it, cropped to the frame's shape. The frame is square until you passaspectRatioor anaspect-*class, where the AI SDK Elementsimgkept the picture's own shape.uint8Arrayis used when there's nobase64. AI SDK Elements ignored it.- An image with no data yet shows the generating placeholder, where AI SDK Elements drew a broken
img, so a loader you showed in its place can go.
FAQ
What image data does Generated Image take?
The shape the AI SDK's generateImage returns: base64, or the bytes as uint8Array, with a mediaType such as image/png, which is also the default. The frame turns them into a data URL, so any format the browser decodes works, PNG, JPEG, WebP and GIF among them.
When both are there, base64 wins. For an image you already have at a URL, a plain img does the job without the frame.
How do I show that an image is still generating in React?
Set state to generating, or leave it out and pass no data yet. The frame shows a placeholder with a band of light sweeping across it and shimmering words, and sets aria-busy. Once the data arrives with state left out, or once you set state to done, the image takes its place in the same frame.
Anything you put inside GeneratedImage replaces the words Generating image, for a page in another language or a message of your own.
Why does my Generated Image keep saying Generating image?
Because no image data has reached it, and without state the frame waits for base64 or uint8Array. The usual cause is returning the AI SDK's image from a route as is. Its base64 is a getter on a class, so the JSON your client receives has no base64 field.
Send base64: image.base64 and mediaType: image.mediaType as plain fields, as the AI SDK section's route does. Also set state to error when the request fails, or a failure looks the same as a wait.
How do I show that image generation failed?
Set state to error. The frame turns red, shows a broken-image icon and says the image couldn't be generated. Put the tool's errorText, or your own sentence, inside GeneratedImage to replace those words, as the States demo does.
An image whose data won't decode shows the same error frame on its own, and so does state set to done with no data.
How do I keep the layout from jumping when the image loads?
Pass aspectRatio with the ratio you asked the model for, such as 16:9, so the placeholder already has the image's shape. It's 1:1 by default. A request for 1536 by 1024 pixels, like the route in the AI SDK section, is 3:2.
The frame fills the width it's given, so set that width with a class on the frame or its parent, and the height follows from the ratio.
Why is my generated image cropped?
The image covers its frame, and the frame's shape comes from aspectRatio, square unless you say otherwise. A wide picture in a square frame loses its sides, and it's never stretched to fit.
Pass the ratio of the size you requested and the whole picture shows. To fit it inside a frame of another shape instead, change the image's object-cover class to object-contain in components/ui/generated-image, which is your code once installed.
Does Generated Image respect reduced motion?
It does. With reduced motion on, the sweep is hidden, the words stop shimmering and the finished image appears without coming into focus.
Otherwise the focus effect plays once, and only for an image the reader saw generating, so one that was already there when the page loaded just appears. The keyframes come with the install, and no animation library is involved.
What should the alt text of a generated image say?
What the image shows. The prompt is a good start, and the tool call's input has it, as the AI SDK example does with part.input?.prompt. Rewrite an instruction such as "draw me a cat on a skateboard" into a description, a cat riding a skateboard.
Without alt, the image gets an empty one and screen readers skip it as decoration, which suits a picture that only repeats the text beside it.
API Reference
GeneratedImage
data-slot="generated-image"
alt?string
What the image shows, for screen readers: the prompt is a good start.
- Default
""
aspectRatio?`${number}:${number}`
Its width to its height, as generateImage takes it (16:9). The
frame keeps it while it generates, so nothing moves when the image
lands.
- Default
"1:1"
base64?string
The image, base64-encoded.
children?React.ReactNode
The placeholder's words, in place of "Generating image" or, in the error state, "The image couldn't be generated".
mediaType?string
The image's type, such as image/png.
- Default
"image/png"
providerMetadata?unknown
The provider's metadata the AI SDK adds to the file. Unused: taken so that spreading the SDK's object leaves it off the frame.
state?GeneratedImageState
generating draws a shimmering placeholder, error says it failed,
done shows the image. Left out, it's generating until there's an
image.
uint8Array?Uint8Array
The image's bytes: used when there's no base64.
className?string
Merged with the part's classes.
data-state?string
Mirrors state.














