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 { openai } from '@ai-sdk/openai';import { streamObject } from 'ai';import { z } from 'zod';
const { partialObjectStream } = await streamObject({ model: openai('gpt-4-turbo'), schema: z.object({ recipe: z.object({ name: z.string(), ingredients: z.array(z.string()), steps: z.array(z.string()), }), }), prompt: 'Generate a lasagna recipe.',});
for await (const partialObject of partialObjectStream) { console.clear(); console.log(partialObject);}
Import
import { streamObject } from "ai"
API Signature
Parameters
model:
The language model to use. Example: openai('gpt-4-turbo')
mode:
The mode to use for object generation. Not every model supports all modes. Defaults to 'auto'.
schema:
The schema that describes the shape of the object to generate. It is sent to the model to generate the object and used to validate the output. You can either pass in a Zod schema or a JSON schema (using the `jsonSchema` function).
system:
The system prompt to use that specifies the behavior of the model.
prompt:
The input prompt to generate the text from.
messages:
A list of messages that represent a conversation.
CoreSystemMessage
role:
The role for the system message.
content:
The content of the message.
CoreUserMessage
role:
The role for the user message.
content:
The content of the message.
TextPart
type:
The type of the message part.
text:
The text content of the message part.
ImagePart
type:
The type of the message part.
image:
The image content of the message part. String are either base64 encoded content, base64 data URLs, or http(s) URLs.
CoreAssistantMessage
role:
The role for the assistant message.
content:
The content of the message.
TextPart
type:
The type of the message part.
text:
The text content of the message part.
ToolCallPart
type:
The type of the message part.
toolCallId:
The id of the tool call.
toolName:
The name of the tool, which typically would be the name of the function.
args:
Parameters generated by the model to be used by the tool.
CoreToolMessage
role:
The role for the assistant message.
content:
The content of the message.
ToolResultPart
type:
The type of the message part.
toolCallId:
The id of the tool call the result corresponds to.
toolName:
The name of the tool the result corresponds to.
result:
The result returned by the tool after execution.
isError?:
Whether the result is an error or an error message.
maxTokens?:
Maximum number of tokens to generate.
temperature?:
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?:
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.
topK?:
Only sample from the top K options for each subsequent token. Used to remove "long tail" low probability responses. Recommended for advanced use cases only. You usually only need to use temperature.
presencePenalty?:
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?:
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?:
The seed (integer) to use for random sampling. If set and supported by the model, calls will generate deterministic results.
maxRetries?:
Maximum number of retries. Set to 0 to disable retries. Default: 2.
abortSignal?:
An optional abort signal that can be used to cancel the call.
headers?:
Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
experimental_telemetry?:
Telemetry configuration. Experimental feature.
TelemetrySettings
isEnabled?:
Enable or disable telemetry. Disabled by default while experimental.
functionId?:
Identifier for this function. Used to group telemetry data by function.
metadata?:
Additional information to include in the telemetry data.
onFinish?:
Callback that is called when the LLM response and all request tool executions (for tools that have an `execute` function) are finished.
OnFinishResult
usage:
The token usage of the generated text.
CompletionTokenUsage
promptTokens:
The total number of tokens in the prompt.
completionTokens:
The total number of tokens in the completion.
totalTokens:
The total number of tokens generated.
object:
The generated object (typed according to the schema). Can be undefined if the final object does not match the schema.
error:
Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
warnings:
Warnings from the model provider (e.g. unsupported settings).
rawResponse:
Optional raw response data.
RawResponse
headers:
Response headers.
Returns
usage:
The token usage of the generated text. Resolved when the response is finished.
CompletionTokenUsage
promptTokens:
The total number of tokens in the prompt.
completionTokens:
The total number of tokens in the completion.
totalTokens:
The total number of tokens generated.
object:
The generated object (typed according to the schema). Resolved when the response is finished.
partialObjectStream:
Stream of partial objects. It gets more complete as the stream progresses. 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.
textStream:
Text stream of the JSON representation of the generated object. It contains text chunks. When the stream is finished, the object is valid JSON that can be parsed.
fullStream:
Stream of different types of events, including partial objects, errors, and finish events. Only errors that stop the stream, such as network errors, are thrown.
ObjectPart
type:
object:
The partial object that was generated.
TextDeltaPart
type:
textDelta:
The text delta for the underlying raw JSON text.
ErrorPart
type:
error:
The error that occurred.
FinishPart
type:
finishReason:
logprobs:
usage:
Token usage.
rawResponse:
Optional raw response data.
RawResponse
headers:
Response headers.
warnings:
Warnings from the model provider (e.g. unsupported settings).
pipeTextStreamToResponse:
Writes text delta output to a Node.js response-like object. It sets a `Content-Type` header to `text/plain; charset=utf-8` and writes each text delta as a separate chunk.
toTextStreamResponse:
Creates a simple text stream response. Each text delta is encoded as UTF-8 and sent as a separate chunk. Non-text-delta events are ignored.