google/langextract
Python SDK for extracting structured data with traceable source spans
Automated SPDX check for reference only — not legal advice. Disclaimer
GitHub Star History
Full-history star trajectory from GitHub official history. Switch ranges or hover any point for exact counts.
Evidence-Backed Technical Brief
AI Deep ReviewArchitecture & Core Mechanics
LangExtract is an in-process Python SDK, distributed through PyPI and built with setuptools and wheel for Python 3.10+. Its main entry point, lx.extract, accepts text or documents and a task description. Few-shot ExampleData records pair sample text with typed Extraction records. The pipeline can split long inputs, dispatch inference across chunks and passes, and align returned excerpts to character intervals in the source. lx.io.save_annotated_documents and lx.visualize handle persistence and review separately from inference.
The provider router can select a backend by model ID; ModelConfig lets callers specify one explicitly, including for OpenAI-compatible endpoints with nonstandard model IDs. External providers can register with @router.register(...), publish a Python entry point, and optionally implement get_schema_class(). The factory uses provider-specific formatting and schema behavior rather than assuming every backend exposes the same controls.
Key Capabilities & Differentiators
Extraction records contain a class, source excerpt, and optional attributes. When an excerpt aligns, char_interval lets a reviewer find it in context; when it does not, the interval is None, so the application can identify unsupported excerpts. Annotated documents serialize to JSONL, and lx.visualize turns those records into self-contained, interactive HTML. Gemini and OpenAI models that support structured outputs can use a custom output_schema, including attribute enums, with or without examples. Ollama supports JSON mode but not output_schema.
For long documents, max_char_buffer controls chunk size, max_workers controls concurrency, and extraction_passes repeats extraction to improve recall. Inputs can also be fetched from URLs when fetch_urls=True. Batch inference is available through Vertex AI Batch and through opt-in OpenAI Batch, where requests below the configured prompt threshold fall back to realtime calls. These paths suit work that can wait for a batch job rather than an interactive request.
Trade-offs & Limitations
Compared with a direct model SDK, LangExtract saves work on source offsets, long-document processing, and human review. If the job only needs schema-valid JSON from short inputs, a direct SDK call has fewer moving parts. If the source follows stable templates or exact rules, a deterministic parser is easier to reproduce and test. LangExtract works well before indexing in a RAG pipeline, but it does not replace a retriever or vector store.
A matched span shows where the excerpt came from; it does not prove the class or inferred attributes are correct. Schema constraints govern output shape, not factual accuracy or recall. Evaluate field accuracy and span alignment separately, especially for repeated mentions and facts that depend on context across chunks. Additional passes increase inference work, while high concurrency can hit cloud quotas or overload a local model host. Local inference gives you more control over where text is processed, but your team owns capacity planning and model evaluation.
Hardware & Deployment Guide
Install langextract on Python 3.10+; the OpenAI backend requires langextract[openai]. The supplied Dockerfile uses python:3.10-slim, installs the package from PyPI without a version pin, and starts python by default. It is a script runtime, not a ready-made API server. For repeatable builds, pin the package and include or mount your application script. Configure cloud credentials at runtime; Vertex AI supports service-account authentication. For local inference, point the SDK at a running Ollama instance with the chosen model available and make that endpoint reachable from the container if applicable.
Size API workers for document size and concurrent requests; size a local model host separately for inference. Version 1.7.0 revised alignment for repeated mentions and kept an option for the previous algorithm. Version 1.7.1 improved reporting of blocked or failed Gemini batch items. Version 1.7.2 added google-genai 3.0 compatibility to restore transient network retries and escaped data embedded in visualization HTML. Pin and test upgrades against alignment fixtures and representative review artifacts.
char_interval links matched extraction text to source offsets; unmatched extractions carry None.output_schema; Ollama offers JSON mode but not this custom schema interface.max_char_buffer, max_workers, and extraction_passes tune chunk size, concurrency, and repeat extraction independently.@router.register(...), publish Python entry points, and optionally supply a schema class.Python ingestion and RAG preprocessing pipelines that need structured fields from long or varied documents, traceable excerpts, and a human review path.
You need turnkey OCR, retrieval, or an API service; your inputs are reliably rule-parseable; or every inferred field must be correct without validation.
Quick Start & Installation
pip install langextractComparison vs. Top RAG, Data & Memory Alternatives
Full comparison →| Repository | Stars | 7d Trend | Stack | License | Core Positioning |
|---|---|---|---|---|---|
| langextract ★ | 39.5k | +85 | Python | Apache-2.0 | Python SDK for extracting structured data with traceable source spans |
| haystack | 27.2k | +53 | Python | Apache-2.0 | Component-based Python orchestration for RAG pipelines and tool-using agents |
| auto-ontology | 19.1k | +3.7k | Python | Apache-2.0 | An ontology framework that grounds agent-generated SQL in reviewed business defi |
| llama_index | 53.1k | +104 | Python | MIT | A modular Python SDK for document ingestion, indexing, and RAG |
AI Context Briefing (for Claude / Cursor / Windsurf)
Standardized architecture summary & quickstart commands ready to paste into coding agents.
curl -sL https://repoforai.com/repo/google/langextract.md
Active README embeds unlock Hot-Tier 6h Telemetry Refresh automatically (zero pay-to-rank).
Frequently Asked Questions about langextract
How should I integrate LangExtract into a document ingestion pipeline?
lx.extract with source text or documents and a task description. Use representative ExampleData examples with verbatim spans in source order, or supply an output_schema on a supported backend. Route records with char_interval = None to review or exclude them from evidence-backed fields. Store the source document identifier with the offsets so downstream consumers can recover the evidence.Do I need a GPU, and how should I size workers?
Can we use it commercially and keep sensitive documents on our own infrastructure?
When is it preferable to a direct model SDK or a deterministic parser?
Upstream README & Documentation
This profile focuses on independent engineering analysis and live telemetry. Read the full upstream README, changelog, and examples directly on GitHub.
Top 10 Breakout Open-Source AI Repos, Every Thursday
Signal-only digest of fast-growing AI repos, SPDX license alerts, and architecture picks. Zero spam.
Zero spam · 1-click unsubscribe · Privacy Policy