camelAI Documentation

Guides

Structured output and JSON Schema validation in camelRun v2

Pass outputSchema to camelRun v2 agent runs and stateless runs to get schema-validated JSON in run.output.

View as Markdown

Pass a JSON Schema object in outputSchema on an agent run (POST /v2/agents/:idOrKey/runs), an initial PUT /v2/agents/:key prompt, or a stateless run (POST /v2/runs) to receive validated JSON in run.output.

Structured output on an agent run

ts
interface TicketTriage {
  priority: "low" | "medium" | "high";
  team: "billing" | "engineering" | "support";
  summary: string;
}

const outputSchema = {
  type: "object",
  properties: {
    priority: { type: "string", enum: ["low", "medium", "high"] },
    team: { type: "string", enum: ["billing", "engineering", "support"] },
    summary: { type: "string" },
  },
  required: ["priority", "team", "summary"],
  additionalProperties: false,
};

const run = await agent.run<TicketTriage>(
  "Customer says checkout returned 502 twice and they were charged twice.",
  { outputSchema },
);

console.log(run.output);
// { priority: "high", team: "billing", summary: "Duplicate charge after 502 during checkout." }

Over REST

bash
curl -s -X POST "https://run.camelai.com/v2/agents/triage-agent/runs" \
  -H "Authorization: Bearer $CAMELRUN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Customer cannot reset their password.",
    "outputSchema": {
      "type": "object",
      "properties": {
        "priority": { "type": "string", "enum": ["low", "medium", "high"] },
        "team": { "type": "string", "enum": ["billing", "engineering", "support"] },
        "summary": { "type": "string" }
      },
      "required": ["priority", "team", "summary"]
    }
  }' | jq .output

Validation and automatic repair

When outputSchema is set on an agent run:

  1. The agent can still call tools across multiple steps before producing its final answer.
  2. When the model returns its final message with no tool calls, camelRun parses the response as JSON and validates it against outputSchema.
  3. If parsing or validation fails and steps remain within maxSteps, camelRun automatically sends one repair turn telling the model the validation error so it can return valid JSON.
  4. If the repaired response still fails validation, the run ends with status: "failed" and error code output_schema_invalid (422).

On a stateless run (POST /v2/runs), invalid JSON fails immediately with 422 output_schema_invalid.

Classification with /v2/decisions

If you only need to classify an input into a fixed list of choices with calibrated probabilities, use POST /v2/decisions instead of writing a full schema:

ts
const decision = await client.decisions.decide({
  input: "I was billed twice for my October invoice.",
  choices: ["billing", "technical", "spam"] as const,
  instructions: "Route incoming support messages to the right queue.",
});

console.log(decision.choice);     // "billing"
console.log(decision.confidence); // 0.94
console.log(decision.scores);     // { billing: 0.94, technical: 0.05, spam: 0.01 }

See Stateless runs and decisions.