Skip to main content

Python Client SDK + framework adapters for the human-in-the-loop approval API.

Project description

intrupt-py-sdk

Client SDK + framework adapters for the human-in-the-loop approval API.

Overview

intrupt-py-sdk provides a Python client and framework adapters for integrating human approval workflows into AI agents. It includes:

  • HTTP Client: Direct API client for creating and managing approvals
  • ApprovalMiddleware: Process-wide singleton for easy client access
  • LangGraph Adapter: Decorator-based approval integration for LangGraph agents
  • Policy Engine: Configurable approval policies based on tool names and risk levels

Installation

uv add intrupt-py-sdk or pip install intrupt-py-sdk

Or install with development dependencies:

uv add intrupt-py-sdk[test] or pip install intrupt-py-sdk[test]

Quick Start

1. Set up the Approval API

First, ensure you have the approval API running. Set the following environment variables:

export APPROVAL_BASE_URL="http://localhost:8080"    # intrupt API base URL
export APPROVAL_API_KEY="your-api-key" # Optional for self-hosted intrupt API

2. Initialize the Middleware

Initialize the ApprovalMiddleware once at application startup:

from intrupt_py_sdk.adapters.approval_middleware import ApprovalMiddleware

ApprovalMiddleware(
    base_url="http://localhost:8080", # intrupt API base URL
    api_key="your-api-key" # Optional for self-hosted intrupt API
)

3. Use the Client

Get the client instance and create approvals:

from intrupt_py_sdk.adapters.approval_middleware import ApprovalMiddleware
from intrupt_py_sdk.core.client import ApprovalClient

# Get the singleton client instance
client = ApprovalMiddleware.get_client()

# Or create a new client instance
client = ApprovalClient(
    base_url="http://localhost:8080",
    api_key="your-api-key",
    timeout=10.0
)

approval = client.create_approval(
    thread_id="thread-123",
    action="purchase_stock",
    message="Approve buying shares",
    channel="slack",
    tool={
        "name": "purchase_stock",
        "kwargs": {"symbol": "AAPL", "quantity": 10}
    },
    agent_callback_url="http://localhost:8081/resume"
)

LangGraph Integration

The SDK provides a decorator for LangGraph tools that require human approval:

from langchain_core.tools import tool
from langgraph.types import RunnableConfig
from intrupt_py_sdk.adapters.langgraph import approval_required

@tool
@approval_required(
    action="purchase_stock",
    message="Approve buying shares",
    channel="slack",
    args=["symbol", "quantity"],
)
def purchase_stock(symbol: str, quantity: int, config: RunnableConfig) -> dict:
    """Simulate purchasing a given quantity of a stock symbol."""
    return {
        "status": "success",
        "message": f"Purchase order placed for {quantity} shares of {symbol}.",
        "symbol": symbol,
        "quantity": quantity,
    }

When the decorated tool is called:

  1. The agent pauses via langgraph.types.interrupt()
  2. An approval record is created on the API
  3. The tool body only executes if the human approves

Complete LangGraph Example

See example/agent.py for a complete FastAPI + LangGraph agent with approval workflows

Configuration

Environment Variables

  • APPROVAL_BASE_URL: Base URL of the approval API (default: empty string)
  • APPROVAL_API_KEY: Bearer token for API authentication (default: empty string)

ApprovalMiddleware

The ApprovalMiddleware is a singleton that holds a process-wide ApprovalClient. Initialize it once at startup:

ApprovalMiddleware(
    base_url="http://localhost:8080",  # Optional, defaults to APPROVAL_BASE_URL
    api_key="your-api-key"              # Optional, defaults to APPROVAL_API_KEY
)

Development

uv sync

Running Tests

uv run pytest -v

Running the Example Agent

uv run python example/agent.py

The example agent runs on http://localhost:8081 and provides:

  • POST /call-tool: Start or continue a chat
  • POST /resume: Resume an approval-paused run

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

intrupt_py_sdk-0.0.1a2.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

intrupt_py_sdk-0.0.1a2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file intrupt_py_sdk-0.0.1a2.tar.gz.

File metadata

  • Download URL: intrupt_py_sdk-0.0.1a2.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for intrupt_py_sdk-0.0.1a2.tar.gz
Algorithm Hash digest
SHA256 e52f7ff4d74dcd18518dd0b9a56bc3d3747adc42010e7195f8f177c73cfbfbe6
MD5 5fbf84482b0afe574857e18804b37d59
BLAKE2b-256 37e31dc2a57e3d99e2a234455fd1eb614823b04ec515798e8951e6f196549476

See more details on using hashes here.

File details

Details for the file intrupt_py_sdk-0.0.1a2-py3-none-any.whl.

File metadata

  • Download URL: intrupt_py_sdk-0.0.1a2-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for intrupt_py_sdk-0.0.1a2-py3-none-any.whl
Algorithm Hash digest
SHA256 9d1c213978d30ababcd44551b402c5c1d55f0b88d40771a73534b9cc9b4f0312
MD5 27281d120f07dffc386cdd15eb2412a1
BLAKE2b-256 e59ef1ced9eeda0a2db1f1c7fdd99f418abe0509dd34eb8fd7fb388e4acf4f34

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