Skip to main content

Self-healing API proxy for AI agents with x402 outcome-based pricing. Successes are free. Pay per successful heal in USDC.

Project description

graceful-fail

PyPI npm

Part of SelfHeal — autonomous error recovery for AI agents.

Self-healing API proxy for AI agents. Route your HTTP calls through Graceful Fail and get structured, LLM-powered fix instructions when APIs return errors.

Instead of your agent crashing on a 422 or retrying a 503 blindly, it gets back:

{
  "is_retriable": false,
  "actionable_fix_for_agent": "Remove 'name' field. Add 'first_name' and 'last_name' as separate string fields.",
  "suggested_payload_diff": {
    "remove": ["name"],
    "add": {"first_name": "string", "last_name": "string"}
  },
  "error_category": "validation_error"
}

Successful requests (2xx/3xx) pass through with zero overhead. You only pay when the LLM is invoked on a failed request.

Install

pip install graceful-fail

With LangChain support:

pip install 'graceful-fail[langchain]'

Quick Start

from graceful_fail import GracefulFail

gf = GracefulFail(api_key="gf_your_key")

response = gf.post("https://api.example.com/users", json={"name": "Alice"})

if response.intercepted:
    # The API returned an error — here's exactly how to fix it
    print(response.error_analysis.actionable_fix_for_agent)
    print(response.error_analysis.suggested_payload_diff)
else:
    # Success — here's the data
    print(response.data)

Async Support

from graceful_fail import GracefulFailAsync

async with GracefulFailAsync(api_key="gf_your_key") as gf:
    response = await gf.post("https://api.example.com/users", json={"name": "Alice"})

LangChain Integration

As a Tool (recommended for agents)

from graceful_fail.langchain import GracefulFailTool
from langchain.agents import create_react_agent

tool = GracefulFailTool(api_key="gf_your_key")
agent = create_react_agent(llm, [tool])

# Your agent now gets structured fix instructions instead of raw HTTP errors

As a Requests Wrapper

Drop-in replacement for LangChain's TextRequestsWrapper:

from graceful_fail.langchain import GracefulFailRequests

requests = GracefulFailRequests(api_key="gf_your_key")
result = requests.get("https://api.example.com/users")

requests-Compatible Session

For codebases already using requests:

from graceful_fail.patch import GracefulFailSession

session = GracefulFailSession(api_key="gf_your_key")
resp = session.post("https://api.example.com/users", json={"name": "Alice"})

print(resp.status_code)
print(resp.json())

if resp.graceful_fail_intercepted:
    print(resp.error_analysis)

Response Object

Every call returns a GracefulFailResponse:

Field Type Description
status_code int HTTP status from the destination API
intercepted bool True if the error was analyzed by the LLM
data Any Response body (success) or full error envelope (intercepted)
error_analysis ErrorAnalysis LLM analysis (only when intercepted=True)
raw_response Any Original destination API response body
credits_used int 0 for pass-through, 1 for intercepted

ErrorAnalysis Fields

Field Type Description
is_retriable bool Whether retrying the same request may succeed
human_readable_explanation str What went wrong, in plain English
actionable_fix_for_agent str Exact instruction for the agent
suggested_payload_diff PayloadDiff What to change (remove/add/modify)
error_category str validation_error, auth_error, rate_limit, etc.

Auto-Apply Fix

if response.intercepted:
    fixed_payload = response.error_analysis.suggested_payload_diff.apply(original_payload)
    retry_response = gf.post(url, json=fixed_payload)

Get Your API Key

  1. Sign up at selfheal.dev
  2. Create an API key in the dashboard
  3. Free tier: 500 requests/month

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graceful_fail-0.4.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graceful_fail-0.4.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file graceful_fail-0.4.0.tar.gz.

File metadata

  • Download URL: graceful_fail-0.4.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for graceful_fail-0.4.0.tar.gz
Algorithm Hash digest
SHA256 3d8431b33832652f25033c3bda68e070ffd1b24424c5bbce3b976a5c18e659af
MD5 70d5a3fddb77b671aad396e2d1749d3a
BLAKE2b-256 1e12930b58b97cede54d4b33698e9482e211f1638c90f7c654af93a9b05734ac

See more details on using hashes here.

File details

Details for the file graceful_fail-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: graceful_fail-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for graceful_fail-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c02f1b176c015c5c9e5f6d3383a073553cbfe5fd2ffdf9ff3e7c7803fad001ed
MD5 46e81efad22f2cf4b3ccd8dc490889e1
BLAKE2b-256 0026fd1542bee7b051e1ccb7a40701674b1652a69b8943b3b5c50374a9701d71

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page