By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
The extraction pipeline ran clean for six weeks. Then a vendor changed the invoice layout, your prompt said return JSON, and the model returned JSON wrapped in a sentence: Here is the data: {...}. Your parser threw. You added a regex to strip the wrapper, and two weeks later the wrapper changed.
There is a firmer contract than asking politely. You describe a tool, give it an input schema written in JSON Schema (a standard way to describe allowed fields, types and values), and force the model to call it. The model's answer is then the tool call input, and that input is checked against the schema before you ever read it.
A tool definition has three parts: a name, a description, and an input_schema that lists allowed properties, their types and their values. tool_choice then decides whether the model may use it. Setting tool_choice to {'type': 'tool', 'name': 'record_invoice'} forces that one tool, so the reply cannot be prose.
The forced call is the mechanism. The schema is the contract. Together they turn a probabilistic answer into an object your code can refuse, which is the property you actually need in a pipeline.
Most loose schemas fail in the same few places. The table lists the keywords worth deliberate thought, and the bug each one prevents.
| Keyword | What it does | The bug it prevents |
|---|---|---|
required | Names the fields that must be present | A missing total that becomes a null in your warehouse |
enum | Closes a field to a fixed list of values | Severity values that drift into synonyms or new spellings |
| nullable type | Allows null without making the key optional | An absent field that looks the same as an unknown value |
additionalProperties: false | Rejects keys you never defined | Confident extra fields that break a strict consumer |
minimum and maximum | Bounds a number | Negative totals and impossible quantities |
Strict mode goes one step further. Every property is listed in required, optionality is expressed with a nullable type such as ['string', 'null'], and additionalProperties is false. That combination is what makes extra keys are rejected a guarantee instead of a hope.
The snippet below shows the parts of the request that shape the output. It defines one tool, forces it, and checks a sample payload the way a strict consumer would.
TOOL = {
"name": "record_invoice",
"description": "Record one extracted invoice. Call this once per document.",
"input_schema": {
"type": "object",
"additionalProperties": False,
"required": [
"invoice_id",
"total_cents",
"currency",
"status",
"status_detail",
],
"properties": {
"invoice_id": {"type": "string", "minLength": 1},
"total_cents": {"type": "integer", "minimum": 0},
"currency": {"type": "string", "enum": ["USD", "EUR", "GBP"]},
"status": {"type": "string", "enum": ["paid", "unpaid", "other"]},
"status_detail": {"type": ["string", "null"]},
"due_date": {"type": ["string", "null"]},
},
},
}
REQUEST = {
"tools": [TOOL],
"tool_choice": {"type": "tool", "name": "record_invoice"},
"messages": [
{"role": "user", "content": "Extract the invoice from this document."}
],
}
def schema_problems(payload: dict, schema: dict) -> list[str]:
problems = []
for name in schema["required"]:
if name not in payload:
problems.append(f"missing {name}")
if schema.get("additionalProperties") is False:
extra = set(payload) - set(schema["properties"])
problems.extend(f"unexpected {name}" for name in sorted(extra))
return problems or ["ok"]
sample = {
"invoice_id": "INV-9",
"total_cents": 12900,
"currency": "USD",
"status": "other",
"status_detail": "credit note",
"confidence_note": "guessed",
}
print(schema_problems(sample, TOOL["input_schema"]))Enums are the strongest constraint you can write, and the fastest way to break a pipeline. The moment a real document contains a status your enum does not list, a strict schema has nowhere to put it. The model either forces a wrong member or fails the call.
The pattern that survives is an other member plus a detail field. The enum stays closed and switchable, and the raw text lands in status_detail for the reviewer who has to extend the list. In the schema above, status accepts other and status_detail is a nullable string, so the unknown value has a legal home.
Write down the rule for who resolves other. If nobody does, you have built a quiet queue of unread values instead of a closed enum.
Write a schema for an interview note with one enum, one nullable field and one open-ended value.
outcome as an enum of advance, hold, reject and other.outcome_detail as a nullable string and require it whenever outcome is other.strongest_signal as a plain string, because free text is the point of a note.required.Key takeaways
A tool schema plus a forced tool_choice makes prose impossible and shape checkable.
Spend keywords deliberately: required, enum, nullable types, no extra keys.
Strict mode requires every property and rejects keys you did not define.
Keep a closed enum open with an other member plus a detail field.
A schema validates shape, not truth; business checks still belong in your code.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.