AI SDK CorestreamObject

streamObject

Streams a typed, structured object for a given prompt and zod schema using a language model.

It can be used to force the language model to return structured data, e.g. for information extraction, synthetic data generation, or classification tasks.

Import

import { streamObject } from "ai"

Parameters

model:

LanguageModel
The language model to use. Example: openai('gpt-4-turbo')

schema:

zod schema
The schema of the object to be generated.

mode:

'auto' | 'json' | 'grammar' | 'tool'
The mode to use for object generation. Not all models support all modes. Defaults to 'auto'.

system:

string
The system prompt to use that specifies the behavior of the model.

prompt:

string
The input prompt to generate the text from.

messages:

Array<CoreUserMessage | CoreAssistantMessage | CoreToolMessage>
A list of messages that represent a conversation.
CoreUserMessage

role:

'user'
The role for the user message.

content:

string | Array<TextPart | ImagePart>
The content of the message.
TextPart

type:

'text'
The type of the message part.

text:

string
The text content of the message part.
ImagePart

type:

'image'
The type of the message part.

image:

ArrayBuffer | Uint8Array | Buffer | URL
The image content of the message part.
CoreAssistantMessage

role:

'assistant'
The role for the assistant message.

content:

string | Array<TextPart | ToolCallPart>
The content of the message.
TextPart

type:

'text'
The type of the message part.

text:

string
The text content of the message part.
ToolCallPart

type:

'tool-call'
The type of the message part.

toolCallId:

string
The id of the tool call.

toolName:

string
The name of the tool, which typically would be the name of the function.

args:

object based on zod schema
Parameters generated by the model to be used by the tool.
CoreToolMessage

role:

'tool'
The role for the assistant message.

content:

Array<ToolResultPart>
The content of the message.
ToolResultPart

type:

'tool-result'
The type of the message part.

toolCallId:

string
The id of the tool call the result corresponds to.

toolName:

string
The name of the tool the result corresponds to.

result:

any
The result returned by the tool after execution.

isError?:

boolean
Whether the result is an error or an error message.

maxTokens?:

number
Maximum number of tokens to generate.

temperature?:

number
Temperature setting. The value is passed through to the provider. The range depends on the provider and model. It is recommended to set either `temperature` or `topP`, but not both.

topP?:

number
Nucleus sampling. The value is passed through to the provider. The range depends on the provider and model. It is recommended to set either `temperature` or `topP`, but not both.

presencePenalty?:

number
Presence penalty setting. It affects the likelihood of the model to repeat information that is already in the prompt. The value is passed through to the provider. The range depends on the provider and model.

frequencyPenalty?:

number
Frequency penalty setting. It affects the likelihood of the model to repeatedly use the same words or phrases. The value is passed through to the provider. The range depends on the provider and model.

seed?:

number
The seed (integer) to use for random sampling. If set and supported by the model, calls will generate deterministic results.

maxRetries?:

number
Maximum number of retries. Set to 0 to disable retries. Default: 2.

abortSignal?:

AbortSignal
An optional abort signal that can be used to cancel the call.

Result Object

partialObjectStream:

AsyncIterable<DeepPartial<T>> & ReadableStream<DeepPartial<T>>
Note that the partial object is not validated. If you want to be certain that the actual content matches your schema, you need to implement your own validation for partial results.

rawResponse:

RawResponse
Optional raw response data.
RawResponse

header:

Record<string, string>
Response headers.

warnings:

Warning[] | undefined
Warnings from the model provider (e.g. unsupported settings).

Examples