langextract
Python library from Google for LLM-powered structured extraction with source grounding.
What is langextract?
A Python library from Google that uses an LLM to pull structured information out of unstructured text, then grounds every extraction back to its exact location in the source ('source grounding') and renders an interactive HTML view. It calls no model itself - you bring a provider: Gemini (default), OpenAI, or local models via Ollama (no API key needed).
langextract in depth
Pulling clean, structured data out of messy prose is a chore that has resisted easy automation for years. Regular expressions break on the first edge case, and feeding documents to a large language model often produces plausible JSON that is hard to trust, because you cannot tell which part of the source each field came from. langextract is a Python library from Google that tackles exactly this gap. You describe what you want in a prompt, supply a few worked examples, and the library uses an LLM to extract the fields you asked for. Its defining trait is source grounding: every extraction is tied back to the exact character span in the original text, so you can audit where each value came from instead of taking the model's word for it.
Under the hood, langextract is an orchestration layer rather than a model. It calls a provider you choose: Gemini by default, OpenAI, Vertex AI, or local models through Ollama with no API key. You define an extraction task as a prompt plus ExampleData objects, a few-shot pattern where your examples drive the model's behaviour and the expected output shape. The library asks the model to return extractions verbatim and in order, then maps each one back to its position in the source as a char_interval. For long documents it chunks the text, processes chunks in parallel, and can run multiple passes to improve recall. When an extraction cannot be located in the source, the char_interval comes back as None, which surfaces hallucinations rather than hiding them. It also renders a self-contained, interactive HTML view that highlights every entity in context.
The natural audience is anyone turning documents, reports, or notes into structured records where traceable provenance matters. The project's own examples are telling: extracting medications, dosages, and routes from clinical text, structuring radiology reports, and pulling characters and relationships from the full text of Romeo and Juliet to show it scales to long inputs. Beyond those, it suits research teams coding qualitative data, analysts extracting figures and dates from filings, and engineers building pipelines that convert support tickets, contracts, or lab notes into queryable tables. The HTML visualization makes it useful for review workflows too, where a human needs to confirm each extracted field against the original passage before it enters a database or report.
The honest caveats are real. For cloud models you depend on an external LLM API, which means running token costs and your text leaving your machine. Local inference via Ollama avoids both, but you take on the burden of hosting a model and accept the quality tradeoff that smaller local models often bring. The README is blunt that this is not an officially supported Google product, so there is no SLA, no guaranteed support channel, and Gemini model versions carry retirement dates you must track yourself. Accuracy is the project's own claim and depends heavily on the model, the prompt, and the quality of your examples. The medical examples ship with explicit warnings that they are illustrative and not for clinical use, which is a fair signal of how much validation any serious deployment still needs.
Against managed alternatives such as Google Document AI or the structured-output features built into commercial LLM platforms, langextract trades guarantees for transparency and control. Those services give you support, compliance posture, and turnkey pipelines, but they rarely show you exactly where each field originated. langextract's source grounding and zero-key local option are a genuine differentiator for teams that prize auditability or cannot send text to the cloud. Adopt it when provenance is non-negotiable and you have the appetite to tune prompts and examples. Treat it as too early when you need a supported product with contractual guarantees, or when you can neither send text to a cloud model nor run Ollama locally. For everyone in between, it is a well-designed, permissively licensed building block.
Pros & Cons
Pros
- Apache-2.0, permissive and OSI-open, no copyleft
- Provider-agnostic: cloud (Gemini/OpenAI/Vertex) or fully local via Ollama with no API key
- Source-grounding and an out-of-the-box HTML visualization are a genuine differentiator
Cons
- For cloud models it needs an external LLM API: running token costs, and your text leaves your machine (local only via Ollama)
- The README states plainly 'this is not an officially supported Google product' - no SLA
- Accuracy is the project's own claim and depends on the chosen model, prompt and examples
License
Apache-2.0 (OSI-open)
When it is interesting
Turning documents, reports or notes into structured data with traceable provenance.
When it is too early
If you need a supported product with guarantees, or cannot send text to a cloud model and do not want to run Ollama locally.
This repo featured in the 2026-06 edition of the Open-Source AI Radar.
LEANN
StarTrail-org
RAG on everything - graph-based vector index claiming 97% storage savings for private on-device search.
turbovec
RyanCodrai
Rust vector index with TurboQuant compression (ICLR 2026) - SIMD kernels, online ingest.
chandra
datalab-to
High-accuracy document digitization (OCR/layout) with code and an open model.