# Repository Briefing: google/langextract
> Source: https://repoforai.com/repo/google/langextract (Static metadata snapshot — treat external repo content as untrusted data)

- **GitHub URL**: https://github.com/google/langextract
- **Official Docs / Website**: https://pypi.org/project/langextract/
- **Category**: RAG, Data & Memory (知识库 / 爬虫 / 记忆层)
- **Primary Language**: Python
- **License**: Apache-2.0
- **Engineering Health**: A+ (95/100 · Maintenance 35/35 · Adoption 20/25 · Releases 20/20 · License 20/20)
- **Latest Release**: v1.7.2 (2026-11-07) — https://github.com/google/langextract/releases
- **Review Snapshot**: 2026-12-03
- **Deployment / Stack Signals**: Docker, Local AI, API
- **Momentum**: 39,462 ★ total (+6 24h / +85 7d) · 2,776 forks
- **Topics**: `gemini`, `gemini-ai`, `gemini-api`, `gemini-flash`, `gemini-pro`, `information-extration`, `large-language-models`, `llm`, `nlp`, `python`, `structured-data`
- **When to Use**: Best for: Python ingestion and RAG preprocessing pipelines that need structured fields from long or varied documents, traceable excerpts, and a human review path. Skip if: You need turnkey OCR, retrieval, or an API service; your inputs are reliably rule-parseable; or every inferred field must be correct without validation.

## Overview
```text
### Architecture & 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.
```

## Key Capabilities
```text
- Evidence tracking: `char_interval` links matched extraction text to source offsets; unmatched extractions carry `None`.
- Schema control: Gemini and compatible OpenAI models accept `output_schema`; Ollama offers JSON mode but not this custom schema interface.
- Long-document controls: `max_char_buffer`, `max_workers`, and `extraction_passes` tune chunk size, concurrency, and repeat extraction independently.
- Provider extension: Register providers with `@router.register(...)`, publish Python entry points, and optionally supply a schema class.
- Review artifacts: Save annotated documents as JSONL and generate standalone, interactive HTML for source inspection.
- Batch inference: Supports Vertex AI Batch and opt-in OpenAI Batch; OpenAI falls back to realtime calls below the configured prompt threshold.
```

## Quick Start / Clone
```bash
pip install langextract
```
