Skip to main content

counterfix

Find why an LLM call failed, and get a fix that was tested to work.

Most LLM evaluation tools tell you that a model failed. counterfix is built to tell you why, and to hand back a fix it has already checked. When a call fails, it changes one thing at a time (adds context, removes a false premise, rewords the prompt, gives the model a tool, raises max_tokens) and re-runs the call. The change that fixes the failure is both the diagnosis and the prescription. We call this counterfactual diagnosis.

Status: pre-alpha (v0.0.1). The core records, the guard() wrapper and the seed failure set are in place. Detectors and the counterfactual engine are being built. APIs will change.

What it plugs into

counterfix is middleware: it sits between your code and any text-in, text-out LLM.

from openai import OpenAI
from counterfix import guard

client = guard(OpenAI())  # same API as before
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Why did Einstein win his third Nobel Prize?"}],
)
print(response.choices[0].message.content)
print(response.diagnosis.summary())  # what counterfix found

The same wrapper works with any OpenAI-compatible endpoint, for example Groq or a local Ollama server:

client = guard(OpenAI(base_url="http://localhost:11434/v1", api_key="ollama"))

Design rules:

  • Fail open. If counterfix itself errors, your call still returns normally.
  • Your errors stay yours. Exceptions from the provider reach your code unchanged.
  • Safe defaults. Live mode only flags failures and makes no extra LLM calls.
  • Light core. The core depends on 4 small libraries; providers and heavy tools are extras.

Failure types (v1)

Code Failure
F1 Truncation
F2 Format break (JSON, schema)
F3 Over-refusal
F4 Hallucination from missing knowledge
F5 False-premise acceptance
F6 Dropped instruction
F7 Prompt fragility
F8 Computation or token-level error
F9 Ambiguous request

API errors, rate limits and provider content filters are tracked separately, so they are never counted as the model's fault.

Install (development)

git clone https://github.com/KANSADWALA/CounterFix.git
cd CounterFix
pip install -e ".[dev]"
pytest

Optional extras: openai, anthropic, litellm, langchain, llamaindex, otel, semantic, cache, cli, report, jev.

Roadmap

  1. Foundations and seed failure set ✅
  2. Core records and the guard() plug ✅
  3. Cheap live detectors (F1 to F3) and the prompt linter
  4. LLM-judge and sampling detectors
  5. Diagnose mode and HTML report
  6. Counterfactual engine with verified fixes
  7. Model failure profiles
  8. More plugs: decorator, Anthropic, LiteLLM, LangChain, OpenTelemetry
  9. Evaluation on public benchmarks
  10. Release and paper

License

MIT

Metadata

Release files for counterfix 0.0.1

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

Source distribution (sdist)

Source distribution for counterfix 0.0.1
File Size Uploaded Provenance Actions
counterfix-0.0.1.tar.gz 11.3 kB Without provenance Details for counterfix-0.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 26.5 kB

Release files / counterfix-0.0.1.tar.gz

Download URL counterfix-0.0.1.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1dd79f4b4e4f5ac92c404868f510792956063f1af5b23132d243a8d9304b3583
BLAKE2b-256 checksum
How to use checksums
9840e61ebacec41e7284fb409bc79955fa0509fff3a06f846aa77553b951ee7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.16

Release files / counterfix-0.0.1-py3-none-any.whl

Download URL counterfix-0.0.1-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1cb10a8fbfd40a431f0f68ac2da033ee20600e2b7fbd2f31f3a40b22f61ea40c
BLAKE2b-256 checksum
How to use checksums
751e160612e5e702031f6d0cb0e689bdecb6493f0fb89e474049ba7fc6fa6a46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.16

Security for counterfix 0.0.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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