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.
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.
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.