Google LangExtract: Structured Information Extraction with Source Grounding

Oct 6, 2025

Introduction

Processing unstructured text—from dense legal contracts to sprawling medical reports—often feels like searching for a needle in a haystack. Traditional regex-based parsing breaks the moment a document format shifts, and naive LLM prompts often hallucinate or fail to provide traceable evidence for their claims. Google’s LangExtract, an open-source Python library, solves this by enabling developers to extract structured, traceable data from unstructured text using Large Language Models (LLMs) with precise source grounding. By mapping every extraction back to its exact character offset in the source text, LangExtract transforms messy documents into reliable, structured datasets that are audit-ready and ready for Graph-RAG pipelines.

What Is Google LangExtract?

LangExtract is a Python library that uses LLMs to extract structured information from unstructured text documents based on user-defined instructions. Unlike standard LLM wrappers, it is specifically engineered for high-fidelity information extraction where traceability is non-negotiable. It allows users to define an extraction schema through a few-shot prompt and a set of examples, which the library then applies to documents of any length.

Maintained by Google, the project is released under the Apache-2.0 license, making it highly accessible for both commercial and open-source projects. It is designed to be model-agnostic, supporting cloud-based models like the Google Gemini family, OpenAI’s GPT series, and local open-source models via the built-in Ollama interface.

Why LangExtract Matters

For years, the industry has struggled with the “hallucination problem” in AI-driven data extraction. When an LLM extracts a date or a price from a 50-page contract, the developer often has no way to verify where that specific piece of data originated without manually re-reading the document. LangExtract eliminates this gap by providing precise source grounding, ensuring that every extracted entity is linked to its exact location in the original text.

Furthermore, LangExtract addresses the “needle-in-a-haystack” challenge of long-context windows. While modern LLMs can technically accept huge prompts, their recall often drops in the middle of the document. LangExtract implements an optimized strategy of text chunking, parallel processing, and multiple passes to ensure higher recall and consistency across thousands of pages.

As the industry shifts toward Graph-RAG (Retrieval-Augmented Generation), the need for clean, structured knowledge graphs has skyrocketed. LangExtract provides the fastest path to turn raw PDFs, HTML, and DOC files into the entities and relationships required to power these advanced AI agents and vector databases.

Key Features

  • Precise Source Grounding: Maps every extracted entity to its exact character offsets in the source text. This allows for visual highlighting in the original document, making verification and auditing effortless.
  • Reliable Structured Outputs: Uses few-shot examples and controlled generation (in supported models like Gemini) to enforce a consistent output schema, ensuring the results are always in a valid, predictable JSON format.
  • Optimized Long-Context Processing: Employs a sophisticated strategy of text chunking and parallel processing to handle documents with 25,000+ words without losing critical information.
  • Interactive Visualization: Automatically generates a self-contained HTML file that allows users to click on an extracted entity and instantly jump to its location in the source text for rapid review.
  • Flexible LLM Backend Support: Works seamlessly with cloud providers (Gemini, OpenAI, Anthropic) and local models (via Ollama), giving developers full control over privacy, cost, and latency.
  • Domain Agnostic Extraction: Requires no model fine-tuning. Users can define extraction tasks for any domain—from radiology reports to Shakespearean plays—using only a few well-chosen examples.
  • Multi-Pass Recall Enhancement: Runs extraction tasks multiple times over the same text to catch missed entities, significantly increasing the recall rate for complex documents.

How LangExtract Compares

When compared to traditional NLP libraries or naive LLM prompting, LangExtract occupies a unique space focused on traceability and scale.

Feature LangExtract Instructor / Pydantic spaCy NER
Source Grounding Exact character offsets None (usually) Token-level
Schema Flexibility Few-shot examples Pydantic models Fixed labels
Long Document Handling Chunking + Multi-pass Token limits Stream-based
Visualization Built-in HTML UI None displaCy

Unlike Instructor, which focuses on forcing LLMs to return Pydantic-validated JSON, LangExtract focuses on the provenance of that data. While Instructor tells you what the data is, LangExtract tells you where it came from. This makes it indispensable for high-stakes industries like law or medicine where a citation is as important as the value itself.

Compared to spaCy, LangExtract removes the need for expensive training sets. You don’t need thousands of labeled examples to build a Named Entity Recognition (NER) model; you only need three or four high-quality examples to guide the LLM. This drastically reduces the time-to-value for new extraction pipelines.

Getting Started: Installation

LangExtract is available via PyPI and can be installed in a variety of ways depending on your model provider needs.

Standard Installation

To install the core library with support for Google Gemini models:

pip install langextract

Integration with Other Providers

If you intend to use OpenAI models, you can install the optional dependencies as extras:

pip install "langextract[openai]"

Installation from Source

For developers who wish to contribute or modify the library:

git clone https://github.com/google/langextract.git
cd langextract
pip install -e .

Prerequisites: Ensure you have Python 3.10+ installed. For Mac users, libmagic is often required for document type detection.

How to Use LangExtract

The core workflow of LangExtract follows a simple four-step process: define the task, execute the extraction, process the results, and visualize the output.

First, you define an extraction task by providing a clear prompt and a few-shot example. The prompt tells the LLM what to look for, and the example shows it exactly how to format the output. This “few-shot” approach is what allows LangExtract to adapt to any domain without fine-tuning.

Next, you call the lx.extract function, passing in your source text, the prompt, and your examples. LangExtract handles the heavy lifting—chunking the text, managing API calls to the LLM, and calculating the character offsets for the grounding. Once the extraction is complete, the library returns a result object containing the structured data and its source coordinates.

Code Examples

Below are examples of how to implement LangExtract, ranging from a basic entity extraction to a complex literary analysis.

Basic Entity Extraction

This example shows how to extract simple entities from a short piece of text.

import langextract as lx
import textwrap

prompt = textwrap.dedent("""\nExtract the company name and the CEO's name from the text. 
Use exact text from the input for extraction_text.
""")

examples = [
    lx.data.ExampleData(
        text="Apple Inc. is led by Tim Cook.",
        extractions=[
            lx.data.Extraction(extraction_class="company", extraction_text="Apple Inc.", attributes={"status": "public"}),
            lx.data.Extraction(extraction_class="person", extraction_text="Tim Cook", attributes={"role": "CEO"})
        ]
    )
]

text = "Microsoft Corporation is currently headed by Satya Nadella."
result = lx.extract(text, prompt, examples)
print(result)

Complex Literary Analysis (Long Text)

This example demonstrates processing a large document (like the full text of Romeo and Juliet) to extract characters and their emotional states.

import langextract as lx
import textwrap

prompt = textwrap.dedent("""\nExtract characters, emotions, and relationships from the given text. 
Provide meaningful attributes for every entity to add context and depth. 
Important: Use exact text from the input for extraction_text. Do not paraphrase.
""")

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"})
        ]
    )
]

# Processing a large document from a URL
result = lx.extract("https://www.gutenberg.org/files/1590/1590.txt", prompt, examples)
print(result)

Advanced Configuration

To use cloud-hosted models like Gemini or OpenAI, you must configure your API keys. LangExtract looks for specific environment variables to authenticate requests.

You can set these in your terminal or via a .env file in your project root:

# For Google Gemini
export LANGEXTRACT_API_KEY="your-gemini-api-key"

# For OpenAI
export OPENAI_API_KEY="your-openai-api-key"

For developers using local LLMs via Ollama, no API key is required. Simply ensure the Ollama service is running locally (ollama serve) and that you have pulled the desired model (e.g., ollama pull gemma3:4b).

Real-World Use Cases

LangExtract is particularly powerful in domains where the cost of a mistake is high and the volume of text is immense.

  • Clinical Report Structuring: A healthcare provider can use LangExtract to process thousands of radiology reports, extracting dosages, frequencies, and medications into a structured database while maintaining a direct link to the original clinical note for physician verification.
  • Legal Contract Analysis: A legal team can extract parties, obligations, and expiration dates from a library of 1,000+ contracts. Because LangExtract provides source grounding, lawyers can click a result and instantly see the exact clause in the contract that triggered the extraction.
  • Financial Document Processing: Analysts can extract key performance indicators (KPIs) and risk factors from quarterly earnings reports. The multi-pass processing ensures that subtle risk factors buried in the footnotes are not missed.
  • Graph-RAG Pipeline Construction: Developers building AI agents can use LangExtract to turn raw corporate wikis into a knowledge graph of entities and relationships, providing a structured foundation for more accurate RAG responses.

Contributing to LangExtract

As an open-source project, Google encourages community contributions. If you find a bug or wish to suggest a feature, the standard GitHub flow is recommended.

Developers should first review the CONTRIBUTING.md file in the repository to understand the project’s coding standards and submission process. When submitting a Pull Request, ensure that all tests pass and that the following guidelines are followed: report bugs via the GitHub Issues tab, search for existing issues before opening a new one, and look for “good first issue” labels to get started with contributions.

Community and Support

The primary hub for LangExtract support is the official GitHub repository. Users can utilize GitHub Discussions for architectural questions and the Issues tab for bug reports and feature requests.

Because the library is integrated with the broader Google AI ecosystem, many users find helpful patterns in the Google AI Studio documentation and the Gemini API reference. For local model users, the Ollama community forums are a primary resource for optimizing on-device LLM performance.

Conclusion

Google LangExtract represents a significant shift in how we approach information extraction. By combining the reasoning capabilities of LLMs with the deterministic precision of character-level source grounding, it solves the most critical problem in AI data extraction: trust. When you can verify every single piece of data against its source, the potential for using AI in high-stakes professional environments increases exponentially.

Whether you are building a Graph-RAG pipeline, automating legal discovery, or structuring clinical data, LangExtract is the right choice when traceability is a requirement, not a feature. If you are simply looking for a quick summary of a text, a standard LLM prompt may suffice; however, for production-grade structured data, LangExtract is the superior tool.

Star the repo, try the quickstart, and join the community to start transforming your unstructured text into structured gold.

What is Google LangExtract and what problem does it solve?

Google LangExtract is a Python library that extracts structured information from unstructured text using LLMs. It solves the problem of AI hallucinations and lack of traceability by providing precise source grounding, mapping every extraction back to its exact location in the source text.

How do I install LangExtract?

You can install LangExtract via pip using the command pip install langextract. For OpenAI support, use pip install "langextract[openai]", and for local models, ensure you have Ollama installed and running on your system.

Does LangExtract require a paid API key?

The library itself is free and open-source. However, if you use cloud-hosted models like Gemini or OpenAI, you will need an API key from the respective provider. If you use local models via Ollama, no API key is required.

How does LangExtract compare to Instructor or LlamaIndex?

While Instructor focuses on Pydantic-based schema validation, LangExtract focuses on source grounding and traceability. It provides exact character offsets for every extraction, which is a key differentiator for auditing and verification workflows.

Can I use LangExtract for PDF extraction?

Yes, LangExtract can process text extracted from PDFs. When combined with a PDF parser like PyMuPDF, it can turn messy PDF content into structured JSON data with precise source grounding.

Can I use LangExtract for local LLMs?

Yes, LangExtract has built-in support for local models via the Ollama interface, allowing you to process sensitive data locally without sending it to a cloud provider.

What is the maximum document length LangExtract can handle?

LangExtract is optimized for long documents. It uses a strategy of text chunking and parallel processing to handle documents with 25,000+ words, overcoming the typical context window limits of most LLMs.