LangExtract
A Python library by Google for extracting structured data from unstructured text using LLMs, with precise source grounding and interactive HTML visualization.
At a Glance
About LangExtract
LangExtract is an open-source Python library published by Google under the Apache 2.0 license. It uses large language models to extract structured information from unstructured text documents based on user-defined instructions and few-shot examples, mapping every extraction back to its exact location in the source text. The library is authored by Akshay Goel (goelak@google.com) and is available on PyPI as langextract.
What It Is
LangExtract sits in the structured-data-extraction category of NLP tooling. Its core job is to take free-form text — clinical notes, legal documents, literary works, radiology reports — and produce typed, schema-consistent output objects that are traceable to specific character intervals in the original document. Unlike generic LLM prompting, LangExtract enforces output schemas, handles long-document chunking automatically, and generates self-contained interactive HTML files for reviewing thousands of extracted entities in context. It requires no model fine-tuning; a few high-quality examples are enough to define a new extraction task.
Core Capabilities
- Precise source grounding: Every extraction is mapped to a
char_intervalin the source text. Extractions that cannot be located in the source (e.g., hallucinated from few-shot examples) are flagged withchar_interval = Noneand can be filtered out. - Reliable structured outputs: Output schemas are enforced using controlled generation on supported models (Gemini) or structured outputs / JSON mode on OpenAI models.
- Long-document optimization: Text chunking, parallel processing (
max_workers), and multiple extraction passes (extraction_passes) address the "needle-in-a-haystack" problem for documents of arbitrary length. - Interactive visualization: A single
lx.visualize()call generates an animated, self-contained HTML file for reviewing entities in their original context. - Flexible LLM support: Built-in providers for Google Gemini (via AI Studio or Vertex AI), OpenAI (GPT-4o, GPT-4o-mini, and compatible endpoints), and local models via Ollama. A plugin system allows community-contributed providers.
- Domain agnostic: Demonstrated use cases include literary character extraction, medication and dosage extraction from clinical notes, and radiology report structuring (RadExtract demo on HuggingFace Spaces).
Model Support and Provider Architecture
LangExtract ships with three built-in providers and a lightweight plugin system for extending to additional backends:
- Gemini (default):
gemini-3.5-flashis the recommended default. Vertex AI Batch API is supported for cost-optimized large-scale workloads. - OpenAI: Requires
pip install langextract[openai]. Supports structured outputs, JSON mode, and the OpenAI Batch API for high-volume jobs. Compatible with any OpenAI-compatible endpoint viaModelConfig. - Ollama (local inference): Runs models like
gemma2:2blocally without an API key. Uses JSON mode viaFormatModeSchema. - Custom providers: Registered via
@router.register(...)fromlangextract.providers, distributable as separate Python packages, and resolved via priority-based routing.
Setup Path
Installation is a single pip command (pip install langextract) requiring Python ≥ 3.10. Optional extras include [openai], [dev], [test], and [notebook]. Docker support is included for containerized workflows. API keys for cloud models can be supplied via environment variable (LANGEXTRACT_API_KEY), a .env file, direct parameter, or Vertex AI service accounts.
Update: v1.6.0 (July 2, 2026)
The current release is v1.6.0, published July 2, 2026. The release history shows rapid iteration since the initial 1.0.0 release in July 2025, with major versions reaching 1.6 within roughly one year. Notable milestones in the release history include v1.1.0 (November 2025), v1.2.0 (March 2026), v1.3.0 (April 2026), v1.4.0 and v1.5.0 (May 2026), and v1.6.0 (July 2026). The project carries a Zenodo DOI (10.5281/zenodo.17015089), indicating it is intended for academic citation. The GitHub repository is tagged with topics including gemini, gemini-api, information-extraction, llm, nlp, and structured-data.
Community Discussions
Be the first to start a conversation about LangExtract
Share your experience with LangExtract, ask questions, or help others learn from your insights.
Pricing
Open Source
Free to use under the Apache 2.0 license. Install via pip.
- Full library access
- Gemini, OpenAI, and Ollama provider support
- Interactive HTML visualization
- Long-document parallel processing
- Custom provider plugin system
Capabilities
Key Features
- Precise source grounding with character-interval mapping
- Structured output schema enforcement via few-shot examples
- Long-document chunking and parallel processing
- Multiple extraction passes for higher recall
- Interactive self-contained HTML visualization
- Google Gemini support (AI Studio and Vertex AI)
- OpenAI model support with structured outputs and Batch API
- Local LLM inference via Ollama
- Custom model provider plugin system
- Vertex AI Batch API for cost-optimized large-scale extraction
- JSONL output format for extracted entities
- Automatic hallucination detection (char_interval = None)
- Domain-agnostic extraction via few-shot examples
- Docker support
- Jupyter/Colab notebook compatibility
