generateObject and streamObject for structured output in TypeScript. Since dottxt is OpenAI-compatible, use @ai-sdk/openai with a custom baseURL.
For dottxt, generateObject and streamObject are the key integration points: you provide a Zod or JSON Schema schema, the SDK sends a structured output request to dottxt, and the response is parsed back into a typed object.
Use dottxt.chat(...) for these examples so the SDK targets the OpenAI-compatible chat completions API rather than the Responses API.
Install
Configure
Create a provider pointed at dottxt:Basic usage with Zod
Pass a Zod schema togenerateObject for typed structured output:
Using raw JSON Schema
UsejsonSchema() when you have a JSON Schema object instead of a Zod schema:
Streaming
UsestreamObject to receive partial results as tokens stream in:
Notes
generateObjectreturns a fully validated object.streamObjectyields partial objects as they stream.- Use
dottxt.chat(...)instead ofdottxt(...)with dottxt’s current OpenAI-compatible API support. The default model helper uses the Responses API path. - The
schemaNameparameter is optional but recommended when usingjsonSchema(); it sets thenamefield in the request for better model guidance. - Use schema constraints like
.min(),.max(), enums, and.describe()to improve output quality and make the generated schema more specific. - See the Zod authoring guide for how to write effective schemas.