# google/langextract architecture

> Architecture diagram of the GitHub repository google/langextract, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/google/langextract
- Repository: https://github.com/google/langextract

## Overview

LangExtract is a Python library for extracting structured, source-grounded information from unstructured documents. The principal workflow accepts text and user-defined examples/instructions, chunks documents, prompts an LLM, parses and resolves its output to source spans, and returns annotated documents; results can then be saved or visualized. Provider selection, schema-constrained output, multi-pass extraction, and progress reporting support that workflow. The graph combines README capabilities with indexed module references and sampled excerpts; unsampled internals and some provider wiring are not asserted beyond documented behavior.

Main entry points: [`langextract/extraction.py`](https://github.com/google/langextract/blob/main/langextract/extraction.py) (Extract API), [`langextract/annotation.py`](https://github.com/google/langextract/blob/main/langextract/annotation.py) (Document annotator), [`langextract/factory.py`](https://github.com/google/langextract/blob/main/langextract/factory.py) (Model factory), [`langextract/resolver.py`](https://github.com/google/langextract/blob/main/langextract/resolver.py) (Output grounding)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_workflow["Extraction workflow"]
  node_extract["Extract API<br/>[extraction.py]"]
  node_annotate["Document annotator<br/>[annotation.py]"]
  node_chunks["Chunking<br/>[chunking.py]"]
  node_prompt["Prompting<br/>[prompting.py]"]
  node_progress["Progress tracking<br/>[progress.py]"]
end

subgraph group_core["Grounding and schemas"]
  node_data["Document and extraction data<br/>[data.py]"]
  node_tokenizer["Tokenization<br/>[tokenizer.py]"]
  node_resolver["Output grounding<br/>[resolver.py]"]
  node_format["Output parsing<br/>[format_handler.py]"]
  node_schema["Schema framework<br/>[schema.py]"]
  node_output_schema["User output schemas<br/>[output_schema.py]"]
  node_exceptions["Core exceptions<br/>[exceptions.py]"]
  node_debug["Debug utilities<br/>[debug_utils.py]"]
end

subgraph group_models["Model providers"]
  node_base_model["Model interface<br/>[base_model.py]"]
  node_factory["Model factory<br/>[factory.py]"]
  node_router["Provider router<br/>[router.py]"]
  node_patterns["Model ID patterns<br/>[patterns.py]"]
  node_gemini["Gemini provider<br/>[gemini.py]"]
  node_gemini_batch["Gemini batch inference<br/>[gemini_batch.py]"]
  node_openai["OpenAI provider<br/>[openai.py]"]
  node_ollama["Ollama provider<br/>[ollama.py]"]
  node_openai_schema["OpenAI schema adapter<br/>[openai.py]"]
end

subgraph group_results["Results and review"]
  node_io["Document I/O<br/>[io.py]"]
  node_visualization["Interactive visualization<br/>[visualization.py]"]
  node_output["Annotated results<br/>[data.py]"]
end

node_user(("Caller"))
node_llm(("LLM service"))
node_jsonl["JSONL file"]
node_html["Interactive HTML"]

node_user -->|"requests extraction"| node_extract
node_extract -->|"delegates to"| node_annotate
node_extract -->|"creates model"| node_factory
node_extract -->|"builds prompts"| node_prompt
node_extract -->|"configures grounding"| node_resolver
node_annotate -->|"chunks documents"| node_chunks
node_annotate -->|"tracks batches"| node_progress
node_annotate -.->|"runs inference"| node_base_model
node_annotate -.->|"resolves outputs"| node_resolver
node_annotate -->|"emits annotations"| node_data
node_chunks -->|"uses tokens"| node_tokenizer
node_resolver -->|"parses with"| node_format
node_resolver -->|"aligns spans"| node_tokenizer
node_resolver -->|"creates extractions"| node_data
node_factory -->|"resolves provider"| node_router
node_factory -->|"uses patterns"| node_patterns
node_factory -.->|"loads provider"| node_gemini
node_factory -.->|"loads provider"| node_openai
node_factory -.->|"loads provider"| node_ollama
node_gemini -->|"supports batch"| node_gemini_batch
node_gemini -.->|"calls API"| node_llm
node_openai -.->|"calls API"| node_llm
node_ollama -.->|"calls service"| node_llm
node_base_model -->|"applies schemas"| node_schema
node_output_schema -->|"implements schema"| node_schema
node_openai -.->|"adapts schema"| node_openai_schema
node_format -->|"formats extractions"| node_data
node_tokenizer -->|"logs calls"| node_debug
node_base_model -->|"raises errors"| node_exceptions
node_extract -->|"saves results"| node_io
node_io -->|"serializes documents"| node_data
node_user -->|"requests review"| node_visualization
node_visualization -->|"loads results"| node_io
node_visualization -->|"generates HTML"| node_html
node_io -->|"writes JSONL"| node_jsonl
node_extract -->|"returns annotations"| node_output

click node_extract "https://github.com/google/langextract/blob/main/langextract/extraction.py"
click node_annotate "https://github.com/google/langextract/blob/main/langextract/annotation.py"
click node_chunks "https://github.com/google/langextract/blob/main/langextract/chunking.py"
click node_prompt "https://github.com/google/langextract/blob/main/langextract/prompting.py"
click node_progress "https://github.com/google/langextract/blob/main/langextract/progress.py"
click node_data "https://github.com/google/langextract/blob/main/langextract/core/data.py"
click node_tokenizer "https://github.com/google/langextract/blob/main/langextract/core/tokenizer.py"
click node_resolver "https://github.com/google/langextract/blob/main/langextract/resolver.py"
click node_format "https://github.com/google/langextract/blob/main/langextract/core/format_handler.py"
click node_schema "https://github.com/google/langextract/blob/main/langextract/core/schema.py"
click node_output_schema "https://github.com/google/langextract/blob/main/langextract/core/output_schema.py"
click node_base_model "https://github.com/google/langextract/blob/main/langextract/core/base_model.py"
click node_factory "https://github.com/google/langextract/blob/main/langextract/factory.py"
click node_router "https://github.com/google/langextract/blob/main/langextract/providers/router.py"
click node_patterns "https://github.com/google/langextract/blob/main/langextract/providers/patterns.py"
click node_gemini "https://github.com/google/langextract/blob/main/langextract/providers/gemini.py"
click node_gemini_batch "https://github.com/google/langextract/blob/main/langextract/providers/gemini_batch.py"
click node_openai "https://github.com/google/langextract/blob/main/langextract/providers/openai.py"
click node_ollama "https://github.com/google/langextract/blob/main/langextract/providers/ollama.py"
click node_openai_schema "https://github.com/google/langextract/blob/main/langextract/providers/schemas/openai.py"
click node_exceptions "https://github.com/google/langextract/blob/main/langextract/core/exceptions.py"
click node_debug "https://github.com/google/langextract/blob/main/langextract/core/debug_utils.py"
click node_io "https://github.com/google/langextract/blob/main/langextract/io.py"
click node_visualization "https://github.com/google/langextract/blob/main/langextract/visualization.py"
click node_output "https://github.com/google/langextract/blob/main/langextract/core/data.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_extract,node_annotate,node_chunks,node_prompt,node_progress toneBlue
class node_data,node_tokenizer,node_resolver,node_format,node_schema,node_output_schema,node_exceptions,node_debug toneAmber
class node_base_model,node_factory,node_router,node_patterns,node_gemini,node_gemini_batch,node_openai,node_ollama,node_openai_schema toneMint
class node_io,node_visualization,node_output toneRose
class node_user,node_llm,node_jsonl,node_html toneIndigo
```

## Components

### Extraction workflow

- [Extract API](https://github.com/google/langextract/blob/main/langextract/extraction.py): `langextract/extraction.py`
- [Document annotator](https://github.com/google/langextract/blob/main/langextract/annotation.py): `langextract/annotation.py`
- [Chunking](https://github.com/google/langextract/blob/main/langextract/chunking.py): `langextract/chunking.py`
- [Prompting](https://github.com/google/langextract/blob/main/langextract/prompting.py): `langextract/prompting.py`
- [Progress tracking](https://github.com/google/langextract/blob/main/langextract/progress.py): `langextract/progress.py`

### Grounding and schemas

- [Document and extraction data](https://github.com/google/langextract/blob/main/langextract/core/data.py): `langextract/core/data.py`
- [Tokenization](https://github.com/google/langextract/blob/main/langextract/core/tokenizer.py): `langextract/core/tokenizer.py`
- [Output grounding](https://github.com/google/langextract/blob/main/langextract/resolver.py): `langextract/resolver.py`
- [Output parsing](https://github.com/google/langextract/blob/main/langextract/core/format_handler.py): `langextract/core/format_handler.py`
- [Schema framework](https://github.com/google/langextract/blob/main/langextract/core/schema.py): `langextract/core/schema.py`
- [User output schemas](https://github.com/google/langextract/blob/main/langextract/core/output_schema.py): `langextract/core/output_schema.py`
- [Core exceptions](https://github.com/google/langextract/blob/main/langextract/core/exceptions.py): `langextract/core/exceptions.py`
- [Debug utilities](https://github.com/google/langextract/blob/main/langextract/core/debug_utils.py): `langextract/core/debug_utils.py`

### Model providers

- [Model interface](https://github.com/google/langextract/blob/main/langextract/core/base_model.py): `langextract/core/base_model.py`
- [Model factory](https://github.com/google/langextract/blob/main/langextract/factory.py): `langextract/factory.py`
- [Provider router](https://github.com/google/langextract/blob/main/langextract/providers/router.py): `langextract/providers/router.py`
- [Model ID patterns](https://github.com/google/langextract/blob/main/langextract/providers/patterns.py): `langextract/providers/patterns.py`
- [Gemini provider](https://github.com/google/langextract/blob/main/langextract/providers/gemini.py): `langextract/providers/gemini.py`
- [Gemini batch inference](https://github.com/google/langextract/blob/main/langextract/providers/gemini_batch.py): `langextract/providers/gemini_batch.py`
- [OpenAI provider](https://github.com/google/langextract/blob/main/langextract/providers/openai.py): `langextract/providers/openai.py`
- [Ollama provider](https://github.com/google/langextract/blob/main/langextract/providers/ollama.py): `langextract/providers/ollama.py`
- [OpenAI schema adapter](https://github.com/google/langextract/blob/main/langextract/providers/schemas/openai.py): `langextract/providers/schemas/openai.py`

### Results and review

- [Document I/O](https://github.com/google/langextract/blob/main/langextract/io.py): `langextract/io.py`
- [Interactive visualization](https://github.com/google/langextract/blob/main/langextract/visualization.py): `langextract/visualization.py`
- [Annotated results](https://github.com/google/langextract/blob/main/langextract/core/data.py): `langextract/core/data.py`

### Other components

- **Caller**
- **LLM service**
- **JSONL file**
- **Interactive HTML**

## Connections

- Caller → Extract API: requests extraction
- Extract API → Document annotator: delegates to (evidence: `langextract/extraction.py`)
- Extract API → Model factory: creates model (evidence: `langextract/extraction.py`)
- Extract API → Prompting: builds prompts (evidence: `langextract/extraction.py`)
- Extract API → Output grounding: configures grounding (evidence: `langextract/extraction.py`)
- Document annotator → Chunking: chunks documents (evidence: `langextract/annotation.py`)
- Document annotator → Progress tracking: tracks batches (evidence: `langextract/annotation.py`)
- Document annotator → Model interface: runs inference (evidence: `langextract/annotation.py`)
- Document annotator → Output grounding: resolves outputs (evidence: `langextract/annotation.py`)
- Document annotator → Document and extraction data: emits annotations (evidence: `langextract/annotation.py`)
- Chunking → Tokenization: uses tokens (evidence: `langextract/chunking.py`)
- Output grounding → Output parsing: parses with (evidence: `langextract/resolver.py`)
- Output grounding → Tokenization: aligns spans (evidence: `langextract/resolver.py`)
- Output grounding → Document and extraction data: creates extractions (evidence: `langextract/resolver.py`)
- Model factory → Provider router: resolves provider (evidence: `langextract/factory.py`)
- Model factory → Model ID patterns: uses patterns (evidence: `langextract/factory.py`)
- Model factory → Gemini provider: loads provider
- Model factory → OpenAI provider: loads provider
- Model factory → Ollama provider: loads provider
- Gemini provider → Gemini batch inference: supports batch (evidence: `langextract/providers/gemini.py`)
- Gemini provider → LLM service: calls API
- OpenAI provider → LLM service: calls API
- Ollama provider → LLM service: calls service
- Model interface → Schema framework: applies schemas (evidence: `langextract/core/base_model.py`)
- User output schemas → Schema framework: implements schema (evidence: `langextract/core/output_schema.py`)
- OpenAI provider → OpenAI schema adapter: adapts schema
- Output parsing → Document and extraction data: formats extractions (evidence: `langextract/core/format_handler.py`)
- Tokenization → Debug utilities: logs calls (evidence: `langextract/core/tokenizer.py`)
- Model interface → Core exceptions: raises errors (evidence: `langextract/core/base_model.py`)
- Extract API → Document I/O: saves results (evidence: `langextract/extraction.py`)
- Document I/O → Document and extraction data: serializes documents (evidence: `langextract/io.py`)
- Caller → Interactive visualization: requests review
- Interactive visualization → Document I/O: loads results (evidence: `langextract/visualization.py`)
- Interactive visualization → Interactive HTML: generates HTML
- Document I/O → JSONL file: writes JSONL
- Extract API → Annotated results: returns annotations (evidence: `langextract/extraction.py`)
