When triggering runs in Inferable, you can specify a schema to ensure the output is structured in a way that’s easy to process programmatically.

Defining Output Schema

You can define the expected structure of your run’s output using Zod schemas:

import { z } from "zod";

const run = await client.run({
  initialPrompt: "List all products with low inventory",
  resultSchema: z.object({
    products: z.array(
      z.object({
        name: z.string(),
        quantity: z.number(),
        needsRestock: z.boolean(),
      }),
    ),
  }),
});

Output Format

When a run completes successfully, the result will be structured according to your schema:

{
  "id": "01J8GNNHRRZA9E605BEM57A3HT",
  "status": "done",
  "result": {
    "products": [
      {
        "name": "Widget X",
        "quantity": 5,
        "needsRestock": true
      },
      {
        "name": "Gadget Y",
        "quantity": 12,
        "needsRestock": false
      }
    ]
  }
}

If a run cannot produce a structured result matching your schema, the result field will be null and a message explaining why will be provided.

Using with Agents

When working with Agents, you can define a default resultSchema that will be used for all runs created with that agent. This is particularly useful when you want to standardize output across multiple runs:

const run = await client.run({
  agentId: "AGENT_ID",
  initialPrompt: "Analyze customer sentiment",
  // This will override any resultSchema defined in the agent
  resultSchema: z.object({
    sentiment: z.enum(["positive", "neutral", "negative"]),
    confidence: z.number(),
    keywords: z.array(z.string()),
  }),
});

Integration Examples

Zapier

When using Inferable with Zapier, structured output allows you to use the results in subsequent steps of your Zap. The output will be automatically converted to a format Zapier can understand.

API Endpoints

When building API endpoints that use Inferable, structured output ensures your API responses are consistent and typed:

app.post("/analyze", async (req, res) => {
  const run = await client.run({
    initialPrompt: req.body.text,
    resultSchema: z.object({
      categories: z.array(z.string()),
      summary: z.string(),
      confidence: z.number(),
    }),
  });

  const result = await run.poll();
  res.json(result);
});