Skip to main content
When you need data rather than prose, pass an outputSchema to a run. The agent loop runs to completion, coerces the final answer to your schema, and returns a validated object — typed against the schema you defined. The schema is an ArkType assertion built with Assert from @ooneex/validation.

Asking for an object

Define the shape, pass it as outputSchema, and type the call with the schema’s inferred type:
import { Assert } from "@ooneex/validation";

const ProductSchema = Assert({
  name: "string",
  price: "number",
  description: "string",
});

const product = await chat.run<typeof ProductSchema.infer>({
  prompt: "Generate a product for an e-commerce store.",
  outputSchema: ProductSchema,
});

console.log(product.name, product.price); // typed and validated
Without a schema, run returns the assistant’s text as a string. With one, it returns the validated object — the result is coerced and checked before it reaches you, so a malformed answer fails loudly rather than slipping through.

Richer schemas

Anything ArkType expresses works — optional fields, unions, arrays, and constraints:
const ReviewSchema = Assert({
  sentiment: "'positive' | 'neutral' | 'negative'",
  score: "0 <= number <= 10",
  tags: "string[]",
  "summary?": "string",
});

const review = await chat.run<typeof ReviewSchema.infer>({
  prompt: "Analyze this customer review: …",
  outputSchema: ReviewSchema,
});

With tools

Structured output and tools combine: the agent can call tools across the loop to gather what it needs, then shape the final answer into your schema. The object you get back reflects everything the agent learned during the run.
const AnswerSchema = Assert({
  answer: "string",
  sources: "string[]",
});

const result = await chat.run<typeof AnswerSchema.infer>({
  prompt: "What were this company's last three funding rounds?",
  tools: [ExaSearchTool],
  outputSchema: AnswerSchema,
});
Middleware can transform the structured-output call before it is sent — including the JSON Schema handed to the provider — through the onStructuredOutputConfig hook. See Middleware.