Contextgem
Structured document extraction with LLMs
Replaces Reducto

Who it's for
Extract payment terms, termination clauses, notice periods, and governing law from contracts. Each extracted clause maps back to its source paragraph, and decision models can classify clause types at high volume for a fraction of the LLM cost.
Embed ContextGem as a tool inside an agent that ingests documents, routes questions to the right model, and passes typed, validated results to downstream steps. The serializable pipeline makes it easy to reuse extraction logic across document sets.
User needs a general-purpose agent framework rather than document-focused extraction.
Use decision models to classify thousands of documents by type, risk level, or label at low cost and high speed. Each answer comes with a probability score, and you set the threshold at which a classification is accepted.
User only needs open-ended extraction rather than closed-question classification.
Extract specific facts, numbers, dates, and entities from research papers, financial reports, or technical documents. Concepts target the whole document while aspects focus extraction on relevant sections, so values spread across multiple sections are not missed.
The problem
Reliable extraction from documents requires writing prompts, designing validation schemas, mapping outputs back to source text, orchestrating multi-step pipelines, and tracking usage across models. Doing this from scratch for each project is repetitive and error-prone, and retrieval-based approaches miss values that span multiple sections or require reasoning across the whole document.
How it works
Prompts are generated automatically from your plain-language descriptions of what to extract. You describe the aspect or concept; ContextGem handles prompt construction.
Extraction results are validated into typed items: text strings, numbers, dates, booleans, labels, ratings, and JSON objects. Every item is checked before it reaches your code.
Each extracted item maps back to the paragraph or sentence it came from. Concepts can also return a justification explaining why the value was extracted, at brief or detailed depth.
Boolean, label, and rating concepts can be routed to a decision model, which returns the probability of each possible answer without generating text. You set a decision threshold per model or per concept.
Aspects identify specific document sections (clauses, chapters, terms) and focus downstream concept extraction on the relevant text. Sub-aspects are supported for finer nesting.
Documents can be loaded from PDF, DOCX, PPTX, XLSX, CSV, EPUB, email, images, audio, video, HTML, and Markdown via Docling. Each paragraph retains its position in the file structure and the pages it spans.
Features
- No prompts to write
- Typed, validated output
- Source references and justifications
- Decision models for closed questions
- Aspects and nested context
- Docling file conversion
How to install
pip install -U contextgem # with Docling for PDF, DOCX and other file formats: pip install -U "contextgem[docling]"
Recommended for this setup
Strengths
- Declarative pipeline that is reusable and serializable: A pipeline defines aspects and concepts once and can be applied to many documents, serialized to storage, and deserialized for later use, without rebuilding the extraction logic each time.
- LLM-agnostic via LiteLLM: Any cloud or local LLM accessible through LiteLLM works out of the box: OpenAI, Anthropic, Google, Azure OpenAI, xAI, Ollama, LM Studio, and more. Decision models include TypeSafe Jev, OpenAI Decisions API, and Microsoft Foundry.
- Built-in cost and model routing: A model group routes each aspect and concept to the model that suits it — LLM for open-ended values, decision model for closed questions — and any model can fall back on others. This keeps costs down without changing how you describe extraction targets.
- Agentic workflow integration: ContextGem is designed to work inside agentic systems: at document ingestion, as a tool an agent calls, or at decision points in a workflow. The serializable pipeline fits naturally into pipelines that process documents at scale.
Trade-offs
- Python 3.10 no longer supported: Version 1.0 dropped Python 3.10 support. Projects on 3.10 must upgrade their Python version before migrating from the 0.x series.
- Smaller local LLMs may not follow extraction instructions: The framework is tested with hosted models. A smaller LLM that runs locally may not follow the detailed extraction instructions reliably; the documentation recommends checking on a sample of your documents and consulting the troubleshooting guide.
- Decision models are limited to closed questions: Decision models extract only Boolean, label, and rating concepts, and aspects, from text. Open-ended values — strings, numbers, dates, JSON objects, or image-based extraction — require an LLM.
- Breaking changes from 0.x to 1.0: Some classes and parameters were renamed or removed in 1.0, and some defaults changed. Projects upgrading from the 0.x series must follow the migration guide before adopting the new release.
Tech stack
FAQ
Any LLM accessible through LiteLLM works, including OpenAI, Anthropic, Google, Azure OpenAI, xAI, Ollama, and LM Studio. Decision models include TypeSafe Jev, the OpenAI Decisions API, and Microsoft Foundry, cloud or local.
Via the Docling integration, ContextGem converts PDF, DOCX, DOC, RTF, ODT, Pages, PPTX, PPT, ODP, Keynote, XLSX, XLS, ODS, Numbers, CSV, EPUB, email, Box Notes, images, audio, video, WebVTT, HTML, MHTML, and Markdown into documents ready for extraction.
An aspect identifies and returns a section of the document itself — for example, the paragraphs that form the payment terms clause. A concept extracts or infers a new typed value from the document or from within an aspect — for example, the exact payment period as a number, or whether termination for convenience is permitted as a boolean.
Use a decision model for closed questions — yes/no, one of a fixed set of labels, or a level of a scale — especially in volume, where speed and cost matter. Use an LLM for open-ended values, answers that require reasoning, or any extraction type a decision model does not support (strings, numbers, dates, JSON objects, image content). The documentation recommends comparing both on a sample of your documents before committing.
Yes. Pipelines are reusable and serializable. You define aspects and concepts once, serialize the pipeline to storage, and deserialize it to process new documents without rebuilding the extraction logic.