Epi: An Epistemic Type System for Containing LLM Hallucination in Generated Code

  • Randerson Oliveira Melville Rebouças UFRGS
  • Dante Augusto Couto Barone UFRGS
  • Eliseo Reategui UFRGS

Resumo


We introduce Epi, a domain-specific language whose type system makes the boundary between deterministic computation and LLM-inferred values explicit and enforceable at compile time. From a single .epi source, the Epi transpiler emits a complete Next.js project: database schema, API routes, authorization middleware, runtime validators, LLM call wrappers, and human-review checkpoints. Every AI-inferred field carries a type-level contract (an enum, a numeric range, a confidence threshold, an optional Bayesian prior) that the transpiler discharges in the generated code: the developer does not write the validation, the type does. We argue that LLM hallucination cannot be prevented, but that it can be contained, by construction, at a type-system boundary in generated code. We exercise Epi end-to-end on one source compiled against Anthropic Claude and Qwen 2.5 3B (local, via Ollama), where the boundary intercepts a low-confidence misclassification before it reaches a database of record, and probe it on 40 curated inputs: no value outside a declared enum ever reached the persistence path, while containment of merely wrong values is bounded by the model’s confidence calibration. Unlike typed-LLM interfaces such as BAML and DSPy, which give structure to individual model calls, Epi propagates one epistemic contract across the whole generated application, from the database column to the human-review checkpoint. A mechanized soundness proof and a controlled comparison against hand-written baselines remain future work. Epi is open-source under the Apache 2.0 license.

Palavras-chave: Type Systems, Domain-Specific Languages, Large Language Models, Code Generation, Information-Flow Control

Referências

Anthropic. 2025. Claude API Documentation. [link]. Accessed: May 2026.

Guillaume Baudart, Louis Mandel, Eric Atkinson, Benjamin Sherman, Marc Pouzet, and Michael Carbin. 2020. Reactive Probabilistic Programming. In Proceedings of the 41st ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2020). Association for Computing Machinery, New York, NY, USA, 898–912. DOI: 10.1145/3385412.3386009

Yves Bertot and Pierre Castéran. 2004. Interactive Theorem Proving and Program Development: Coq’Art: The Calculus of Inductive Constructions. Springer, Berlin, Heidelberg. DOI: 10.1007/978-3-662-07964-5

BoundaryML. 2024. BAML: A Domain-Specific Language for AI Engineering. [link]. Accessed: May 2026.

Maria I. Gorinova, Andrew D. Gordon, and Charles Sutton. 2019. Probabilistic Programming with Densities in SlicStan: Efficient, Flexible, and Deterministic. Proceedings of the ACM on Programming Languages 3, POPL, Article 35 (2019), 30 pages. DOI: 10.1145/3290348

Ziwei Ji, Nayeon Lee, Rita Frieske, Tiezheng Yu, Dan Su, Yan Xu, Etsuko Ishii, Ye Jin Bang, Andrea Madotto, and Pascale Fung. 2023. Survey of Hallucination in Natural Language Generation. Comput. Surveys 55, 12, Article 248 (2023), 38 pages. DOI: 10.1145/3571730

Omar Khattab, Arnav Singhvi, Paridhi Maheshwari, Zhiyuan Zhang, Keshav Santhanam, Sri Vardhamanan, Saiful Haq, Ashutosh Sharma, Thomas T. Joshi, Hanna Moazam, Heather Miller, Matei Zaharia, and Christopher Potts. 2023. DSPy: Compiling Declarative Language Model Calls Into Self-Improving Pipelines. arXiv:2310.03714 [cs.CL] [link]

Colin McDonnell. 2024. Zod: TypeScript-First Schema Validation with Static Type Inference. [link]. Accessed: May 2026.

Ulf Norell. 2009. Dependently Typed Programming in Agda. In Advanced Functional Programming (AFP 2008). Lecture Notes in Computer Science, Vol. 5832. Springer, 230–266. DOI: 10.1007/978-3-642-04652-0_5

Ollama Inc. 2024. Ollama: Run Open-Weight LLMs Locally. [link]. Accessed: May 2026.

Benjamin C. Pierce. 2002. Types and Programming Languages. MIT Press, Cambridge, MA, USA.

Alejandro Russo and Andrei Sabelfeld. 2010. Dynamic vs. Static Flow-Sensitive Security Analysis. In Proceedings of the 23rd IEEE Computer Security Foundations Symposium (CSF 2010). IEEE Computer Society, 186–199. DOI: 10.1109/CSF.2010.20

Andrei Sabelfeld and Andrew C. Myers. 2003. Language-Based Information-Flow Security. IEEE Journal on Selected Areas in Communications 21, 1 (2003), 5–19. DOI: 10.1109/JSAC.2002.806121

Erez Shinan. 2024. Lark: A Parsing Toolkit for Python. [link]. Accessed: May 2026.

Wasp. 2024. Wasp: A DSL for Full-Stack Web Apps. [link]. Accessed: May 2026.
Publicado
08/09/2026
REBOUÇAS, Randerson Oliveira Melville; BARONE, Dante Augusto Couto; REATEGUI, Eliseo. Epi: An Epistemic Type System for Containing LLM Hallucination in Generated Code. In: SIMPÓSIO BRASILEIRO DE LINGUAGENS DE PROGRAMAÇÃO (SBLP), 30. , 2026, São Paulo/SP. Anais [...]. Porto Alegre: Sociedade Brasileira de Computação, 2026 . p. 190-195. DOI: https://doi.org/10.5753/sblp.2026.29670.