Google's LangExtract Quietly Hit 36K Stars Solving AI Hallucinations in Documents
Google's open-source LangExtract library ties every AI-extracted field back to its exact source location, eliminating the hallucination verification problem at scale.
Takeaways
- Source grounding: Every extracted field maps to exact character offsets in the source text, with
char_interval=Noneflagging hallucinations automatically. - No fine-tuning needed: Define extraction tasks with a natural language prompt and 1-2 annotated examples -- the library handles the rest.
- Long document support: Handles 100k+ character documents via chunking, parallel processing, and multi-pass extraction to maximize recall.
- Multi-model: Works with Gemini (default), OpenAI GPT-4o, and local models via Ollama; Vertex AI Batch API available for cost control at scale.
- Top use cases: Clinical note structuring, legal contract extraction, financial report parsing, and RAG metadata generation in regulated industries.
- Free and open-source: Apache 2.0 license, 36.5k GitHub stars; install with
pip install langextract.
LangExtract is Google's open-source Python library for pulling structured data out of unstructured text using LLMs. It has quietly accumulated over 36,500 GitHub stars and 2,500 forks since its launch, and the reason is simple: it solves a problem that everyone building document pipelines runs into. When you ask an LLM to extract information from a contract, a clinical note, or a financial filing, how do you know it didn't make something up? LangExtract's answer is to make every extracted field point back to the exact character offset in the source document where it came from.
The hallucination problem, solved differently
The library addresses a specific gap in document processing pipelines: once text has been extracted from a document, how do you reliably convert unstructured narratives into structured data while proving every output is grounded in the source? The traditional alternatives are painful. Regex-based systems are brittle and break the moment the format changes. Training a custom NER model requires thousands of labeled examples and weeks of work. And raw LLM prompting gives you no audit trail -- the model might return the right answer, or it might confidently invent one.
The library's most significant architectural feature is its commitment to precise source grounding. Every Extraction object returned by the library contains not only the extracted text and its associated attributes but also the exact start and end character offsets that pinpoint its location in the original document. If the model can't find a span in the source text, char_interval is set to None -- a built-in signal that something was hallucinated. You can filter those out with a single line:
grounded = [e for e in result.extractions if e.char_interval]Few-shot, no fine-tuning
The core value proposition is built on a declarative, few-shot instruction approach. The library enables users to define complex extraction tasks using natural language prompts and a small number of high-quality examples, which obviates the need for brittle regular expressions or resource-intensive model fine-tuning. You write a prompt describing what you want, provide one or two annotated examples, and the library handles the rest. Here's the complete setup for extracting characters and emotions from text: