the identical form of reply. One desires a single quantity with a quotation, one other an inventory with one merchandise per row, a 3rd a sure or no with a caveat. Pressure all of them by one technology name and every form degrades the others. The repair is a small set of technology patterns, one per reply form.
This text is a companion to Enterprise Doc Intelligence, the sequence whose philosophy is specified by Amplify the Professional. It zooms in on brick 4 (technology) of the four-brick structure, and pushes again on the vocabulary groups attain for when a RAG reply is incorrect.
Hallucination, within the strict sense, is a fabrication from the mannequin’s parametric reminiscence: the LLM invents a truth with none grounding within the enter. In RAG the mannequin reads the context; when the reply is incorrect, the trigger is upstream, within the extraction chain (parsing, query, retrieval, or the technology contract itself). Calling each failure a hallucination closes the controversy on the place to behave. Naming the actual trigger opens it again up.
The mainstream story has technology as ship retrieved chunks + query to the LLM, get a solution string again. We reject this entire body. The reply just isn’t a string. It’s a typed Pydantic object with citations, constancy flags, and self-assessment fields. The LLM is a operate, not an oracle. The schema is the contract. The validator runs earlier than the consumer sees something.

📓 Reproduce the seven patterns within the shipped pocket book: fill the typed AnswerWithEvidence schema on a broker-corpus query, watch every self-assessment flag flip, then rerun with the schema decomposed to see how a small mannequin catches as much as a frontier one. Repo → doc-intel/notebooks-vol1.

The naive baseline this text pushes again on

The naive pipeline asks the LLM for a solution and trusts what comes again. There is no such thing as a separation between discovered and invented, no place to file confidence, and no technique to flag not discovered as a first-class end result. We fill the schema as a substitute: each subject is a query the pipeline asks, and each reply is checkable.

Under are the seven patterns that preserve technology on the typed-contract aspect. The primary six are the contract itself. The seventh names a constraint the contract should respect when the mannequin is small.
Sample 1 – The LLM is a operate, not an oracle
The reply schema is a contract: structured retrieval in, a typed Pydantic object out, with citations, constancy flags, and pipeline-feedback fields, by no means “the reply as a string”. Every little thing the technology brick does is in service of filling that contract, subject by subject.
Take “what’s the premium quantity?”. The naive baseline returns a sentence: "The premium is $124 per 30 days, per the contract.". To make use of that downstream you must parse the quantity again out of the prose, and nothing tells you the place it got here from or how positive the mannequin was. The sequence returns a row as a substitute: Quantity(worth=124.0, foreign money="USD", unit="month", proof=[Span(line_start=42, line_end=42, quote="premium of $124 / month")], answer_found=True, confidence=0.95). The worth is already typed, the proof factors on the actual line, and the self-assessment fields journey with it. The caller reads structured values, not textual content it has to re-read.
→ Article 8A (the reply contract) develops the typed contract in full.
Sample 2 – Extract typed values, by no means compute
Ask for Quantity(worth, foreign money, unit) and let Python examine with a visual change fee; computation contained in the mannequin erases the audit path and silently does conversions no human can replay. The LLM extracts, Python computes.
Take “is the premium greater than 100 euros per 30 days?”. Ask the LLM straight and it solutions "sure, the premium is round 130 EUR / month", having transformed $124 to euros with a fee it by no means exhibits you. You can’t examine the arithmetic, and subsequent month the identical query may convert at a unique invisible fee. The sequence extracts the uncooked worth as a substitute, Quantity(worth=124, foreign money="USD", unit="month"), and lets Python do the comparability with a fee that sits within the audit log, 1 USD = 0.92 EUR. That offers 114 euros, so the reply is True, and each step, the extraction, the speed, and the comparability, may be replayed and checked by hand.
→ Article 8A (the reply contract) covers typed values and the no-compute self-discipline.
Sample 3 – Completeness from construction, not self-rating
The LLM contained in the retrieval scope can’t see whether or not the following web page continues an inventory; retrieval pulls one additional web page and the pipeline checks for a bit boundary, so completeness is deterministic and grounded in construction. A mannequin that may solely see what was despatched to it can’t decide what was lacking.
Take “checklist all exclusions”. The exclusions part runs from web page 7, and the following part, “Limitations”, begins on web page 10, so the reply lives on pages 7 to 9. Naive RAG arms these pages to the LLM and asks “is the checklist full?”. The mannequin can solely see what it was given, so it reads pages 7 to 9 and says sure, with no technique to know whether or not a tenth exclusion was ever retrieved. The sequence checks the construction at retrieval time as a substitute: does the following web page nonetheless belong to the exclusions part? If it does, fetch it; as soon as it crosses into “Limitations”, cease. Completeness turns into a truth about the place the part ends, not a guess the mannequin makes about textual content it can’t see.
→ Article 8A (the reply contract) and Article 8C (validation) construct programmatic completeness.
Sample 4 – Two booleans, not one confidence float
Off-corpus, partial, and full every path to a unique subsequent motion, and two flags power clearer choices than a single scale. The float-confidence body collapses three totally different outcomes into one quantity.
Take “checklist all exclusions” once more, however this time retrieval discovered web page 7 and missed web page 8. A single confidence = 0.6 tells the orchestrator virtually nothing: it can’t inform whether or not two of three exclusions got here again or whether or not the entire reply was invented. The sequence splits that into two booleans. answer_found = True says the query is answerable from this doc, and complete_answer_found = False says the checklist that got here again is partial. These two flags map to 3 clear subsequent strikes: preserve retrieving when the reply is discovered however incomplete, ship when it’s discovered and full, and refuse when it’s not discovered in any respect. A single float collapses these three outcomes into one quantity and loses the choice.
→ Article 8A (the reply contract) explains the two-boolean cut up.
Sample 5 – One immediate per form, dispatched at runtime
A dispatcher composes BASE + one form fragment + non-compulsory constraints and information which fragments utilized, so a incorrect format six months later is traceable, not like a mega-prompt that grows uncommented clauses. A single immediate that everybody retains appending to is a typical RAG codebase odor; composing it from named fragments is what retains every reply’s immediate reconstructable.
Take “efficient date?”, whose reply form is Date. A naive baseline runs one generic immediate that all the time asks for “a solution”, regardless of the query. The sequence composes the immediate from elements as a substitute: the BASE immediate, plus a Date form fragment that claims "return YYYY-MM-DD", plus the DateAnswer schema. The audit log information precisely which elements have been used, prompt_id=BASE@v3 + form=date_v2. So when the mannequin returns “July 1st 2026” six months later and somebody has to clarify the incorrect format, the workforce can rebuild that actual immediate from the log and reproduce the bug, as a substitute of guessing which clause of a thousand-line mega-prompt fired.
→ Article 8B (immediate meeting) designs the immediate dispatcher.
Sample 6 – No reasoning fashions on JSON extraction
The schema already constrains the work, so the additional “pondering” provides latency and wanders off the schema with out including precision. Reasoning fashions earn their place on open-ended duties; on schema-constrained extraction, they’re over-engineered.
Take “what’s the deductible quantity?”. A workforce hoping for cleaner extraction switches to a reasoning mannequin. It thinks for eight seconds and returns the identical quantity a plain mannequin returned in a single, as a result of the schema had already pinned the output to a single typed subject. The reasoning tokens value latency and cash and purchased nothing, since there was no open-ended judgment to make. The stance that follows is easy: use the smallest mannequin that fills the schema reliably, and save reasoning for the steps that genuinely want it, such because the LLM arbiter on the finish of retrieval.
→ Article 8A (the reply contract) and Article 8B (immediate meeting) work by the trade-offs.
Sample 7 – Decompose for small fashions, one name for the massive ones
A frontier mannequin can fill a compound schema (extract + convert + format) in a single name; a small mannequin can’t, and asking for it invents the derived fields silently. Mannequin dimension units the granularity of the contract, not the form of it.
Take “is the premium greater than 100 euros per 30 days?” with a compound schema, PremiumComparison(raw_amount: Quantity, converted_eur: float, exchange_rate: float, over_100_eur: bool). GPT-4.1 fills each subject appropriately in a single name: it extracts $124/month into raw_amount, converts to 114.08 EUR with a visual fee, and returns over_100_eur=True. Hand the identical schema to llama-3.2-3B and the mannequin returns raw_amount populated, converted_eur invented at 117.6 (from an imaginary 0.95 fee the log by no means carries), and over_100_eur=True derived from that invented fee. The small mannequin doesn’t fail JSON validation. It fills each subject. Three of the 4 are fabricated.
The repair is to decompose the contract into phases the small mannequin can deal with in isolation. First name: extract Quantity(worth=124, foreign money="USD", unit="month"). Python takes over: fee lookup (logged, 1 USD = 0.92 EUR), conversion (124 * 0.92 = 114.08 EUR), threshold (114.08 > 100 = True). Non-compulsory second name if a natural-language abstract is required: hand the finished compound row again with a plain format immediate. Identical last row, similar accuracy, no fabricated intermediate.
The rule: the dimensions of the mannequin determines the variety of extraction calls, not the form of the schema. Massive mannequin = one dense name. Small mannequin = a series of typed extracts with Python computing within the center and one non-compulsory format name on the finish. The typed contract survives both method; solely the granularity modifications.
Article B05 (selecting a mannequin) benchmarks the granularity trade-off throughout a fleet of 13 fashions (hyperlink to come back).
The seven patterns share one transfer: refuse the LLM-as-oracle body, and deal with technology as filling a typed contract. The schema is the contract; citations and self-assessment fields are first-class outputs; the validator runs earlier than the consumer sees something. Sample 7 provides the operational nuance: the identical contract holds throughout mannequin sizes, however a small mannequin wants it damaged into phases the mannequin can fill with out inventing derived values. The deep-dives (8A, 8B, 8C) ship runnable code on actual paperwork; this piece is {the catalogue} that names every sample and factors to the code that implements it.
Throughout sectors and professions
The typed-contract self-discipline (Pydantic schema + citations + self-assessment fields) holds in each area. What modifications per query is the schema form. The contract sample stays the identical. 5 sectors beneath, 5 reply shapes, one validator operating the identical checks throughout all of them.

The validator runs the identical checks throughout all 5 rows: line spans inside doc bounds, verbatim quotes match cited strains, format constraints revered (a Period truly parses as a period, an Quantity carries foreign money and unit). The authorized row demonstrates the two-boolean cut up (Sample 4): the reply was discovered on this doc however just isn’t full with out a cross-reference, so the orchestrator continues retrieval as a substitute of delivery a partial reply. The medical row exhibits the audit path (Sample 1): the caveat captures a known-unknown the LLM couldn’t resolve.
The place these patterns land within the sequence
The numbered articles develop every sample in code, with runnable notebooks:
Sources and additional studying
Most writing on LLM technology comes out of chatbots and open-ended textual content. The sequence assumes one thing totally different: the reply should be auditable, and the LLM should refuse to invent.





