google /langextract

A Python library for extracting structured information from unstructured text using LLMs with precise source grounding and interactive visualization.

★ 39,296⑂ 2,750👁 166⑃ 330● 221▤ 21◈ 21
PythonApache-2.0Created 2025-07-08pypi.org/project/langextract/
39,296
stars
+7
2,750
forks
166
watchers
330
prs
221
issues
21
releases
21
discussions

README

LangExtract



!Tests



 Table of Contents

- Introduction
- Why LangExtract?
- Quick Start
- Installation
- API Key Setup for Cloud Models
- Adding Custom Model Providers
- Using OpenAI Models
- Using Local LLMs with Ollama
- More Examples
  - Romeo and Juliet Full Text Extraction
  - Medication Extraction
  - Radiology Report Structuring: RadExtract
- Community Providers
- Contributing
- Testing
- How to Cite
- Disclaimer

 Introduction

LangExtract is a Python library that uses LLMs to extract structured information from unstructured text documents based on user-defined instructions. It processes materials such as clinical notes or reports, identifying and organizing key details while ensuring the extracted data corresponds to the source text.


  
    
    
  



  Try the live demo →
  Run grounded extraction on Romeo and Juliet in your browser, no install required.


 Why LangExtract?

1.  Precise Source Grounding: Maps every extraction to its exact location in the source text, enabling visual highlighting for easy traceability and verification.
2.  Reliable Structured Outputs: Enforces a consistent output schema based on your few-shot examples, leveraging controlled generation in supported models like Gemini to guarantee robust, structured results.
3.  Optimized for Long Documents: Overcomes the "needle-in-a-haystack" challenge of large document extraction by using an optimized strategy of text chunking, parallel processing, and multiple passes for higher recall.
4.  Interactive Visualization: Instantly generates a self-contained, interactive HTML file to visualize and review thousands of extracted entities in their original context.
5.  Flexible LLM Support: Supports your preferred models, from cloud-based LLMs like the Google Gemini family to local open-source models via the built-in Ollama interface.
6.  Adaptable to Any Domain: Define extraction tasks for any domain using just a few examples. LangExtract adapts to your needs without requiring any model fine-tuning.
7.  Leverages LLM World Knowledge: Utilize precise prompt wording and few-shot examples to influence how the extraction task may utilize LLM knowledge. The accuracy of any inferred information and its adherence to the task specification are contingent upon the selected LLM, the complexity of the task, the clarity of the prompt instructions, and the nature of the prompt examples.

 Quick Start

 Note: Using cloud-hosted models like Gemini requires an API key. See the API Key Setup section for instructions on how to get and configure your key.

Extract structured information with just a few lines of code.

 1. Define Your Extraction Task

First, create a prompt that clearly describes what you want to extract. Then, provide a high-quality example to guide the model.

python
import langextract as lx
import textwrap

 1. Define the prompt and extraction rules
prompt = textwrap.dedent("""\
    Extract characters, emotions, and relationships in order of appearance.
    Use ex

Top issues

Discussions

Latest release

v1.7.2 v1.7.2 2026-11-07
Highlights

- Support google-genai 3.0, which replaced httpx with httpx2, so Gemini retries transient network errors again (605).
- Escape extraction data embedded in visualize() output (573) and declare a UTF-8 charset in visualization HTML (466).
- Route gpt-6 model IDs to the OpenAI provider (599) and send max_output_tokens as max_completion_tokens to OpenAI (600).
- Stop dict_to_annotated_document from mutating its input (577), load annotated documents saved with null extractions (584), and retokenize a Document after its text changes (583).
- Preserve literal strings such as NA and NULL w

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON