The AI SDK supports different output strategies to define the shape of the generated data:
object (Default): Returns the data as a single object. No explicit setting required.array: Generates an array of objects. The schema should define the shape of a single element in that array. When using streamObject, you can use elementStream to iterate over individual elements as they are generated.enum (Available with generateObject only): Used for classification tasks. Provide a list of allowed values in the enum parameter.no-schema: Used when you want structured output but don't want to enforce a specific schema (e.g., for dynamic user requests).
// Array strategy example
const { elementStream } = streamObject({
model: openai("gpt-4.1"),
output: "array",
schema: z.object({
name: z.string(),
class: z.string().describe("Character class, e.g. warrior, mage, or thief."),
description: z.string(),
}),
prompt: "Generate 3 hero descriptions for a fantasy role playing game.",
});
for await (const hero of elementStream) {
console.log(hero);
}
// Enum strategy example
const { object } = await generateObject({
model: "openai/gpt-4.1",
output: "enum",
enum: ["action", "comedy", "drama", "horror", "sci-fi"],
prompt: "Classify the genre of this movie plot...",
});
// No-schema example
const { object } = await generateObject({
model: openai("gpt-4.1"),
output: "no-schema",
prompt: "Generate a lasagna recipe.",
});