run, stream, or streamEvents when your application needs validated data instead of prose. The method returns the schema’s inferred TypeScript type or throws if conversion fails.
Return a typed object
Define the schema close to the run that needs it. Field descriptions help the LLM gather the right information before the final conversion step.result.totalFiles is typed as number, and result.fileTypes is typed as string[].
Use a complete agent example
This example asks an agent to inspect files and return a typed summary.How validation works
run() and stream() convert the final text result to the schema after agent execution completes. The SDK validates with Zod and retries formatting up to three times.
streamEvents() adds schema information to the prompt before execution and emits structured-output events after the stream finishes.
Stream structured output events
UsestreamEvents() when a UI needs progress events during structured-output conversion.