Interface StreamObjectOptions<T, K>

Options for a streamObject call.

Shares the same shape as GenerateObjectOptions from generateObject. At minimum, schema and either prompt or messages must be supplied.

Example

const opts: StreamObjectOptions<typeof mySchema> = {
schema: z.object({ name: z.string(), items: z.array(z.string()) }),
prompt: 'List 3 fruits with a person name',
};
interface StreamObjectOptions<T, K> {
    provider?: string;
    model?: string;
    schema: T;
    schemaName?: string;
    schemaDescription?: string;
    elements?: K;
    prompt?: string;
    system?: string;
    messages?: Message[];
    temperature?: number;
    maxTokens?: number;
    effort?: string;
    thinking?: false | {
        budgetTokens: number;
    };
    maxRetries?: number;
    apiKey?: string;
    baseUrl?: string;
    abortSignal?: AbortSignal;
    requestTimeout?: number;
    maxAttempts?: number;
    onAttempt?: ((outcome) => void);
    serviceTier?: "priority" | "auto" | "default" | "flex";
    customModelParams?: Record<string, unknown>;
}

Type Parameters

  • T extends ZodType

    The Zod schema type defining the expected output shape.

  • K extends string = string

    The array field named by StreamObjectOptions.elements; a call infers it from the name it passes.

Properties

provider?: string

Provider name. When supplied without model, the default text model for the provider is resolved automatically.

Example

`"openai"`, `"anthropic"`, `"ollama"`
model?: string

Model identifier. Prefer the plain model name with provider set; the combined "provider:model" string is also accepted.

Example

`"gpt-4o"` (with `provider: 'openai'`), `"gpt-4o-mini"`
schema: T

Zod schema defining the expected output shape.

schemaName?: string

Human-readable name for the schema, injected into the system prompt.

Example

`"ShoppingList"`
schemaDescription?: string

Description of the schema, injected into the system prompt.

Example

`"A shopping list with a person's name and items."`
elements?: K

The name of one top-level array field of schema, whose elements StreamObjectResult.elementStream yields one at a time as each is completed: an element once the next one begins, and the last one once the array's closing bracket arrives, while any field the schema puts after the array is still streaming, or at the latest when the stream ends with text that parses. Each is parsed by the array's element schema; an element that fails it is skipped and counted in StreamObjectResult.skipped. A name that is not a field of the schema holding an array (optional, nullable or with a default included; one behind a transform, a pipe or z.lazy() is not) throws a TypeError at the call, before any request is sent.

Example

`"sentences"` for `z.object({ sentences: z.array(Sentence) })`
prompt?: string

User prompt.

system?: string

System prompt. Schema instructions are appended automatically.

messages?: Message[]

Full conversation history.

temperature?: number

Sampling temperature forwarded to the provider.

maxTokens?: number

Hard cap on output tokens.

effort?: string

Reasoning depth forwarded to GenerateTextOptions.effort.

thinking?: false | {
    budgetTokens: number;
}

Extended-thinking switch forwarded to GenerateTextOptions.thinking; see GenerateObjectOptions.thinking.

Type declaration

  • budgetTokens: number
maxRetries?: number

Number of retries on validation failure. Unlike generateObject, streaming retries are not currently supported (the stream is consumed once). This field is accepted for API symmetry but is unused; validation errors on the final object throw immediately.

Default

0
apiKey?: string

Override the API key.

baseUrl?: string

Override the provider base URL.

abortSignal?: AbortSignal

Ends the stream when it aborts, forwarded to GenerateTextOptions.abortSignal: the provider's stream stops, the partial-object and element iterations end, and object rejects with the signal's reason.

requestTimeout?: number

Per-call request timeout in milliseconds, forwarded to GenerateTextOptions.requestTimeout.

maxAttempts?: number

The most requests the stream's call sends, a first and its retries together; the provider's configured count when left out. Forwarded to GenerateTextOptions.maxAttempts.

onAttempt?: ((outcome) => void)

Called after each request the stream's call sent, so a caller can settle the attempts it paid for.

Type declaration

serviceTier?: "priority" | "auto" | "default" | "flex"

OpenAI service tier, forwarded to GenerateTextOptions.serviceTier and sent as service_tier verbatim on the streamed request. No default: OpenAI then serves the request at the project's configured tier. Other providers ignore it.

customModelParams?: Record<string, unknown>

Provider-specific top-level request fields, forwarded to GenerateTextOptions.customModelParams and spread onto the streamed request body with the same rules as there: a fallback leg gets them minus the fields only another vendor accepts.