Favicon of LangExtract

LangExtract

Python library for LLM-based structured information extraction with exact source-text grounding and interactive HTML visualization.

LangExtract GitHub repository preview

LangExtract is a Python library that turns unstructured text, like clinical notes, reports, or literary works, into structured data using LLMs. You define an extraction task with a prompt and a handful of few-shot examples, and the library returns entities mapped back to their exact position in the source text. It's built for developers who need reliable, traceable extraction rather than free-form LLM output that's hard to verify.

Instead of hoping the model returns clean JSON, LangExtract enforces a schema derived from your examples and grounds every extracted span to its location in the original document. That grounding matters when you need to audit results or show a reviewer exactly where a claim came from, which is common in clinical, legal, and research text processing.

Key features

  • Precise source grounding: every extraction maps to its exact character offset in the input text, so you can highlight and verify results instead of trusting raw model output.
  • Structured output enforcement: uses controlled generation (on supported models like Gemini) to keep output shape consistent with your few-shot examples.
  • Long-document handling: chunks large text, runs extraction in parallel, and supports multiple passes to improve recall on the "needle-in-a-haystack" problem.
  • Interactive HTML visualization: generates a self-contained HTML file to browse thousands of extracted entities in context, no server required.
  • Multi-model support: works with Gemini, OpenAI models, Vertex AI, and local models via Ollama, with a plugin system for adding custom providers.
  • Domain-agnostic setup: define any extraction task with a prompt and a few examples, no fine-tuning needed.
  • Batch API support: OpenAI Batch API and Vertex AI Batch API integration for large, non-urgent extraction jobs to cut cost.

Ideal use cases

LangExtract fits well when you're pulling structured fields (characters, medications, findings, entities) out of medical notes, radiology reports, legal documents, or long-form text like novels, and you need to trace each extracted value back to its source for verification or compliance. It's also useful for building datasets from unstructured corpora where downstream tools expect consistent, schema-conforming JSON.

It's a good fit if you're comfortable writing few-shot examples to steer extraction behavior, since output quality depends heavily on how well your examples match the target text style. It also works well for large document processing thanks to chunking and parallel passes.

It's not the right tool if you need guaranteed factual accuracy independent of the underlying LLM, since extraction quality is tied to the chosen model's capability and your prompt/example quality. It's also not built for real-time low-latency single-token completions or tasks that don't benefit from schema-constrained extraction, like open-ended generation or chat.

Installation

Install from PyPI:

pip install langextract

For an isolated environment:

python -m venv langextract_env
source langextract_env/bin/activate  # On Windows: langextract_env\Scripts\activate
pip install langextract

From source (for development):

git clone https://github.com/google/langextract.git
cd langextract

pip install -e .            # basic install
pip install -e ".[dev]"     # development, includes linting
pip install -e ".[test]"    # testing, includes pytest

With Docker:

docker build -t langextract .
docker run --rm -e LANGEXTRACT_API_KEY="your-api-key" langextract python your_script.py

For cloud models (Gemini, OpenAI), set an API key as an environment variable or in a .env file:

export LANGEXTRACT_API_KEY="your-api-key-here"

Get keys from AI Studio (Gemini), Vertex AI (enterprise), or the OpenAI Platform. Local models via Ollama don't require an API key at all; just point model_url at your running Ollama instance.

Frequently asked questions

Share:

Stars
37.9K
Forks
2.6K
Last commit
1 day ago
Repository age
1 year
License
Apache-2.0
Self-hosted
No
Activity score
84/100
View Repository
Ad
Favicon

 

  
 

Similar to LangExtract

Favicon

 

  
 
Favicon

 

  
 
Favicon