Function streamObject

  • Streams a structured object by incrementally parsing JSON as the LLM produces tokens, then validates the final result against a Zod schema.

    Returns immediately with a StreamObjectResult containing async iterables and promises. The underlying LLM call begins lazily when a consumer starts iterating partialObjectStream or awaits a promise.

    Type Parameters

    • T extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>

      The Zod schema type. Partial objects are typed as DeepPartial<z.infer<T>>, and the final object promise resolves to z.infer<T>.

    Parameters

    • opts: StreamObjectOptions<T>

      Streaming generation options including the Zod schema, prompt/messages, and optional provider/model overrides.

    Returns StreamObjectResult<z.infer<T>>

    A StreamObjectResult with partialObjectStream, object, text, and usage properties.

    Example

    import { z } from 'zod';
    import { streamObject } from '@framers/agentos';

    const result = streamObject({
    model: 'openai:gpt-4o',
    schema: z.object({ name: z.string(), hobbies: z.array(z.string()) }),
    prompt: 'Create a profile for a fictional character.',
    });

    for await (const partial of result.partialObjectStream) {
    console.log('partial:', partial);
    }

    const final = await result.object;
    console.log('final:', final);

    See