Skip to main content

selfheal-sdk

AI self-healing decorator for any Python data pipeline.

When your pipeline raises an exception, @healed classifies the failure, applies a fix automatically, and escalates to humans only when genuinely needed.

Built by Sahil Deol


Install

pip install selfheal-sdk

With AI backends (recommended):

pip install selfheal-sdk[typesafe]   # TypeSafe JEV — best classification
pip install selfheal-sdk[anthropic]  # Claude Haiku fallback
pip install selfheal-sdk[nvidia]     # Llama-3.1 fallback
pip install selfheal-sdk[all]        # everything

No extras? Zero-dep keyword fallback always works out of the box.


Quickstart

from selfheal import healed

@healed(pipeline="daily_sales_etl")
def run():
    records = extract()
    validate(records)        # raises? selfheal catches and heals it
    cleaned = transform(records)
    load(cleaned)

if __name__ == "__main__":
    run()

That's the only change needed.


How it works

When run() raises, @healed does this:

  1. Classify — sends the error + traceback to TypeSafe JEV (or fallback LLM). Returns failure_type, action, confidence, severity.
  2. Threshold check — auto-heals only when confidence ≥ 0.65 AND severity ≤ 3.
  3. Act:
Failure type Action Auto? Hook needed
BAD_DATA QUARANTINE ✅ run_quarantine()
TRANSIENT RETRY ✅ none
SCHEMA_DRIFT (new col) ADD_COLUMN ✅ add_columns_to_dest()
PERMISSION / CODE_DEFECT ESCALATE ❌ human approval
High severity (≥4) ESCALATE ❌ human approval
  1. Report — best-effort POST to your dashboard if configured. Never blocks the pipeline.

API keys

The healer tries backends in order: TypeSafe JEV → Anthropic → NVIDIA → keyword fallback.

export TYPESAFE_API_KEY=ts-...       # TypeSafe JEV (recommended)
export ANTHROPIC_API_KEY=sk-ant-...  # Claude Haiku fallback
export NVIDIA_API_KEY=nvapi-...      # Llama-3.1 fallback

Keyword fallback requires no API key and always works.


Decorator options

@healed(
    pipeline="my_etl",                    # name in logs
    server_url="http://localhost:8000",   # optional dashboard URL
    on_escalate=None,                     # sync callback(result) on ESCALATE
    max_retries=1,                        # retry attempts before giving up
    silent=False,                         # suppress INFO logs
    retry_safe=False,                     # set True if pipeline is idempotent
    retry_backoff_base=1.0,               # backoff base seconds (exponential)
    retry_jitter=0.25,                    # random jitter added to each backoff
    send_source_code=True,                # set False if code contains secrets
    source_redactor=None,                 # optional fn(src) → redacted_src
)
def run():
    ...

Recovery hooks

Define these in the same module as your decorated function. selfheal calls them automatically.

BAD_DATA → run_quarantine()

@healed(pipeline="daily_sales_etl")
def run():
    records = extract()
    validate(records)
    load(transform(records))

def run_quarantine():
    records = extract()
    clean = [r for r in records if r.get("customer_id") and r.get("amount", 0) > 0]
    bad   = [r for r in records if r not in clean]
    log_bad_rows(bad)
    load(transform(clean))

SCHEMA_DRIFT → add_columns_to_dest(cols, schema)

def add_columns_to_dest(extra_cols: list[str], actual_schema: dict) -> None:
    with connect() as conn:
        for col in extra_cols:
            conn.execute(f"ALTER TABLE dest ADD COLUMN IF NOT EXISTS {col} TEXT")
        conn.commit()

selfheal calls this before retrying, so the retry succeeds with the new column in place.

ESCALATE → on_escalate callback

def notify_slack(result: dict):
    print(f"[ALERT] {result['root_cause']}")

@healed(pipeline="orders_etl", on_escalate=notify_slack, max_retries=3)
def run():
    ...

Environment variables

Variable Default Description
TYPESAFE_API_KEY — TypeSafe JEV API key
ANTHROPIC_API_KEY — Anthropic Claude fallback
NVIDIA_API_KEY — NVIDIA NIM fallback
SH_MIN_CONFIDENCE 0.65 Minimum confidence to auto-heal
SH_MAX_SEVERITY 3 Max severity (1–5) to auto-heal

Version history

Version Changes
0.3.3 Major hardening: PATCH_AND_RETRY permanently removed; normalize_decision() strict fail-closed gate (unknown types → UNKNOWN/ESCALATE, out-of-range confidence fails closed, "false" string → False); ALLOWED_FAILURE_TYPES frozenset; IDENTIFIER_RE SQL injection guard on ADD_COLUMN columns; keyword fallback reordered (PERMISSION/RESOURCE checked before TRANSIENT); BAD_DATA + SCHEMA_DRIFT from keyword fallback always advisory (safe=False); retry_safe=False idempotency guard; exponential backoff on all retries including first; latest exception propagated through retry loop; QUARANTINE/ADD_COLUMN escalate with diagnosis context (no module hooks); send_source_code / source_redactor source privacy controls; bool rejected for max_retries; async on_escalate rejected for sync functions at decoration time; backoff params validated at decoration time; max_retries=0 handled without UnboundLocalError; suppress_escalated_exception param; server_url=None default
0.3.2 PyPI release
0.3.1 README updated for PyPI
0.3.0 TypeSafe JEV primary backend; ADD_COLUMN auto-heal; max_retries; runbook ingestion; confidence + severity thresholds
0.2.0 Anthropic + NVIDIA LLM backends
0.1.0 Initial release — keyword fallback only

selfheal-sdk — by Sahil Deol

Release files for selfheal-sdk 0.3.3

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

Source distribution (sdist)

Source distribution for selfheal-sdk 0.3.3
File Size Uploaded
selfheal_sdk-0.3.3.tar.gz 16.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for selfheal-sdk 0.3.3
File Interpreter ABI Platform
selfheal_sdk-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 31.5 kB

Release files / selfheal_sdk-0.3.3.tar.gz

Download URL selfheal_sdk-0.3.3.tar.gz
Size 16.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b34c89ac82a4d4bb09295329a428adf31f7afa63b8c26da8d5e7e523cba08d26
BLAKE2b-256 checksum
How to use checksums
d5675d3aba508b3e91a58729b5bcb989a7c0ee01c5b76363163e2df05de4ec98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.8

Release files / selfheal_sdk-0.3.3-py3-none-any.whl

Download URL selfheal_sdk-0.3.3-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d0f30c3787cbe81a7ee092ac0486e50553e8fed610aaef2825beb46238eb1d8
BLAKE2b-256 checksum
How to use checksums
29b90699e0d7a709031be735f99008f4a7b05c41339746c545e670fb8302a972
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.8

Release history Release notifications | RSS feed

0.3.5

2 release files

0.3.4

2 release files

This release

0.3.3 This release

2 release files

0.3.2

2 release files

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