Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Function

streamObject

streamObject streams typed, structured data from a language model using a prompt and schema. It supports object, array, enum, and no-schema output strategies.

How it works

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.

Syntax

streamObject({ model, output?, schema?, prompt?, ...options })

Parameters

NameTypeDescription
modelLanguageModelThe language model to use.
output'object' | 'array' | 'enum' | 'no-schema' | undefinedOutput strategy: object, array, enum, no-schema, or undefined; defaults to object.
schemaZod Schema | JSON SchemaZod Schema or JSON Schema describing the generated object or array element; unavailable for no-schema and enum.
promptstring | CoreMessage[]Prompt or other generation input supplied to the model.

Returns

A stream result containing text chunks and structured-data streams, including partialObjectStream; elementStream yields complete array elements.

Key points

  • It incrementally produces typed structured data from a prompt and schema.
  • Use it for constrained outputs such as extraction, synthetic-data generation, or classification.
  • The output option selects the generation shape and defaults to object.
  • A Zod or JSON Schema defines the expected structure and is used for validation.

Common mistakes

  • A schema cannot be supplied when using no-schema or enum output.
  • In array mode, define the schema for one array element rather than the complete array.
  • Use elementStream when consumers need complete array items instead of partial aggregate updates.

Example: Stream a typed object

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.

When to use it

Information extraction

Extract fields from unstructured text while receiving partial results as generation proceeds.

Classification

Produce validated labels or scores for downstream application logic.

Streaming arrays

Generate lists incrementally and process complete items as they arrive.

Generative UI

Render progressively generated structured UI data with React Server Components or useObject.

Interview questions

QWhat problem does streamObject solve?

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.

QWhich output strategies are available?

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'.

QHow do array streams differ from object streams?

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.

  • generateObject
  • streamText
  • useObject
  • elementStream
  • jsonSchema

Source: AI SDK Core: streamObject. Checked 2026-10-15.