google/langextract

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

LangExtract – Structured Extraction with LLMs

What it does – LangExtract is a Python library that lets you pull out structured information (entities, attributes, relationships) from any un‑structured text — clinical notes, literary works, reports, etc. It does this by sending the text to a large language model (LLM) with a few‑shot prompt you write, then it grounds every returned piece of data back to the exact character span in the original document. The library also builds an interactive HTML visualiser so you can see each extraction highlighted in context.

Why it matters –

  • Grounded results: each extraction is linked to its source location, making verification easy and supporting downstream pipelines that need traceability (e.g., medical audit).
  • Works on long documents: the text is automatically chunked, processed in parallel, and can run multiple passes to improve recall.
  • Model‑agnostic: supports Google Gemini (cloud), OpenAI models, and local models via Ollama; you can also add custom providers via a plugin system.
  • No fine‑tuning needed: you only supply a prompt and a few examples; the LLM’s world knowledge is leveraged through prompt engineering.

How to get started –

  1. Install with pip install langextract (or from source/Docker).
  2. Write a short prompt describing the extraction task and provide one or more ExampleData objects that show the desired output format.
  3. Call lx.extract(text_or_documents, prompt_description, examples, model_id=…).
  4. Save the result to a JSONL file with lx.io.save_annotated_documents and generate a visual HTML file via lx.visualize.

Typical workflow snippet

import langextract as lx, textwrap

prompt = "Extract characters, emotions, and relationships in order of appearance. Use exact text."
examples = [
    lx.data.ExampleData(
        text="ROMEO. But soft! …",
        extractions=[
            lx.data.Extraction("character", "ROMEO", {"emotional_state": "wonder"}),
            lx.data.Extraction("emotion", "But soft!", {"feeling": "gentle awe"}),
        ],
    )
]

result = lx.extract(
    text_or_documents="Lady Juliet gazed longingly at the stars…",
    prompt_description=prompt,
    examples=examples,
    model_id="gemini-3.5-flash",
)

lx.io.save_annotated_documents([result], "extraction_results.jsonl")
html = lx.visualize("extraction_results.jsonl")
open("visualization.html", "w").write(html.data)

Supported back‑ends –

  • Google Gemini (cloud, requires LANGEXTRACT_API_KEY).
  • OpenAI (gpt‑4o, gpt‑4o‑mini, etc.; optional langextract[openai]).
  • Ollama for local models such as gemma2:2b (no API key needed).
  • Custom providers can be added as separate packages via a lightweight plugin registry.

Scalability features –

  • Parallel chunk processing (max_workers).
  • Multiple extraction passes (extraction_passes).
  • Vertex AI batch API integration for cost‑effective large‑scale jobs.

Where to learn more –

  • Live demo: https://google‑langextract.hf.space/ (Romeo & Juliet extraction in the browser).
  • Full docs in the docs/ folder, including schema usage, batch API, and provider plugin guide.
  • Example notebooks for long‑text, medication extraction, and radiology report structuring (RadExtract).

Community & contribution – The repo includes a list of community‑built model providers and welcomes pull requests. A Contributor License Agreement is required for contributions.


LangExtract is a genuine open‑source project aimed at LLM‑powered information extraction, fitting squarely within the AI/ML tooling space.

Related

  • Project
  • Project
  • Project
  • Project
  • Project