Skip to main content

Python SDK for Superagent.

Project description

Superagent Python SDK

Python client for calling the Superagent Guard and Redact endpoints.

Installation

pip install superagent-ai

Local development with uv

From the repository root, install the package (including test extras) and create a managed virtual environment:

cd sdk/python
uv sync --extra tests

This will provision .venv, install the SDK in editable mode, and pull in the testing dependencies. Once synced, run the test suite with:

uv run pytest tests

Usage

import asyncio
from superagent_ai import create_client

async def main() -> None:
    client = create_client(
        api_base_url="https://app.superagent.sh/api",  # Optional, this is the default
        api_key="sk-...",
    )

    # Guard: Analyze commands for security threats
    guard_result = await client.guard(
        "Write a hello world script",
        on_block=lambda reason: print("Guard blocked:", reason),
        on_pass=lambda: print("Guard approved!"),
    )

    if guard_result.rejected:
        print("Rejected:", guard_result.reasoning)
    else:
        print("Approved:", guard_result.decision)

    # Redact: Remove sensitive data from text
    redact_result = await client.redact(
        "My email is john@example.com and SSN is 123-45-6789"
    )

    print(redact_result.redacted)
    # Output: "My email is <REDACTED_EMAIL> and SSN is <REDACTED_SSN>"

    await client.aclose()

asyncio.run(main())

Using as a context manager

import asyncio
from superagent_ai import create_client

async def main() -> None:
    async with create_client(api_key="sk-...") as client:
        result = await client.guard("command")
        redacted = await client.redact("text")

asyncio.run(main())

API Reference

create_client(**kwargs)

Creates a new Superagent client.

Parameters:

  • api_key (required) – API key provisioned in Superagent
  • api_base_url (optional) – Base URL for the API (defaults to https://app.superagent.sh/api)
  • client (optional) – Custom httpx.AsyncClient instance
  • timeout (optional) – Request timeout in seconds (defaults to 10.0)

Returns: Client

client.guard(command, *, on_block=None, on_pass=None)

Analyzes a command for security threats.

Parameters:

  • command – The text to analyze
  • on_block (optional) – Callback function called when command is blocked
  • on_pass (optional) – Callback function called when command is approved

Returns: GuardResult

@dataclass
class GuardResult:
    rejected: bool              # True if guard blocked the command
    reasoning: str              # Explanation from the guard
    raw: AnalysisResponse       # Full API response
    decision: Optional[GuardDecision]  # Parsed decision details
    usage: Optional[GuardUsage]        # Token usage statistics

@dataclass
class GuardDecision:
    status: Literal["pass", "block"]
    violation_types: list[str]
    cwe_codes: list[str]

client.redact(text, *, url_whitelist=None, entities=None, file=None, format=None)

Redacts sensitive data from text.

Parameters:

  • text – The text to redact
  • url_whitelist (optional) – List of URL prefixes that should not be redacted
  • entities (optional) – List of custom entity types to redact (natural language descriptions)
  • file (optional) – File object to redact (e.g., PDF document)
  • format (optional) – Format of the file (currently only "PDF" is supported)

Returns: RedactResult

@dataclass
class RedactResult:
    redacted: str               # Text with sensitive data redacted
    reasoning: str              # Explanation of what was redacted
    raw: dict                   # Full API response
    usage: Optional[GuardUsage] # Token usage statistics

Detected PII/PHI Types

The redaction feature detects and replaces:

  • Email addresses<REDACTED_EMAIL>
  • Social Security Numbers<REDACTED_SSN>
  • Credit cards (Visa, Mastercard, Amex) → <REDACTED_CC>
  • Phone numbers (US format) → <REDACTED_PHONE>
  • IP addresses (IPv4/IPv6) → <REDACTED_IP>
  • API keys & tokens<REDACTED_API_KEY>
  • AWS access keys<REDACTED_AWS_KEY>
  • Bearer tokensBearer <REDACTED_TOKEN>
  • MAC addresses<REDACTED_MAC>
  • Medical record numbers<REDACTED_MRN>
  • Passport numbers<REDACTED_PASSPORT>
  • IBAN<REDACTED_IBAN>
  • ZIP codes<REDACTED_ZIP>

Custom Entity Redaction

You can specify custom PII entities to redact using natural language:

client = create_client(api_key="sk-...")

result = await client.redact(
    "My credit card is 4532-1234-5678-9010 and employee ID is EMP-12345",
    entities=["credit card numbers", "employee IDs"]
)
# Output: "My credit card is <REDACTED> and employee ID is <REDACTED>"

URL Whitelisting

You can specify URLs that should not be redacted by passing the url_whitelist parameter:

client = create_client(api_key="sk-...")

result = await client.redact(
    "Check out https://github.com/user/repo and https://secret.com/data",
    url_whitelist=["https://github.com", "https://example.com"]
)
# Output: "Check out https://github.com/user/repo and <URL_REDACTED>"

The whitelist is applied locally after redaction - URLs matching the prefixes are preserved, while non-whitelisted URLs are replaced with <URL_REDACTED>.

PDF File Redaction

You can redact sensitive information from PDF files:

import asyncio
from superagent_ai import create_client

async def main() -> None:
    async with create_client(api_key="sk-...") as client:
        # Read and redact PDF file
        with open("sensitive-document.pdf", "rb") as pdf_file:
            result = await client.redact(
                text="Analyze and redact PII from this document",
                file=pdf_file,
                format="PDF",
                entities=["SSN", "credit card numbers", "email addresses"]
            )

            print(result.redacted)  # Redacted text content from the PDF
            print(result.reasoning) # Explanation of what was redacted

asyncio.run(main())

Note: File redaction uses multipart/form-data encoding and currently supports PDF format only.

Error Handling

from superagent_ai import GuardError

try:
    result = await client.guard("command")
except GuardError as error:
    print(f"Guard error: {error}")

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

superagent_ai-0.0.14.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

superagent_ai-0.0.14-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file superagent_ai-0.0.14.tar.gz.

File metadata

  • Download URL: superagent_ai-0.0.14.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.2

File hashes

Hashes for superagent_ai-0.0.14.tar.gz
Algorithm Hash digest
SHA256 f90781bbee5f9ba5dae15ca725bd04bae3c9d4f9e112d82a1898c7a168d50fb5
MD5 81d83c91cceec92b96ef8a2bdf229c22
BLAKE2b-256 cacf21b9a2ed06bd832b217315afca4083e2774a0f55fdda596b5499008611e7

See more details on using hashes here.

File details

Details for the file superagent_ai-0.0.14-py3-none-any.whl.

File metadata

File hashes

Hashes for superagent_ai-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 cfa3be181aa44abde4045b2f9d10988fa5db906bdb31fe3b59a46dc179aa135c
MD5 75b63b4888dbe7132dd393a2dff8df41
BLAKE2b-256 cb83aee079d9b31a111841d1ab049d474f0b1db79f3537796de6f7af93e74d09

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