All posts

"Structured outputs and function calling: the reliable core of LLM integration"

If your code is fishing JSON out of a model's prose with regex, you're doing the integration the hard way. Structured outputs and function calling are the two mechanisms that make LLMs composable with software, here's how they actually work, and when native schema constraints beat a validate-and-re-ask loop.

AI engineering4 min read1 October 2026by Ahmed
"Structured outputs and function calling: the reliable core of LLM integration"

Somewhere in a codebase near you there is a function that takes a model's response, strips the "Here's the JSON you asked for:" preamble, trims the markdown fences, and hopes json.parse doesn't throw. If that's your integration, the problem isn't the model, it's that you're using a text interface for a job that has had a proper mechanism for a while now.

That mechanism is schema-constrained output, and it comes in two flavours that are really the same idea. Structured output means you hand the provider a JSON schema and the model's response must conform to it. Function calling means you describe tools as schemas and the model responds with a conforming request to call one. Under the hood both are the model emitting structured data against a contract you defined. Everything reliable I've built on LLMs sits on this core.

It's worth being precise about what function calling is, because the name misleads people. The model never calls anything. It returns a piece of data saying "I would like search_invoices called with these arguments", and your code decides whether to honour that, executes it, and feeds the result back. The model requests; your code decides. That gap is where all your safety lives, and it's a feature, not a limitation.

"Structured outputs and function calling: the reliable core of LLM integration"

Native structured output modes, where the provider enforces the schema during generation through constrained decoding, are the strongest guarantee available, and every major provider now ships one: OpenAI's structured outputs, Anthropic's output_format on the Claude API, Gemini's response schemas. They're my default everywhere. When the sampler literally cannot emit a token that would violate the schema, an entire class of failure disappears: no truncated JSON, no invented fields, no string where the number goes.

But there are two honest caveats. First, providers support different subsets of JSON Schema, nested unions, pattern constraints, or specific formats may quietly not be enforceable, and support varies enough between providers that your schema might not port cleanly. Second, and more fundamental: syntax is not semantics. Constrained decoding guarantees the output is shaped like an invoice. It does not guarantee the invoice date isn't in the wrong century, or that the line items sum to the total, or that the email address wasn't helpfully invented. A schema-valid hallucination is still a hallucination.

That's where the second pattern comes in: validate-and-re-ask. Parse the output against your own schema, Zod in TypeScript, Pydantic in Python, with semantic checks layered on, and when validation fails, send the errors back to the model verbatim with its previous attempt and ask it to correct them. Models are notably better at fixing a named mistake than at being right first time. Bound the loop at two or three attempts, and fail closed to a review queue when they run out. My repo at github.com/afareed007/docextract implements this end to end if you want a working reference.

So which do you use? Both, and this is the part people miss because they hear it as either/or. Native structured output is the first line: it makes syntactic failure impossible and semantic failure rarer, since constraining the sampling genuinely helps. Your own validation is the second line, because semantic checks encode facts about your domain that no decoder can enforce. The re-ask loop only exists for the failures that get past the first line. Skipping native mode wastes retries on syntax errors the provider would have prevented; skipping validation ships whatever the schema happened to permit.

On the function-calling side, the design discipline is about the tools themselves. Small argument surfaces, enums instead of free strings wherever a value has a known set, and descriptions written for the model as carefully as documentation for a junior engineer, because that's roughly what they are. Ten narrow tools beat three flexible ones; the model picks correctly from a menu far more reliably than it fills in a blank cheque.

And validate the calls, too. Strict tool modes now extend constrained decoding to the call itself, guaranteeing the arguments parse against your declared schema, take that wherever it's offered. But a syntactically perfect call can still name a customer that doesn't exist or a date range that makes no sense, so your dispatcher still needs an explicit rejection path, ideally one that tells the model what was wrong and lets it try again. The same validate-and-re-ask shape, one level up.

This is the work I do, building LLM integrations on the mechanisms that actually hold, not on regex and optimism. If you're wiring model output into a system where correctness matters, get in touch via the contact form.

Senior engineering roles — or production AI consulting?

I'm open to senior AI and software engineering roles and to selective consulting on agents, RAG, and LLM automation in production. If this post resonates — as a hiring manager or as someone shipping similar work — get in touch.

Get in touch

© 2026 Ahmed Fareed. All rights reserved.

LOADING