← Back to list

Solving Structural Hallucination: How We Built a 6-Stage Firewall for LLM Reliability

We had a problem at the School of Ancient Geomantic Education (SAGE). In generative AI terms, our model had high ‘Creative Fidelity’ but…

Arvind Jolly · 2026-05-24 07:37 · 0 claps · 5.3 min read
#generative-ai #machine-learning #nlp #ai-reliability #design-systems
Open on Medium ↗
Wiki topics: LLM · Large Language Models SAF · Safety & Alignment ML · Machine Learning AI · AI · General PRD · Product Design EDU · Education & Learning 🔒 · Cybersecurity

Solving Structural Hallucination: How We Built a 6-Stage Firewall for LLM Reliability

We had a problem at the School of Ancient Geomantic Education (SAGE). In generative AI terms, our model had high ‘Creative Fidelity’ but low ‘Schema Adherence’.

Our engine could interpret 1,000-year-old geomantic patterns with haunting accuracy across 10 different languages. But it had one fatal flaw: it couldn’t reliably remember to put a section header in the right place. Sometimes it would “clump” three paragraphs together; other times, it would translate the headers into German but leave the prose in English.

In the world of production AI, we call this Structural Hallucination.

Unlike factual hallucination (where the AI lies about a date or a name), structural hallucination is the tendency of a probabilistic model to break its own formatting constraints. For most “toy” apps, this is a minor annoyance. For a mission-critical engine like the SAGE Oracle — where structural integrity determines the validity of a reading — it was a silent killer.

Standard solutions like “better prompting” or “few-shot examples” only got us 90% of the way there. But in engineering, 90% is a failure.

In this article, I’ll walk you through how we achieved 100% schema compliance by building a deterministic “firewall” after the model: the Structural Integrity Enforcement Pipeline (SIEP).

In this post, we will explore:

  • Why “prompt engineering” is not enough for structural reliability.
  • How to use Semantic Anchors to synthetically recover missing headers.
  • The “Zero-Trust” approach to multilingual translation shielding.
  • How we moved from a “Stealth & Patent” mindset to an open, peer-reviewed framework (now published on Zenodo).

If you are building LLM applications that need to do more than just “chat,” this pipeline is for you.

1. The Problem: Structural Hallucination

When you ask a Large Language Model to generate a structured report — eight sections, in a specific order, with specific headers — the model will often comply. Mostly. It might generate seven sections. Or merge two into one. Or write the correct headers in the wrong language. Or silently drop a header and run two sections together in a single block of prose.

We call this Structural Hallucination: the tendency of a probabilistic model to produce output that is semantically correct but structurally malformed. Unlike factual hallucination (where the model invents false information), structural hallucination is harder to detect and more destructive to production systems — it breaks downstream parsers silently.

⚠️ The Core Problem

In the SAGE Oracle engine, a “clumped” or missing section means a user receives a reading that is incomplete or unrenderable. The application fails not because the AI was wrong, but because the AI was structurally imprecise.

Standard solutions — better prompting, few-shot examples, stricter temperature settings — reduce the failure rate but cannot eliminate it. A probabilistic model will always have a non-zero failure probability per generation. At scale, across 10 languages and thousands of users, that probability becomes a certainty.

2. The Solution: Post-Processing as a Structural Firewall

Rather than engineering around the LLM (using better prompts) or through it (using multiple API calls to self-correct), the SAGE engine engineers after it. We treat the LLM as an unreliable narrator and pass every response through a deterministic enforcement pipeline before it ever touches the user interface.

The architecture rests on a fundamental insight: even when an LLM fails to write the correct structural marker, the semantic content of each section is still present in the output. A reading that is missing “Section 3: Planetary Influences” still contains the prose about planetary influences — it’s just untagged.

💡 Core Innovation

If we can identify semantic content reliably, we can synthetically reconstruct the missing structural markers from the prose itself — without re-querying the LLM.

3. The Six-Stage Pipeline

The Structural Integrity Enforcement Pipeline (SIEP) operates in six distinct stages, each targeting a specific failure mode:

  1. Ingestion & Prompt Echo Purge

The raw LLM response is received and immediately scanned for “prompt echoes” — instances where the model has repeated back its own instructions. These are stripped via pattern matching before any structural analysis begins.

  1. Doctrine Firewall & Header Shielding

A locale-aware registry of canonical section headers is applied. Any header that has been malformed, partially translated, or omitted is replaced with the exact canonical string for the target locale. Figure names are also normalized against a hard-coded vocabulary dictionary to prevent LLM-invented terminology from corrupting the doctrinal purity of the reading.

  1. Anchor-Based Synthetic Header Recovery

This is the core innovation. If a section header is entirely absent, the engine scans the surrounding prose for Semantic Anchors — domain-specific keywords unique to each section (e.g., specific life-domain terminology for Section 4). When an anchor is detected — using a canonical registry of domain-specific keywords associated with the missing section (e.g., ‘career’ or ‘Artha’ for Section 4) — the corresponding header is synthetically injected at the correct position, recovering the section without an LLM retry.

  1. Hyper-Isolated Translation Shielding

For multilingual output, content is wrapped in opaque positional markers ([PART-N]) before being passed to the translation model. The markers are explicitly excluded from translation, preventing the translation LLM from corrupting the established segment structure. A multi-variant regex ([\[【「]?PART\s*-\s*(\d+)) handles the marker variations that different LLMs naturally produce.

  1. Token Desquashing

Cross-lingual generation causes technical tokens to “squash” — fusing with adjacent words. A fuzzy pattern normalizer identifies and repairs these fusions (e.g., H1-TariqH1: Tariq), maintaining machine-readable data integrity. A hallucinated placeholder repair system also fixes malformed tokens like {{RF_Tariq using role-deduplication logic.

  1. Automated Multi-Metric QA Validation

A final validation loop runs 12 independent checks: header order verification, section count, bullet counts per section, script purity ratio (to detect language leaks), Mojibake detection, prose repetition similarity, and technical token leakage checks. Only responses passing all checks are delivered to the user interface.

4. The Results in Production

After deploying the SIEP across all 10 supported languages (including complex scripts like Arabic, Chinese, and Japanese), the impact on our system reliability was immediate:

  • 100% Schema Compliance: Over thousands of generations, we have observed zero instances of “clumped” sections or missing headers. Every reading now follows the exact 8-section architecture required by the SAGE engine.
  • Zero Language Leaks: The “Zero-Trust” translation shielding has eliminated cases where the LLM would accidentally respond in English for a non-English user.
  • Significant Latency Reduction: By deterministic “repairing” hallucinated outputs in milliseconds, we eliminated the need for LLM retry loops, which previously added 5–10 seconds to our response times.

The SIEP proved that you don’t need a “smarter” model to get better results — you just need better engineering around the model you already have.

5. Conclusion:

Generative AI is a powerful tool, but for it to be useful in high-precision industries — from divination to data extraction — it must be made reliable. The SIEP framework is our contribution to that mission. We believe that by treating the LLM as an “unreliable narrator” and enforcing structure deterministically downstream, we can build a new generation of apps that users can actually trust.

The SIEP isn’t about making the AI smarter; it’s about acknowledging the inherent probabilistic nature of LLMs and applying a deterministic safety layer. By shifting the burden of structural integrity from the ‘creative’ model to a ‘logical’ pipeline, we achieve production-grade stability without sacrificing the model’s expressive power.

Experience the Oracle The engineering described in this post powers every reading at SAGE (School of Ancient Geomantic Education). If you’d like to see the SIEP in action and experience a highly-reliable geomantic reading, visit us at **www.dotsofdestiny.com**.

We are currently offering 11 free Energy Tokens for all new seekers to explore the platform.

Dig Deeper For those interested in the academic details, you can find the full white paper and official record on Zenodo: 👉 **Official DOI Record: 10.5281/zenodo.19945234**

I’d love to hear your thoughts in the comments. How are you handling structural reliability in your own LLM pipelines?

Written on behalf of the SAGE Engineering Team


메타데이터
post_id
66d91dc4a1c3
slug
solving-structural-hallucination-how-we-built-a-6-stage-firewall-for-llm-reliability-66d91dc4a1c3
url
https://medium.com/@schoolofgeomancy/solving-structural-hallucination-how-we-built-a-6-stage-firewall-for-llm-reliability-66d91dc4a1c3
canonical_url
https://medium.com/@schoolofgeomancy/solving-structural-hallucination-how-we-built-a-6-stage-firewall-for-llm-reliability-66d91dc4a1c3
author_url
https://medium.com/@schoolofgeomancy
status
ok
fetched_at
2026-06-09 15:37:30