Introduction: Why We Need LangExtract

Imagine you’re buried in a pile of documents—medical reports, legal contracts, customer reviews—it’s hard to sift through all that text manually. You want the key facts without the mess, like extracting a patient's medicine dosage or the parties mentioned in a contract. That's exactly where LangExtract comes in.

LangExtract is a free, open-source Python tool by Google. It uses powerful AI (especially their Gemini language models) to read unstructured, natural text and turn it into structured data—meaning neat, organized information you can search, analyze, or visualize easily. And it doesn’t just guess: it even highlights exactly where each piece of data came from, so you can verify everything with just a glance.

1. What Is LangExtract?

LangExtract is an intelligent library designed for developers and analysts to transform unstructured text into structured, machine-friendly data. Whether you're processing clinical notes, legal documents, or articles, LangExtract makes it simple to define what you want to extract and then delivers clean outputs tied directly to the source text.

2. Why It Matters: Main Features

Precise Source Grounding

Every piece of data it extracts is linked to its exact location in the original text. This means you can verify where each fact came from—super important in sensitive domains like healthcare or compliance.

Consistent Structured Output

You guide the tool with simple natural-language instructions and a few examples (“few-shot prompting”). LangExtract then keeps the output consistent and organized.

Smart Handling of Long Documents

For long texts (even those that are “a needle in a haystack”), it splits the text smartly, runs multiple passes, and processes chunks in parallel for better coverage and accuracy.

Interactive Visualization

It can generate an HTML report with highlights and navigation tools, letting you visually explore thousands of extractions in context.

Works with Many Models

You can use Google’s Gemini models in the cloud or run local models via platforms like Ollama—LangExtract adapts to your preference.

Domain-Independent & No Retraining Needed

Just provide a few examples—whether you’re dealing with literature, medical text, or business docs—and LangExtract learns what to extract on the fly.

3. What’s New or Exciting?

4. How to Use LangExtract: Beginner Steps

1. Install the library

Bash
pip install langextract

2. Sign up or log in to Google AI Studio.

3. Generate a Gemini API key.

4. Save this key in a .env file or export it directly:

Python
export LANGEXTRACT_API_KEY="your_api_key_here"

5. Create extract_demo.py, write a simple script based on the official documentation:

Python
from dotenv import load_dotenv
load_dotenv()
import textwrap
import langextract as lx

prompt = textwrap.dedent("""
  Extract characters and emotions from the text.
  Use exact text for extractions. Do not paraphrase or overlap entities.
""")

examples = [
    lx.data.ExampleData(
        text="ROMEO: But soft! What light through yonder window breaks?",
        extractions=[
            lx.data.Extraction("character", "ROMEO"),
            lx.data.Extraction("emotion", "But soft!")
        ]
    )
]

input_text = "Juliet gazed at the night sky, her heart aching for Romeo."

result = lx.extract(
    text_or_documents=input_text,
    prompt_description=prompt,
    examples=examples,
    model_id="gemini-2.5-flash"
)

lx.io.save_annotated_documents([result], output_name="results.jsonl")
html = lx.visualize("test_output/results.jsonl")
with open("visualization.html", "w", encoding="utf-8") as f:
    f.write(html)
print("Extraction complete. Open visualization.html to see results")

This script

  1. Defines what to extract (characters, emotions).
  2. Runs extraction.
  3. Saves the results into results.jsonl.
  4. Converts the results into an interactive visualization.html file.

6. Run the Script

Bash
python extract_demo.py

You’ll get results.jsonl and an interactive visualization.html—just open the HTML file in your browser to explore the highlighted extractions.

5. Who Would Benefit Most?

Summary

LangExtract is your smart assistant for transforming messy, unstructured text into useful, structured insights—all with verifiable traceability and flexible workflows. No retraining needed, just a prompt and examples, and you're off to the races!