Skip to main content

Structured Output You Can Trust: Severity, Test First

Why This Matters

A pipeline cannot act on prose. The moment TestPilot’s answer drives an action (a bug report, a quarantine, a message to a developer), it needs a contract. And like every contract a tester cares about, it needs a test.

Why prose is not enough

This is what one triage looks like without an output format: a well-reasoned report with evidence, other categories, a caveat and next steps. Useful for a human, impossible for a pipeline to act on, and over a thousand output tokens for a single failure.

A long free-text triage report in the Console Playground, with numbered evidence and explanations

One schema, two jobs

src/schema.ts defines the verdict once:

export const triageResultSchema = z.object({
  testId: z.string(),
  category: z.enum(["product_bug", "test_bug", "flaky", "environment", "unknown"]),
  confidence: z.number().min(0).max(1),
  summary: z.string(),
  evidence: z.array(z.string()),
  suggestedAction: z.string(),
});

That schema does two jobs:

  1. It is sent to Claude as the output format (output_config.format), so the answer comes back as JSON in exactly this shape.
  2. It validates the answer in your code before anything acts on it.

Change it in one place and both jobs change together.

The other route: a forced tool call

Native structured output (output_config.format) is the newer way. The classic way, and the one many exam questions still describe, is tool_use as the structured-output mechanism: define a tool whose input_schema is your verdict schema and force it with tool_choice: { type: "tool", name: "submit_verdict" } (lesson 25). The model’s answer is then the tool’s input: JSON in your shape.

Native structured outputForced tool call
Where the JSON isThe response content (parsed_output in the SDK)input of the tool_use block
FitsOne-shot answers like TestPilot’s triageAgent loops that already use tools
Still validate in code?YesYes

Both give you schema-shaped output. Neither replaces your own validation, and when validation fails the right move is the same: retry once with the validation error as feedback (lesson 10), then give up and report it.

Check your model first. Forced tool use is not available everywhere. On Claude Opus 5.5, Sonnet 5.5, Fable 5.x and Mythos 5.x, tool_choice any and tool return a 400 error, and with manual extended thinking they fail on every model. Where forcing is not available, use tool_choice: auto with a strict tool and a prompt that says to call it, or native structured output. Where it is available, a forced call skips thinking. TestPilot uses native structured output for the verdict and auto with a submit_verdict tool in its agent loop, so it works on all of them.

Why validate twice?

triageFailure validates again, although structured output already guarantees the shape:

const parsed = triageResultSchema.safeParse(message.parsed_output);
if (!parsed.success) {
  throw new Error(`Triage for ${result.id} did not match the schema: ...`);
}

Structured output guarantees that the answer matches the schema’s shape, with documented exceptions: an answer cut off by max_tokens or a refusal. The SDK’s zod helper even checks the full schema for you and throws on a mismatch. So why validate again? Because the check in your code keeps working when someone swaps the SDK, calls the API another way, or replaces the client with a fake in a test, and it is the one place where your pipeline decides to trust a result. A check that costs microseconds is cheap insurance.

Add severity, test first

The team wants a severity on every verdict: blocker, major or minor.

1. Write the failing test. In tests/triage.test.ts:

it("rejects a verdict without severity", async () => {
  const { client } = fakeClient(validTriage); // validTriage has no severity
  await expect(triageFailure(client, run, failure)).rejects.toThrow(/did not match the schema/);
});

Run npm test. It fails, for the right reason: the current schema accepts a verdict without severity.

2. Change the schema.

severity: z.enum(["blocker", "major", "minor"]).describe("Impact on the release"),

3. Update the valid fixture in the test with severity: "major" and run npm test until it is green.

4. Run the real triage with npm run triage. Every verdict now has a severity.

Did you need to change the prompt?

Probably not. The schema, including its describe() text, travels with the request, so Claude knows the field exists and what it means. Write down whether the severities make sense. If they do not, the fix is usually a sharper description of what “blocker” means for your team, not a longer prompt.

Exercise

docs/exercises.md, lesson 9. Compare with solution/lesson-09-structured-output. Then compare the output tokens of one verdict with the free-text answer above.

Show suggested answers

Was the schema enough? Usually yes: with structured output, the schema (including each field’s .describe() text) is part of what the model sees, so every verdict gets a valid severity. But valid is not the same as consistent. Without definitions, one run calls a broken checkout major and the next blocker. Adding one line per level to the system prompt (“blocker: release cannot ship”, and so on) makes the values comparable across runs. The schema guarantees the shape; the prompt steers the meaning.

Output tokens: the JSON verdict is typically much shorter than a free-text answer, because there is no introduction, no hedging paragraph and no closing summary. You pay for every output token, so the structured version is also the cheaper one.