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

python3 -m pip install tryagent

From a checkout, install it locally with python3 -m pip install -e packages/python-sdk.

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.2.tar.gz (15.0 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.2-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tryagent-0.0.2.tar.gz
  • Upload date:
  • Size: 15.0 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.2.tar.gz
Algorithm Hash digest
SHA256 c781c90d8be544b3a6a0e8fa8c87c589ba0210a6a55f98d0c0aabbf8c8fbbf28
MD5 3d038a95d03f3ef1eff6ab5275fc8441
BLAKE2b-256 730ad6e0753418809f6c17acea00e66ae6989efd4162ac0c22774b6f27d89f26

See more details on using hashes here.

Provenance

The following attestation bundles were made for tryagent-0.0.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: tryagent-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6fdf9aa126a47bf82426a7ee51b8477d93e768f1d5dcde984a917628a99b6429
MD5 eb5cd2cf3f98eb5fa05f15c703d2b8d9
BLAKE2b-256 b6b9e1bdf9cf236a123c6c6a869b645825924472cdcdd3a63940444f5f3bdf13

See more details on using hashes here.

Provenance

The following attestation bundles were made for tryagent-0.0.2-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