Skip to main content

Python SDK for creating and managing TryAgent escalations.

Project description

tryagent

Python SDK for creating and managing TryAgent escalations from agents, workflows, and reviewer tooling.

Full documentation: https://tryagentai.mintlify.app/quickstart

Install

The Python SDK package lives in this repository and is not published to PyPI yet. From a checkout, install it locally:

python3 -m pip install -e packages/python-sdk

After the first PyPI release, install it with:

python3 -m pip install tryagent

Create a client

import os

from tryagent import TryAgent

tryagent = TryAgent(api_key=os.environ["TRYAGENT_API_KEY"])

Override base_url for local development or tests:

tryagent = TryAgent(
    api_key=os.environ["TRYAGENT_API_KEY"],
    base_url="http://localhost:4000",
)

Send an escalation

Request payloads use the TryAgent API's camelCase fields, even from Python.

import os

from tryagent import TryAgent

tryagent = TryAgent(api_key=os.environ["TRYAGENT_API_KEY"])

escalation = tryagent.escalate(
    "orders.auth_doc",
    {
        "agentId": "order-agent",
        "runId": "run_4821",
        "subject": {
            "type": "order",
            "id": "ord_4821",
            "label": "Order #4821",
        },
        "question": "The authorization document is missing a signature date. Continue?",
        "evidence": [
            "Candidate name is present.",
            "Employer is present.",
            "Signature date is blank.",
        ],
        "choices": [
            {"id": "manual_review", "label": "Send to manual review"},
            {"id": "continue", "label": "Continue anyway"},
        ],
        "resume": {
            "mode": "webhook",
            "url": "https://api.example.com/tryagent/resume",
            "secret": os.environ["TRYAGENT_WEBHOOK_SECRET"],
        },
    },
)

print(escalation["id"], escalation["status"])

Resume a workflow

Always pass the raw request body to tryagent.webhooks.construct_event. Parsing and re-serializing JSON changes the signed bytes.

import os

from tryagent import TryAgent, WebhookSignatureError

tryagent = TryAgent(api_key=os.environ["TRYAGENT_API_KEY"])


def handle_resume(raw_body: bytes, signature: str | None) -> tuple[dict[str, bool], int]:
    try:
        event = tryagent.webhooks.construct_event(
            payload=raw_body,
            signature=signature,
            secret=os.environ["TRYAGENT_WEBHOOK_SECRET"],
        )
    except WebhookSignatureError:
        return {"ok": False}, 401

    resume_workflow(
        event["runId"],
        {
            "escalationId": event["escalationId"],
            "choice": event.get("choice"),
            "answeredBy": event.get("answeredBy"),
            "answeredAt": event.get("answeredAt"),
        },
    )
    return {"ok": True}, 200

Manage escalations

open_escalations = tryagent.escalations.list(status="open")

escalation = tryagent.escalations.get("esc_123")

tryagent.escalations.acknowledge("esc_123")

tryagent.escalations.decide(
    "esc_123",
    {
        "choice": "continue",
        "reason": "Signature verified out of band.",
        "response": {"approvedLimit": 5000, "riskLevel": "low"},
    },
)

tryagent.escalations.cancel("esc_456", {"reason": "Order withdrawn."})

Handle API errors

Non-2xx API responses and network failures raise ApiError. Network failures use status == 0; API responses use the HTTP status code. body contains the parsed response body when available, and request_id is read from the x-request-id response header when present.

from tryagent import ApiError

try:
    tryagent.escalate("orders.auth_doc", input)
except ApiError as error:
    print(error.status, error.request_id, error.body)
    raise

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

tryagent-0.0.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

tryagent-0.0.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file tryagent-0.0.1.tar.gz.

File metadata

  • Download URL: tryagent-0.0.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tryagent-0.0.1.tar.gz
Algorithm Hash digest
SHA256 736bd4b5a3749a8ae1635b5e9b93d1bf92070b98cf0c8828bf504887143b44f6
MD5 3ef8958bbdf3b57ef9222d4124b18b5f
BLAKE2b-256 51d538094b73ccf17576d363d4a0c2dabfc153d63046fa7be4e2ec63685a61a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tryagent-0.0.1.tar.gz:

Publisher: publish-python-sdk.yml on hiredbyreact/agentInbox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tryagent-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: tryagent-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tryagent-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0539cb2e556a6da22697650e137eef99c70ee278accf8c9a219ac6846c1a8590
MD5 ded1916df6f4e8eaeaf16329cda0a63c
BLAKE2b-256 2b4edc569ad8fbef923073bd886ab141c1640240578de0a4db528cffab3b8f90

See more details on using hashes here.

Provenance

The following attestation bundles were made for tryagent-0.0.1-py3-none-any.whl:

Publisher: publish-python-sdk.yml on hiredbyreact/agentInbox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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