LangExtract: a Python library for extracting structured information
LangExtract is a Python library that uses LLMs to extract structured information from unstructured text documents based on user-defined instructions.
What it is · Main features · Getting started
What it is
LangExtract is a Python library that uses LLMs to extract structured information from unstructured text documents based on user-defined instructions. The project says LangExtract processes materials such as clinical notes or reports, identifying and organizing key details while ensuring the extracted data corresponds to the source text. The project says it supports cloud-based LLMs like the Google Gemini family, and local open-source models via the built-in Ollama interface.
Core facts
Read by a script from the official API, not rewritten by a model.
- Repository
- https://github.com/google/langextract
- License
- Apache-2.0
- Languages
- Python
- Maintainer
- Verified
Main features
The project says LangExtract maps extractions to their exact location in the source text, but extractions that cannot be located in the source text have char_interval = None.
According to the project, it enforces a consistent output schema based on the user's few-shot examples.
The project says it generates a self-contained, interactive HTML file for reviewing extracted entities in their original context.
Extractions can be saved to a JSONL file.
LangExtract can then generate an interactive HTML visualization from that file to review the entities in context.
LangExtract supports custom LLM providers via a plugin system.
For larger texts, the project says LangExtract can process entire documents directly from URLs with parallel processing and enhanced sensitivity.
The project says the balance between text evidence and knowledge inference is controlled by the prompt instructions and example attributes.
Getting started
Using cloud-hosted models like Gemini requires an API key.
On-device models do not require an API key.
LangExtract supports local inference using Ollama, allowing models to run without API keys.
The Ollama quick setup is to install Ollama, pull the gemma2:2b model, then run serve.
Ollama does not currently support output_schema.
To define an extraction task, create a prompt that describes what to extract, then provide an example to guide the model.
Examples drive model behavior.
Each extraction_text should ideally be verbatim from the example text, with no paraphrasing, listed in order of appearance.
By default, LangExtract raises Prompt alignment warnings if examples do not follow this pattern.
The extract function receives the input text and prompt materials.
The project recommends gemini-3.5-flash as the default model.
In the documented example, the extraction takes "longing" for Lady Juliet's emotional state and identifies "yearning" from "gazed longingly at the stars."
LLMs may occasionally extract content from few-shot examples rather than the input text.
Extractions that cannot be located in the source text have char_interval = None.
Filtering those out keeps only grounded results.
LangExtract supports OpenAI models, which require an optional dependency.
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 exact text for extractions. Do not paraphrase or overlap entities.
Provide meaningful attributes for each entity to add context.""")
# 2. Provide a high-quality example to guide the model
examples = [
lx.data.ExampleData(
text="ROMEO. But soft! What light through yonder window breaks? It is the east, and Juliet is the sun.",
extractions=[
lx.data.Extraction(
extraction_class="character",
extraction_text="ROMEO",
attributes={"emotional_state": "wonder"}
),
lx.data.Extraction(
extraction_class="emotion",
extraction_text="But soft!",
attributes={"feeling": "gentle awe"}
),
lx.data.Extraction(
extraction_class="relationship",
extraction_text="Juliet is the sun",
attributes={"type": "metaphor"}
),
]
)
]# The input text to be processed
input_text = "Lady Juliet gazed longingly at the stars, her heart aching for Romeo"
# Run the extraction
result = lx.extract(
text_or_documents=input_text,
prompt_description=prompt,
examples=examples,
model_id="gemini-3.5-flash",
)# Save the results to a JSONL file
lx.io.save_annotated_documents([result], output_name="extraction_results.jsonl", output_dir=".")
# Generate the visualization from the file
html_content = lx.visualize("extraction_results.jsonl")
with open("visualization.html", "w", encoding="utf-8") as f:
if hasattr(html_content, 'data'):
f.write(html_content.data) # For Jupyter/Colab
else:
f.write(html_content)pip install langextractexport LANGEXTRACT_API_KEY="your-api-key-here"import langextract as lx
result = lx.extract(
text_or_documents=input_text,
prompt_description=prompt,
examples=examples,
model_id="gemma2:2b", # Automatically selects Ollama provider
model_url="http://localhost:11434",
)Who it is for
LangExtract is not an officially supported Google product. Use of LangExtract is subject to the Apache 2.0 License.
Sources
This page is written from the official sources below. Open them to check the original text.
- GitHub READMEhttps://github.com/google/langextractFetched
Verified ·
This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.