LangExtract
ActiveDescription
A Python library by Google for extracting structured information from unstructured text using LLMs with precise source grounding and interactive visualization, designed for data annotation and knowledge extraction workflows.
Key Features
- Precise source grounding — Every extraction maps to exact source text location with visual highlighting for traceability and verification
- Controlled structured outputs — Few-shot example enforced output schema, leveraging Gemini controlled generation for robust results
- Long document optimization — Text chunking, parallel processing, and multi-pass strategy to overcome needle-in-a-haystack challenges
- Interactive visualization — Self-contained HTML file for browsing thousands of extracted entities in their original context
- Flexible model support — Google Gemini cloud models and Ollama local models with custom model provider support
- Any-domain adaptability — Define extraction tasks for any domain with just a few examples, no model fine-tuning required
Use Cases
Tags
Categories
Quick Start
```python
import langextract as lx
result = lx.extract(
text_or_documents="Lady Juliet gazed longingly at the stars, her heart aching for Romeo",
prompt_description="Extract characters, emotions, and relationships in order of appearance.",
examples=[lx.data.ExampleData(
text="ROMEO. But soft! What light through yonder window breaks?",
extractions=[lx.data.Extraction(
extraction_class="character",
extraction_text="ROMEO",
attributes={"emotional_state": "wonder"}
)]
)],
model_id="gemini-3.5-flash",
)
lx.io.save_annotated_documents([result], output_name="results.jsonl")
```