---
title: "Structured output and tools · Agent Wranglers"
url: https://agentwranglers.se/playbook/ai-features/structured-output-and-tools
updated: 2026-10-20
licence: "All rights reserved"
---

# Structured output and tools

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

> Ask for JSON that matches a schema, validate it, retry with the error, then fall back. Design tools like a small public API with strict inputs.

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers ai features playbook: https://agentwranglers.se/playbook/ai-features/structured-output-and-tools`.

## Required

- [Ask for output that matches a schema](https://agentwranglers.se/playbook/ai-features/structured-output-and-tools/schema-output): When code reads a model's answer, request output that matches a JSON Schema through the provider's structured output mode. Define that schema once in code.
- [Retry a bad answer and then fall back](https://agentwranglers.se/playbook/ai-features/structured-output-and-tools/retry-invalid-output): When an answer fails validation, retry at most twice with the validation error added, then use a fallback. Never pass an invalid answer on.
- [Give the model a way to say it does not know](https://agentwranglers.se/playbook/ai-features/structured-output-and-tools/allow-unknown): Give every schema field the model may not be able to fill an explicit "unknown" value or null. Never force it to invent a plausible answer.
- [Design tools like a small public API](https://agentwranglers.se/playbook/ai-features/structured-output-and-tools/tool-design): Give each tool one clear job, a verb-noun name, a description of when to use it and a strict input schema. Validate every argument in code before the tool runs.
- [Make tools that change things safe to repeat](https://agentwranglers.se/playbook/ai-features/structured-output-and-tools/idempotent-tools): Give every tool that creates or changes data an idempotency key, so a repeated call has no extra effect.

## Recommended

- [Return errors the model can act on](https://agentwranglers.se/playbook/ai-features/structured-output-and-tools/tool-errors-for-models): When a tool fails, return a short structured error that says what went wrong and what to try next. Never return a stack trace or an empty result.

- Previous: [Prompts as code](https://agentwranglers.se/playbook/ai-features/prompts-as-code)
- Next: [Retrieval](https://agentwranglers.se/playbook/ai-features/retrieval)
- [All chapters](https://agentwranglers.se/playbook/ai-features)
