Skip to main content

ContextIR

ContextIR is a local-first adaptive context compiler and privacy gateway for language models. It turns text into a compact, inspectable intermediate representation while retaining critical source fragments when a lossy summary would be unsafe.

Status: stable 1.0 API. ContextIR follows semantic versioning for the public Python surface and the contextir.v2 contract. Semantic compression and PII detection still require evaluation on each deployment's data.

Quick Start

python3 -m pip install contextir
ollama pull qwen3:0.6b
contextir run --model qwen3:0.6b --text "Reply with only READY."
from contextir import ContextPipeline, OllamaClient

pipeline = ContextPipeline(invoke=OllamaClient("qwen3:0.6b"))
result = pipeline.run(
    "If payment 42 is complete, do not send it again.",
    source_lang="en",
    target_lang="en",
)
print(result.answer)

For document QA, pass the question separately instead of embedding it into the document:

result = pipeline.run(
    long_document,
    context_kind="retrieval",
    query="What is the northern deployment credential?",
)

The document and query are masked together inside the trusted process. The masked query guides retrieval and is included in the model prompt and token budget, but is omitted from the public contract and safe trace.

The release wheel avoids a source checkout and Git dependency. A pinned Git install remains available for environments that prefer source builds.

For a two-minute integration and safe restoration example, see Quick start.

Why

Long agent histories and tool outputs consume model context with repeated facts, formatting, and sensitive values. ContextIR provides three explicit modes:

  • raw: masked source text, without protocol overhead;
  • hybrid: semantic events plus source fragments containing negation, conditions, numbers, quotes, or protected placeholders;
  • semantic: compact events only, for callers that accept lossy compression.

auto first distinguishes exhaustive tasks, document retrieval, and operational history. Exhaustive tasks stay raw. Document QA uses local lexical retrieval to keep the query, formatting instructions, and evidence spans verbatim. Operational history continues to use inspectable events and critical source fragments.

ContextPipeline is the product entry point. It measures candidate prompts with a configurable target-model tokenizer, rejects compression without useful savings, verifies output safety, and performs bounded fallback from semantic to hybrid to raw context. The bundled model clients also reserve output tokens and configurable chat-template overhead from their context window; ContextIR packs ranked retrieval evidence to the remaining budget or refuses an unsafe prompt before contacting the model.

For a query whose highest-ranked evidence segment is itself larger than the window, enable bounded chunked retrieval explicitly:

contextir run \
  --model qwen3:0.6b \
  --context-length 4096 \
  --context-kind retrieval \
  --query "What is the release code?" \
  --chunked-retrieval \
  --text "Read the document..."

This path chunks only retrieved evidence for reasoning tasks, selects relevant chunks locally, validates grounded map outputs, and performs at most one reduce call. It does not approximate exhaustive counting or transform tasks.

Install

From a checkout:

python3 -m pip install -e .

Optional Presidio integration:

python3 -m pip install -e '.[privacy]'

Advanced Compiler API

from contextir import ContextIR

gateway = ContextIR()
bundle = gateway.compile_private(
    "If payment 42 is complete, do not send it again. Email person@example.test.",
    source_lang="en",
    target_lang="en",
    mode="hybrid",
)

contract = bundle.contract
model_prompt = gateway.render_bundle(bundle)

# Restore only placeholders explicitly allowed by the application.
answer = gateway.restore("Contact PII_EMAIL_1", bundle, allowed={"PII_EMAIL_1"})

compile() returns only the public contract. compile_private() additionally returns the local PII vault and source-reference map. Neither is inserted into the model prompt.

For model-facing applications, prefer ContextPipeline over calling the compiler directly. Use task="transform" for translation, rewriting, and summarization where numbers, negation, constraints, and placeholders must survive. Normal reasoning answers are not incorrectly required to echo the request.

CLI

contextir run \
  --backend ollama \
  --model qwen3:0.6b \
  --context-length 8192 \
  --max-output-tokens 256 \
  --prompt-overhead-tokens 32 \
  --text "Summarize the current agent state."

contextir compile \
  --text "Если платеж 42 выполнен, не отправляй его повторно." \
  --source-lang ru \
  --target-lang en \
  --mode hybrid \
  --out /tmp/contextir.json

contextir render --contract /tmp/contextir.json

The source checkout also exposes python3 scripts/contextir.py.

Contract

ContextIR v2 records:

  • source and target language;
  • intent and confidence;
  • typed entities and protected placeholders;
  • events with predicate, arguments, polarity, modality, condition, and source reference;
  • execution and privacy constraints;
  • optional lexical concepts;
  • included source fragments and unresolved source references;
  • prompt-size and latency statistics.

The packaged JSON Schema is available through:

from contextir.schemas import load_contract_schema

Privacy

The default detector is deliberately small and recognizes common emails, phones, payment-card-like values, and API-key-like tokens. Presidio can be used as an optional detector and extended with project-specific recognizers.

Automated PII detection is not a security boundary by itself. Keep the vault local, avoid logging prompt bodies, use explicit restoration allowlists, and evaluate recognizers on data representative of the deployment.

Current Evidence

The checked-in compiler smoke benchmark reports:

  • 9 compiler and 12 product-pipeline cases;
  • 0 expectation failures;
  • 0 pipeline failures;
  • 0 PII leaks into public contracts or rendered prompts;
  • 0.3627 prompt/source ratio on the one compression-eligible repeated-context case.

The first model-level A/B exposed a large quality loss in v0.3.0. Query-aware routing corrected that failure. On the v0.5.0 nine-case Qwen3 8B run, raw and auto both scored 0.7272, while auto reduced model input by 70.0% and mean latency from 53.8 s to 2.51 s. An external 1,500-row Presidio Research benchmark measured 0.8471 precision and 1.0000 recall for the dependency-free email/phone/card profile. These are bounded results, not universal quality or privacy claims; broader official tasks and deployment-specific PII evaluation remain required.

A constrained-window retrieval smoke run packs the same long input to fit a 256-token Ollama context. Qwen3 0.6B and 8B both recovered cobalt-seven; the backend reported 229 prompt tokens with 16 tokens reserved for output.

An oversized single-segment follow-up uses bounded chunked retrieval with a 256-token context. Qwen3 0.6B and 8B both recovered cobalt-seven from the same selected chunk using one map call at 75% of the available prompt budget.

A constrained 2K-context A/B over ten official LongBench QA/retrieval examples reduced measured backend input to 39.0% of raw for both tested models. Qwen3 8B quality increased from 0.1668 to 0.6983 with a paired 95% bootstrap delta of [0.2086, 0.8225]. Qwen3 0.6B increased from 0.1889 to 0.3121, but its delta interval [-0.0154, 0.3392] does not establish a reliable quality gain. All ten official cases used one packed direct call; a separate synthetic oversized-segment diagnostic activated the map path and recovered the answer with both models.

End-to-end baselines on the same Qwen3 8B run scored 0.6221 for a 32K same-model neural summary and 0.1910 for Nomic embedding retrieval. ContextIR scored 0.6983. Summary remained statistically competitive in quality on this small subset, but processed 24.95x as many input tokens as ContextIR and took 32.0x its latency. The tested embedding configuration processed 33.43x as many tokens and was significantly worse in paired quality. These costs include preprocessing; they are not answer-prompt-only comparisons.

See the local model A/B card and benchmark roadmap.

Research Layer

The repository retains the earlier RU/EN lexical graph, graph embeddings, synthetic translator, checkpoints, and roundtrip experiments. They are not loaded by the default package path. Use the research extra and source checkout for those experiments.

The old WordNet keyword roundtrip is preserved as a baseline, not presented as the production compiler. See RESEARCH_HISTORY.md.

Development

python3 -m pip install -e '.[dev,research]'
python3 -m unittest discover -s tests -v
python3 scripts/evaluate_contextir.py
python3 -m build
python3 -m twine check dist/*

Architecture, scope, contribution, and security guidance:

License

ContextIR code is licensed under Apache-2.0. Research datasets and derived lexical artifacts retain their upstream terms. See THIRD_PARTY_NOTICES.md before redistributing them.

Metadata

Release files for contextir 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contextir 1.4.0
File Size Uploaded Provenance
contextir-1.4.0.tar.gz 71.9 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for contextir 1.4.0
File Interpreter ABI Platform
contextir-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 144.6 kB

Release files / contextir-1.4.0.tar.gz

Download URL contextir-1.4.0.tar.gz
Size 71.9 kB
Tags Source
SHA-256 checksum
How to use checksums
014a7ae118e55023e6c097be007682cd5c29f3fd99b320bbeb59e6ef81d2aff4
BLAKE2b-256 checksum
How to use checksums
46aeb80e3be82459b93592baa19b5971ca725f221e4fb7dc85964991793c7ddf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 19, 2026.

Transparency log

Release files / contextir-1.4.0-py3-none-any.whl

Download URL contextir-1.4.0-py3-none-any.whl
Size 72.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a46b8bb88fc7ea4e388a1954c73099d8e9dd0c97035d0dbd82651b486df5d103
BLAKE2b-256 checksum
How to use checksums
0720ace81f8c842e15a7daa358957cc1935d3e6fdb73a50cf9cbb3bd2be97fb6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 19, 2026.

Transparency log

Security for contextir 1.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for contextir-1.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page