LangExtract

Active
GitHub Python Apache-2.0

Description

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

💡 Clinical document extraction — Extract medications, dosages, diagnoses and patient info from medical records and clinical notes
💡 Academic literature structuring — Extract characters, emotions, relationships from full papers for literary analysis and knowledge graph construction
💡 Automated report parsing — Auto-parse radiology reports and other professional reports to extract key findings and conclusions
💡 Data annotation & knowledge extraction — High-quality annotations for training datasets supporting NER, relation extraction, and NLP tasks
💡 Document understanding pipeline — Front-end processing module for RAG systems, extracting structured metadata from unstructured documents

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")
```

Related Projects