Skip to main content
The Vercel AI SDK provides 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

npm install ai @ai-sdk/openai zod

Configure

Create a provider pointed at dottxt:
import { createOpenAI } from "@ai-sdk/openai";

const dottxt = createOpenAI({
  baseURL: "https://api.dottxt.ai/v1",
  apiKey: process.env.DOTTXT_API_KEY!,
});

Basic usage with Zod

Pass a Zod schema to generateObject for typed structured output:
import { generateObject } from "ai";
import { z } from "zod";

const { object } = await generateObject({
  model: dottxt.chat("openai/gpt-oss-20b"),
  schema: z.object({
    name: z.string().min(1).describe("Full name"),
    email: z.string().describe("Email address"),
    role: z.string().describe("Job title").optional(),
  }),
  prompt: "Extract: John Smith <john@acme.com>, VP Engineering",
});

console.log(object.name);  // "John Smith"
console.log(object.email); // "john@acme.com"
The AI SDK builds the structured output request and validates the result against your schema. Under the hood, this still uses the same dottxt structured generation flow described in API Overview.

Using raw JSON Schema

Use jsonSchema() when you have a JSON Schema object instead of a Zod schema:
import { generateObject, jsonSchema } from "ai";

const contactSchema = jsonSchema<{
  name: string;
  email: string;
  role: string;
}>({
  type: "object",
  properties: {
    name: { type: "string" },
    email: { type: "string" },
    role: { type: "string" },
  },
  required: ["name", "email", "role"],
  additionalProperties: false,
});

const { object } = await generateObject({
  model: dottxt.chat("openai/gpt-oss-20b"),
  schema: contactSchema,
  schemaName: "contact",
  prompt: "Extract: John Smith <john@acme.com>, VP Engineering",
});
This is useful when integrating with TypeBox or other schema libraries that produce raw JSON Schema objects.

Streaming

Use streamObject to receive partial results as tokens stream in:
import { streamObject } from "ai";
import { z } from "zod";

const { partialObjectStream } = streamObject({
  model: dottxt.chat("openai/gpt-oss-20b"),
  schema: z.object({
    title: z.string(),
    summary: z.string(),
    tags: z.array(z.string()).max(5),
  }),
  prompt: "Summarize: structured output improves LLM reliability...",
});

for await (const partial of partialObjectStream) {
  console.log(partial);
}

Notes

  • generateObject returns a fully validated object. streamObject yields partial objects as they stream.
  • Use dottxt.chat(...) instead of dottxt(...) with dottxt’s current OpenAI-compatible API support. The default model helper uses the Responses API path.
  • The schemaName parameter is optional but recommended when using jsonSchema(); it sets the name field 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.