Information extraction
Extract fields from unstructured text while receiving partial results as generation proceeds.
AI Tools · Function
streamObject streams typed, structured data from a language model using a prompt and schema. It supports object, array, enum, and no-schema output strategies.
streamObject sends a prompt and schema to a language model, then exposes progressively generated structured data. Its default output strategy is an object; array mode describes each element with the schema, while enum and no-schema modes support other documented output shapes.
streamObject({ model, output?, schema?, prompt?, ...options })| Name | Type | Description |
|---|---|---|
model | LanguageModel | The language model to use. |
output | 'object' | 'array' | 'enum' | 'no-schema' | undefined | Output strategy: object, array, enum, no-schema, or undefined; defaults to object. |
schema | Zod Schema | JSON Schema | Zod Schema or JSON Schema describing the generated object or array element; unavailable for no-schema and enum. |
prompt | string | CoreMessage[] | Prompt or other generation input supplied to the model. |
A stream result containing text chunks and structured-data streams, including partialObjectStream; elementStream yields complete array elements.
import { streamObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
const result = streamObject({
model: openai('gpt-4.1'),
schema: z.object({
classification: z.enum(['positive', 'negative']),
confidence: z.number(),
}),
prompt: 'Classify the sentiment of: The product is excellent.',
});
for await (const partial of result.partialObjectStream) {
console.log(partial);
}Streams progressively validated partial structured objects from a language model.
Extract fields from unstructured text while receiving partial results as generation proceeds.
Produce validated labels or scores for downstream application logic.
Generate lists incrementally and process complete items as they arrive.
Render progressively generated structured UI data with React Server Components or useObject.
streamObject generates structured data incrementally from a prompt and schema. The schema both describes the requested shape and validates the generated result.
Follow-up: How would you stream a list? Use output: 'array', define the schema for one item, and consume elementStream for complete elements.
The default output is object. Other documented strategies include array, enum, and no-schema, with schema availability depending on the selected strategy.
Follow-up: What changes in no-schema mode? Omit schema and explicitly set output to 'no-schema'.
The function returns a result containing text chunks and streaming structured-data interfaces. For arrays, elementStream provides complete array elements as they become available.
Follow-up: How can a generated UI consume the stream? The documentation describes combining streamObject with React Server Components or the useObject hook.
generateObjectstreamTextuseObjectelementStreamjsonSchema