How to Optimize LangExtract Performance for Large-Scale Extraction
Optimize LangExtract for large-scale extraction by maximizing max_char_buffer near your model's context limit, setting batch_length equal to max_workers, using the fast RegexTokenizer, and disabling unnecessary features like URL fetching and multiple extraction passes.
When processing massive document collections with google/langextract, default configuration settings can create bottlenecks that throttle throughput and inflate API costs. To effectively optimize LangExtract performance for large-scale extraction, you must tune the chunking strategy, batching behavior, and parallelism settings while eliminating optional overhead from network calls and validation checks.
Understand the LangExtract Pipeline Architecture
LangExtract processes documents through a sequential pipeline defined in langextract/extraction.py. The extract() function orchestrates several stages that directly impact performance:
- Tokenization and Chunking (
langextract/chunking.py): Splits documents intoTextChunkobjects based onmax_char_buffer - Batching (
langextract/chunking.py): Groups chunks into batches viamake_batches_of_textchunk - Parallel Extraction (
langextract/extraction.py): Distributes batches across a thread pool sized bymax_workers - Annotation Merging (
langextract/annotation.py): Combines results across multipleextraction_passes
Each stage offers tuning parameters that trade off between API cost, latency, and accuracy.
Optimize Chunking to Minimize API Calls
The chunking stage is your primary lever for reducing the total number of expensive LLM invocations.
Maximize max_char_buffer
In langextract/chunking.py#L25-L40, the ChunkIterator splits documents when they exceed max_char_buffer. Setting this value too low generates excessive tiny chunks, while setting it near your model's context window (e.g., 12,000 characters for Gemini-Flash) minimizes API calls.
extractions = extraction.extract(
text_or_documents=large_corpus,
prompt_description="Extract entities",
config=model_cfg,
max_char_buffer=12000, # Near Gemini-Flash context limit
)
Select the Fast RegexTokenizer
LangExtract provides two tokenizers in langextract/core/tokenizer.py: RegexTokenizer (fast for ASCII/English) and UnicodeTokenizer (slower but better for multilingual text). For large-scale English document processing, explicitly use the regex implementation or leave tokenizer=None to use the default fast path.
from langextract.core.tokenizer import RegexTokenizer
tokenizer = RegexTokenizer() # Fast, lightweight
extractions = extraction.extract(
# ... other params
tokenizer=tokenizer,
)
Maximize Throughput with Batching and Parallelism
After optimizing chunk size, tune how chunks are grouped and processed concurrently.
Configure batch_length for Worker Alignment
In langextract/chunking.py#L65-L80, the make_batches_of_textchunk function groups chunks into batches sized by batch_length. To minimize request overhead, set batch_length approximately equal to max_workers, ensuring each parallel worker receives a full batch per iteration.
extractions = extraction.extract(
# ... other params
batch_length=12, # Match max_workers
max_workers=12, # Parallel threads
)
Tune max_workers to Provider Limits
The max_workers parameter in langextract/extraction.py#L48-L55 controls the thread pool size for concurrent API calls. Set this to your CPU core count or your LLM provider's rate limit quota—whichever is lower. For Google Gemini with a high quota, 12-16 workers typically saturates the API without local CPU contention.
Eliminate Performance Overhead
Disable optional features that add latency without benefit for bulk processing.
Disable Multiple Extraction Passes
The extraction_passes parameter in langextract/extraction.py#L71-L82 re-runs the pipeline to improve recall. Each additional pass multiplies your API costs linearly. For large-scale extraction, keep extraction_passes=1 unless you observe specific recall issues.
extractions = extraction.extract(
# ... other params
extraction_passes=1, # Single pass for maximum speed
)
Turn Off URL Fetching
If your input contains URLs, fetch_urls=True triggers network I/O for each document. Set fetch_urls=False and pre-download content using langextract.io.download_text_from_url to avoid blocking the extraction pipeline.
# Pre-fetch outside the hot path
from langextract.io import download_text_from_url
texts = [download_text_from_url(url) for url in url_list]
# Then extract with network disabled
extractions = extraction.extract(
text_or_documents=texts,
fetch_urls=False, # No network overhead
# ... other params
)
Disable Schema Constraints and Progress Bars
For maximum throughput, disable validation and UI overhead:
- Set
use_schema_constraints=Falseto skip extra prompt tokens for structured output validation - Set
fence_output=Falsewhen you don't need markdown code blocks in the response - Set
show_progress=Falseto suppress thetqdmprogress bar in headless environments
extractions = extraction.extract(
# ... other params
use_schema_constraints=False,
fence_output=False,
show_progress=False,
)
Production-Ready Code Examples
High-Throughput Configuration for Gemini-Flash
This configuration maximizes throughput for processing millions of documents with Google's Gemini-Flash model:
from langextract import extraction, factory, data
# Configure for Gemini-Flash's ~8k token context window
model_cfg = factory.ModelConfig(
model_id="gemini-2.5-flash",
provider_kwargs={
"api_key": "YOUR_API_KEY",
"max_workers": 12,
},
)
extractions = extraction.extract(
text_or_documents=large_corpus,
prompt_description="Extract all medication names and dosages.",
examples=[example1, example2],
config=model_cfg,
max_char_buffer=12000, # Near context limit
batch_length=12, # Match workers
max_workers=12, # Parallel inference
extraction_passes=1, # Single pass
fetch_urls=False, # No network I/O
show_progress=False, # Clean logs
)
Streaming Extraction for Low-Memory Processing
Process datasets larger than RAM using Python generators:
def stream_documents(file_path):
"""Yield Document objects one at a time to keep memory low."""
for line in open(file_path, "r"):
yield data.Document(text=line.strip(), document_id=None)
for result in extraction.extract(
text_or_documents=stream_documents("huge_corpus.txt"),
prompt_description="Extract entities.",
config=model_cfg,
max_char_buffer=8000,
batch_length=8,
max_workers=8,
extraction_passes=1,
fetch_urls=False,
show_progress=False,
):
handle(result) # Process immediately, don't accumulate
Disabling Schema Constraints for Raw JSON Output
When you don't need strict schema validation, disable constraints to reduce token usage:
extractions = extraction.extract(
text_or_documents=big_corpus,
prompt_description="List all ICD-10 codes.",
examples=[],
config=model_cfg,
max_char_buffer=12000,
batch_length=12,
max_workers=12,
use_schema_constraints=False, # Skip validation overhead
fence_output=False, # Expect raw JSON
)
Summary
To optimize LangExtract performance for large-scale extraction:
- Size chunks aggressively: Set
max_char_buffernear your model's context limit (e.g., 12,000 characters for Gemini-Flash) to minimize API calls, as implemented inlangextract/chunking.py. - Align batching with parallelism: Match
batch_lengthtomax_workers(e.g., 12 each) to ensure each thread processes full batches without idle time. - Use fast tokenization: Stick with the default
RegexTokenizerfor English text rather than the slowerUnicodeTokenizer. - Eliminate overhead: Set
extraction_passes=1,fetch_urls=False,show_progress=False, anduse_schema_constraints=Falseto disable redundant processing. - Stream for scale: Use Python generators for
text_or_documentsto process corpora larger than available RAM.
Frequently Asked Questions
What is the optimal max_char_buffer size for high-throughput extraction?
Set max_char_buffer to approximately 75% of your language model's token context window converted to characters. For Google Gemini-Flash with an 8,000-token limit, use 12,000 characters. This minimizes the number of chunks created in langextract/chunking.py while avoiding model rejection for overly long prompts.
How does batch_length interact with max_workers for parallel processing?
The batch_length parameter in langextract/chunking.py groups chunks into single API requests, while max_workers in langextract/extraction.py controls concurrent thread execution. For optimal throughput, set batch_length equal to max_workers (e.g., 12). This ensures each worker receives a full batch per iteration, eliminating idle time and minimizing request overhead.
Should I use RegexTokenizer or UnicodeTokenizer for bulk document processing?
Use RegexTokenizer for large-scale extraction of English or ASCII text. Located in langextract/core/tokenizer.py, it provides significantly faster performance than UnicodeTokenizer by using regex-based word boundaries rather than complex Unicode segmentation. Only switch to UnicodeTokenizer if processing multilingual documents where Unicode-aware tokenization is required for accurate entity boundaries.
When should I disable schema constraints during extraction?
Disable schema constraints by setting use_schema_constraints=False when you need maximum throughput and can tolerate raw JSON output without strict validation. This removes extra prompt tokens and processing overhead, particularly effective when combined with fence_output=False for unstructured responses. Enable constraints only when you require guaranteed schema adherence for downstream structured data pipelines.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →