Skip to main content

Specora Runtime SDK (Python)

Thin Python client SDK for Specora Runtime Authority enforcement. Wrap your AI agent tool calls with deterministic runtime governance.

Installation

pip install specora-runtime

Quickstart

from specora_runtime import SpecoraClient, CompletionStatus

client = SpecoraClient(
    base_url="https://api.specora.ai",
    api_key="sk_...",
    org_id="your-org-uuid",
)

# Wrap tool execution with enforcement
with client.enforced(
    action_type="db_write",
    scope={"table": "users"},
    args={"where": {"id": 1}, "set": {"name": "Alice"}},
    estimated_cost=0.02,
    is_write_op=True,
) as ctx:
    # Your tool execution here
    result = execute_database_write()
    ctx.complete(status=CompletionStatus.SUCCESS)

Features

  • enforce() - Request enforcement decision before execution
  • enforced() - Context manager with auto-completion reporting
  • simulate() - Dry-run enforcement check (no reservations)
  • Fail-closed by default - Network errors block execution
  • Idempotency - request_id ensures exactly-once processing
  • Typed exceptions - SpecoraBlockedError, SpecoraSuspendedError

Usage

enforce() - Direct Enforcement

result = client.enforce(
    action_type="api_call",
    scope={"service": "stripe"},
    args={"method": "charge"},
    estimated_cost=0.50,
    estimated_volume=1,
    is_write_op=True,
)

if result.is_allowed:
    # Execute the action
    pass

enforced() - Context Manager

The context manager handles completion reporting automatically:

from specora_runtime import CompletionStatus

with client.enforced(
    action_type="file_mutation",
    scope={"repo": "infra", "path": "prod.tf"},
    args={"op": "write"},
    estimated_cost=0.01,
    is_write_op=True,
) as ctx:
    # Execute your tool
    write_file()

    # Explicit completion (optional - auto-completes on exit)
    ctx.complete(status=CompletionStatus.SUCCESS, cost_actual=0.008)

Behavior:

  • Raises SpecoraBlockedError if action is blocked
  • Raises SpecoraSuspendedError if approval is required
  • Auto-completes with SUCCESS on normal exit
  • Auto-completes with FAILURE if exception occurs

simulate() - Dry-Run

sim = client.simulate(
    action_type="schema_migration",
    scope={"db": "prod"},
    args={"migration": "2026_03_add_idx"},
    estimated_cost=5.0,
    is_write_op=True,
)

print(f"Would be: {sim.decision}")  # allow, block, or suspend
print(f"Blocked reason: {sim.blocked_reason}")

Simulation mode:

  • Returns decision without making reservations
  • Does NOT require completion reporting
  • Does NOT raise on block/suspend

Fail Mode

from specora_runtime import FailMode

# Default: CLOSED - errors raise FailClosedError
client = SpecoraClient(
    base_url="https://api.specora.ai",
    api_key="sk_...",
    org_id="...",
    fail_mode=FailMode.CLOSED,
)

# Alternative: OPEN - errors return allow (with warning)
client = SpecoraClient(
    base_url="https://api.specora.ai",
    api_key="sk_...",
    org_id="...",
    fail_mode=FailMode.OPEN,
)

CLOSED (default):

  • Network timeouts raise FailClosedError
  • Server errors (5xx) raise FailClosedError
  • Action is NOT executed

OPEN:

  • Network/server errors return EnforcementResult(decision=ALLOW, source="fail_open")
  • Action MAY proceed (caller decides)

Note: 4xx client errors (400, 401, 403) always raise regardless of fail_mode.

Idempotency

All requests support idempotency via request_id:

from uuid import uuid4

request_id = uuid4()

# First call
result1 = client.enforce(
    action_type="payment",
    scope={"account": "123"},
    args={"amount": 100},
    request_id=request_id,
)

# Duplicate call with same request_id returns same journal entry
result2 = client.enforce(
    action_type="payment",
    scope={"account": "123"},
    args={"amount": 100},
    request_id=request_id,
)

assert result1.journal_id == result2.journal_id

If request_id is omitted, a UUID is auto-generated.

Error Handling

from specora_runtime import (
    SpecoraBlockedError,
    SpecoraSuspendedError,
    FailClosedError,
)

try:
    with client.enforced(...) as ctx:
        do_something()
except SpecoraBlockedError as e:
    print(f"Blocked: {e.reason}")
    print(f"Journal ID: {e.journal_id}")
except SpecoraSuspendedError as e:
    print(f"Approval required: {e.approval_source}")
except FailClosedError as e:
    print(f"Service unavailable: {e}")

Callbacks

Register callbacks for observability:

def on_decision(result):
    print(f"Decision: {result.decision}")
    metrics.record_enforcement(result)

def on_error(exc):
    logging.error(f"Specora error: {exc}")

client = SpecoraClient(
    base_url="https://api.specora.ai",
    api_key="sk_...",
    org_id="...",
    on_decision=on_decision,
    on_error=on_error,
)

Configuration

Parameter Default Description
base_url - Specora API URL
api_key - API key (Bearer token)
org_id - Organization UUID
project_id None Project scope (optional)
agent_id None Agent identifier (optional)
fail_mode CLOSED CLOSED or OPEN
timeout_seconds 10 Request timeout
retries 2 Retry attempts for transient errors

Security Notes

  • Scope must be explicit - Always specify meaningful scope for audit trail
  • request_id is idempotent - Use for exactly-once semantics
  • API key is Bearer token - Store securely, never log

Versioning

This SDK follows Semantic Versioning:

  • MAJOR: Breaking changes to public API
  • MINOR: New features, backward compatible
  • PATCH: Bug fixes, backward compatible

API contract: The SDK is compatible with Specora Runtime Authority v1.x.

License

Apache 2.0 - See LICENSE for details.

Metadata

Release files for specora-runtime 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for specora-runtime 0.2.0
File Size Uploaded Provenance Actions
specora_runtime-0.2.0.tar.gz 6.0 MB Without provenance Details for specora_runtime-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for specora-runtime 0.2.0
File Interpreter ABI Platform Actions
specora_runtime-0.2.0-py3-none-any.whl Python 3 none any Details for specora_runtime-0.2.0-py3-none-any.whl

Total release size: 6.0 MB

Release files / specora_runtime-0.2.0.tar.gz

Download URL specora_runtime-0.2.0.tar.gz
Size 6.0 MB
Tags Source
SHA-256 checksum
How to use checksums
c357a453ce3fe788daf2a9abb29ede456778f0dd8150f9210cc2f1aa805987d7
BLAKE2b-256 checksum
How to use checksums
b8385359325a117c7cdabb0d83fd1c4621d149a7e7ccbd2f7866432779319ee5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / specora_runtime-0.2.0-py3-none-any.whl

Download URL specora_runtime-0.2.0-py3-none-any.whl
Size 34.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b3aa97a1e8db65ee48039f4f2466241401d7cb6069af285707c015c7e6e6fae1
BLAKE2b-256 checksum
How to use checksums
c515b5e8fcfec8e88ac1e56b2a41aee976d48de07d86e58d3bbb6197e910c981
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for specora-runtime 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page