Acadbyte
Start free

Structured Outputs & Function Calling

'Respond only with JSON' works 299 times, then breaks your parser at 2am. What makes it impossible to break instead of merely unlikely?

Intermediate7 min2 concepts · 1 question · 1 card

Asking nicely vs. making it impossible

You need JSON. You write "Respond only with valid JSON." It works. It works again. On the 300th call it returns something like this — a chatty preamble, then the JSON politely wrapped in a markdown code fence:

Sure! Here's the JSON you asked for:
<fenced code block>
{"sentiment": "positive"}
<end fence>

and your parser throws at 2am.

This isn't disobedience. "Sure! Here's..." is a genuinely plausible continuation of a request — the model has seen a million of them. A prompt makes the format likely, not certain, and "likely" is exactly the kind of promise that holds in testing and breaks in production.

Constrained decoding

Structured outputs change the mechanism instead of the wording. You supply a JSON Schema. At every generation step, the decoder masks out any token that could not legally continue a valid instance of that schema.

If the schema says the next character must be {, then the tokens spelling "Sure" have their probability set to zero. Not discouraged. Not penalised. Unreachable.

The difference is categorical, and it's worth being precise about:

You stopped negotiating with a probability distribution and started editing it.

Function calling is the same idea

A tool definition is a schema, and the model's call is constrained to match its signature. issue_refund(order_id: string, amount: number) cannot come back with a missing amount or a string where a number goes. Same machinery, different destination.

The rest of this topic comes with a free account

Reading stays open to everyone. The questions, the cards and the review schedule need somewhere to remember you — that’s all an account is for here.

Or start with the free track, which needs no account at all.

Next in GenAI in Practice

Evaluating LLM Outputs

Your prompt change looks better on the three inputs you tried. How would you know if it wasn't?

Read the preview