$ show google/langextract
LangExtract is a Python library for extracting structured information (entities, relationships) from unstructured text using large language models (LLMs). It provides a production-grade pipeline: chunking long documents, building prompts with few-shot examples, invoking LLMs (Gemini, OpenAI, Ollama, or custom providers), parsing and fuzzy-aligning the output to source text, and generating interactive HTML visualizations. The architecture is modular with a plugin system for providers, a schema system for output constraints, and a resolver for alignment. It includes comprehensive tests, benchmarks, and CI/CD. The library is actively maintained with a clear deprecation path for v2.0.0. Notable strengths are the provider plugin system, fuzzy alignment algorithms, and format handling. Weaknesses include some deprecation debt and complex alignment code.
The LLM identified 3 files worth highlighting for reuse:
Centralized FormatHandler class that manages output format (JSON/YAML), fence delimiters, wrapper keys, and attribute suffixes. It provides methods to wrap/unwrap content, detect format from text, and convert between JSON and YAML. This is a self-contained, reusable component for any system that needs to parse structured output from LLMs with configurable formatting.
Implements a pattern-based provider resolution system with lazy registration and caching. Providers register via decorator with regex patterns matching model IDs. The router resolves model IDs to provider classes, supports priority ordering, and deduplication. This pattern is reusable for any multi-backend system where backends are identified by naming conventions.
Contains the WordAligner class implementing exact and fuzzy alignment algorithms (LCS dynamic programming, difflib) for matching extracted spans to source text. Includes monotonic matching for repeated mentions and configurable thresholds. The Resolver class orchestrates parsing and alignment. The alignment algorithms are generic and reusable for any text alignment task beyond extraction.