Structured outputs and tool calling
Structured outputs force an AI model to return data in a strict, machine-readable shape (such as JSON matching a schema) instead of free-form prose, while tool calling lets the model emit a structured request for your software to run a specific function. Together, they are the bridge that lets AI plug into real databases, CRMs, and workflows without breaking parsers.
The one friendly sentence that breaks an automation
You ask an AI to pull a customer name, service type, and urgency from an inbound email so your workflow can open a CRM draft. Nine times in a row it returns clean fields. On the tenth email, it adds "Sure! Here is the extracted summary:" before the JSON, or invents a new urgency label like "fairly urgent," and your automation crashes.
Structured output and function calling fix that fragility. Instead of begging the model in prose to "please only return valid JSON," you hand the API a formal schema that locks in exact field names, data types, and allowed values. Your code receives predictable data it can validate and pass straight to another system.
A strict schema and the tool call it produces
Notice how enum values and required fields eliminate guesswork before your CRM code ever sees the payload:
Example JSON Schema and structured tool call
// 1. Output schema (enforced before your code runs)
{
"name": "lead_intake_v1",
"strict": true,
"schema": {
"type": "object",
"properties": {
"customer_name": { "type": "string" },
"service_type": {
"type": "string",
"enum": ["roof_repair", "gutter_install", "inspection", "unknown"]
},
"urgency": { "type": "string", "enum": ["routine", "urgent"] },
"needs_human_review": { "type": "boolean" }
},
"required": ["customer_name", "service_type", "urgency", "needs_human_review"],
"additionalProperties": false
}
}
// 2. Resulting tool call handed to your CRM handler
{
"tool": "create_crm_draft",
"arguments": {
"customer_name": "Jordan Lee",
"service_type": "inspection",
"urgency": "urgent",
"needs_human_review": false
}
}When to enforce structured outputs vs. let the model write prose
Enforce structured outputs when
- Another piece of software (a CRM, spreadsheet, database, or webhook) will read the AI output directly.
- You need consistent classification labels, extracted dates, or boolean flags across hundreds of runs.
- An agent must invoke a tool with typed arguments that deterministic code validates before execution.
Keep free-form prose when
- A human is reading the response directly (such as drafting an email, memo, or plain explanation).
- You are brainstorming early ideas where rigid fields would choke useful nuance.
- The output shape is still changing every day while you explore the problem.
For builders wiring LLM outputs into production APIs and automated workflows.
Constrained decoding guarantees shape, not truth: Strict structured-output modes use constrained decoding at token-generation time to mask out any token that would violate your JSON Schema. That eliminates missing braces, extra keys, and conversational filler. What it does not guarantee is semantic accuracy: a model can still put a hallucinated date inside a perfectly valid date field, so you still validate business rules in code.
Deterministic validation and bounded repair loops: Treat every model payload like untrusted user input. Validate it with a real schema library (such as Pydantic or Zod) plus domain checks (does that job ID actually exist? Is the date in the future?). If a domain check fails, feed the exact validation error back to the model for one or two repair retries, then route to a human queue instead of looping forever.
Preventing schema drift: When you rename a field or add a new enum category in your database, update the tool schema, your validation layer, and your eval fixtures in the same change. Versioning schemas (like lead_intake_v1) and running contract tests keeps silent field mismatches from breaking downstream automations.
Try this in 2 minutes
Tighten one extraction or classification step so downstream software never trips on prose:
-
List the exact fields your next step needs
Keep the object flat and minimal: 3 to 6 required fields beat a deeply nested structure.
-
Lock categories into enums
Replace open text labels with an explicit allowed list plus an 'unknown' fallback.
-
Turn on strict schema mode
Use your model API's native structured-output or tool-calling schema instead of prompt-only JSON instructions.
-
Validate business rules in code
Verify IDs, ranges, and permissions in deterministic code before writing anything to a live database.
Need AI plugged cleanly into your CRM, forms, or internal tools?
We wire AI steps into business software using strict schemas, deterministic validation, and clean fallback paths so an unexpected email never crashes your workflow.
Connects directly with What is workflow automation? (bounded AI steps inside fixed flows), What is MCP? (standardizing tool schemas), and What is an AI eval? (deterministic schema checks). Or browse the AI glossary.
Common questions about structured outputs and tool calling
What is a structured output in AI?
A structured output is a model response constrained to follow a fixed data format, such as JSON matching a schema, so other software can parse and use it reliably without breaking on extra prose.
How does tool calling (function calling) work?
You describe available functions and their input schemas to the model. When the model needs an action or lookup, it outputs a structured function name and arguments, your code runs the real function, and you hand the result back to the model.
Does the AI actually execute my code or database query itself?
No. The model only outputs a structured request asking for a tool call. Your application or harness inspects the request, checks permissions, runs the real code, and returns the result.
Does strict JSON mode stop an AI from hallucinating?
No. Strict schema mode guarantees valid formatting and allowed field names, not factual truth. You still need grounding, business-rule validation in code, and human review where mistakes are costly.
Why should every classification schema include an unknown option?
Because strict schemas force the model to pick one of the listed values. Without an explicit 'unknown' or 'needs_review' escape hatch, an ambiguous input forces a confident wrong guess.
How do structured outputs relate to MCP?
Function calling and JSON schemas are the underlying mechanics that let models request actions cleanly. MCP is a shared protocol that packages those tool schemas and calls so any compatible AI client can reuse them.