Inspiration
Integrating Large Language Models into mission-critical software pipelines—such as financial reconciliation, healthcare triage, and automated SQL generation—often breaks down at the serialization boundary. Even with JSON mode or standard function calling, LLMs frequently violate complex cross-field business rules (e.g., an end date preceding a start date, line items failing to sum to a stated total, or referencing non-existent foreign keys). Relying on brute-force retry loops wastes tokens and introduces unpredictable latency. We built SchemaGuard to guarantee mathematical, relational, and logical validity on every LLM generation pass using constrained decoding paired with symbolic constraint solvers.
What it does
SchemaGuard sits between an application's inference layer and downstream databases or APIs, enforcing strict neuro-symbolic validation in real time:
Context-Free Grammar (CFG) Token Masking: Dynamically compiles Pydantic models and regex rules into finite-state automata that mask invalid vocabulary logits before token sampling occurs—eliminating syntax errors at the source.
SMT Solver Cross-Field Verification: Translates declarative business logic invariants (such as arithmetic balances, date ranges, and conditional dependencies) into formal constraints verified in sub-millisecond time by an embedded Z3 theorem prover.
Targeted Backtrack Repair: When a semantic invariant fails mid-generation, SchemaGuard pinpoints the exact offending JSON field, rewinds the KV-cache to that token boundary, and injects a symbolic counterexample hint to guide immediate self-correction.
How we built it
Logit Processor & Grammar Engine: Built in Rust with PyO3 bindings to compile JSON schemas into deterministic pushdown automata capable of masking 128k-token vocabularies in under 0.4ms per step.
Symbolic Verification Layer: Integrated Microsoft's Z3 SMT solver via Python to evaluate complex arithmetic and relational assertions across generated fields.
Inference Adapters: Built native middleware hooks for vLLM, llama.cpp, and OpenAI-compatible streaming endpoints.
Interactive Playground & Audit UI: Developed a web workbench using React, TypeScript, and Monaco Editor where developers can define custom invariants, inspect logit masks token-by-token, and benchmark repair latency.
Challenges we ran into
Translating arbitrary Python validation functions into Z3-compatible symbolic expressions without forcing developers to learn formal verification syntax was our biggest design challenge. Early versions required writing raw SMT-LIB rules, which created steep friction. We solved this by building a lightweight AST transpiler that inspects standard Python assert statements and type annotations, automatically converting common arithmetic, string-length, and temporal comparisons into Z3 constraints.
Accomplishments that we're proud of
Achieved 100% structural and semantic compliance across a 1,500-prompt financial invoice extraction benchmark where standard JSON-mode prompting failed on 23% of cross-field arithmetic checks.
Reduced average validation-and-repair overhead to under 35 milliseconds, cutting token consumption by 48% compared to full-payload retry loops.
What's next for SchemaGuard
Adding live database schema introspection so foreign-key existence checks can be compiled into token masks automatically before generation begins.
Releasing lightweight WebAssembly builds to run client-side structured output verification directly in browser environments.