Skip to main content

Python SDK for Semantic Security - capture and send IntentEvents with LangGraph integration

Project description

Tupl Python SDK

Python client library for the Semantic Security MVP - capture and send IntentEvents to the Management Plane for policy enforcement.

Installation

# Install with uv (recommended)
uv pip install -e .

# Or with pip
pip install -e .

Quick Start

from tupl import TuplClient, IntentEvent, Actor, Resource, Data, Risk
import time
import uuid

# Create client
client = TuplClient(endpoint="http://localhost:8000")

# Create an intent event
event = IntentEvent(
    id=f"evt-{uuid.uuid4()}",
    tenantId="tenant-123",
    timestamp=time.time(),
    actor=Actor(id="user-alice", type="user"),
    action="read",
    resource=Resource(type="database", name="users_db", location="cloud"),
    data=Data(categories=["pii"], pii=True, volume="row"),
    risk=Risk(authn="mfa", network="corp", timeOfDay=14)
)

# Send to Management Plane
result = client.capture(event)

if result:
    print(f"Decision: {'ALLOW' if result.decision == 1 else 'BLOCK'}")
    print(f"Similarities: {result.slice_similarities}")

client.close()

Features

Immediate Mode (Default)

Send events immediately and get synchronous responses:

client = TuplClient(endpoint="http://localhost:8000")
result = client.capture(event)  # Blocks until response

Buffered Mode

Buffer events and send in batches for better performance:

client = TuplClient(
    endpoint="http://localhost:8000",
    buffered=True,
    buffer_size=10,      # Flush after 10 events
    buffer_timeout=5.0   # Or flush after 5 seconds
)

client.capture(event1)  # Returns None (buffered)
client.capture(event2)  # Returns None (buffered)
# ... automatically flushes when buffer is full or timeout occurs

client.flush()  # Manual flush
client.close()  # Flushes remaining events

Async Support

from tupl import AsyncTuplClient

async with AsyncTuplClient(endpoint="http://localhost:8000") as client:
    result = await client.capture(event)

Context Manager

with TuplClient(endpoint="http://localhost:8000") as client:
    result = client.capture(event)
# Automatically closes and flushes

Configuration

TuplClient Options

  • endpoint (str): Management Plane base URL (default: http://localhost:8000)
  • api_version (str): API version (default: v1)
  • buffered (bool): Enable event buffering (default: False)
  • buffer_size (int): Max events before auto-flush (default: 10)
  • buffer_timeout (float): Seconds before auto-flush (default: 5.0)
  • timeout (float): HTTP request timeout in seconds (default: 10.0)
  • retry_count (int): Number of retries on failure (default: 3)

Data Types

IntentEvent

Structured record of an LLM/tool call intent:

  • id: Unique event ID (UUID)
  • schemaVersion: Schema version (always "v1")
  • tenantId: Tenant identifier
  • timestamp: Unix timestamp
  • actor: Who initiated the action
  • action: What action ("read", "write", "delete", "export", "execute", "update")
  • resource: What resource is being accessed
  • data: Data characteristics
  • risk: Risk context

Actor

  • id: Actor identifier
  • type: "user" or "service"

Resource

  • type: Resource type ("database", "file", "api", "service", "user_data")
  • name: Optional resource name
  • location: Optional location ("local", "cloud", "external")

Data

  • categories: List of data categories ("pii", "financial", "medical", "public", "internal")
  • pii: Optional boolean indicating PII data
  • volume: Optional volume class ("row", "table", "dump", "bulk")

Risk

  • authn: Authentication level ("none", "user", "mfa", "service")
  • network: Network context ("corp", "vpn", "public")
  • timeOfDay: Optional hour of day (0-23)

ComparisonResult

Response from Management Plane:

  • decision: 0 = block, 1 = allow
  • slice_similarities: List of 4 floats (action, resource, data, risk similarity scores)

Examples

See examples/basic_usage.py for a complete example.

# Run basic example
cd tupl_sdk/python
uv run python examples/basic_usage.py

Testing

# Run unit tests
uv run pytest tests/test_client.py -v

# Run with coverage
uv run pytest tests/test_client.py --cov=tupl --cov-report=html

Development

Project Structure

tupl_sdk/python/
├── tupl/                  # Main package
│   ├── __init__.py       # Public API exports
│   ├── types.py          # Pydantic data models
│   ├── client.py         # TuplClient + AsyncTuplClient
│   └── buffer.py         # EventBuffer for batching
├── tests/                # Unit tests
│   ├── __init__.py
│   └── test_client.py
├── examples/             # Usage examples
│   ├── __init__.py
│   └── basic_usage.py
├── pyproject.toml        # Package configuration
└── README.md             # This file

Dependencies

  • Python 3.14+
  • httpx (HTTP client)
  • pydantic (data validation)

Code Style

  • Type hints on all functions
  • Pydantic models for all data structures
  • No Dict[str, Any] fields (Google GenAI compatibility)
  • Comprehensive docstrings

Troubleshooting

Connection Errors

Ensure the Management Plane is running:

cd management-plane
./run.sh
# Server should start on http://localhost:8000

Test health endpoint:

curl http://localhost:8000/health

Import Errors

Install the package in development mode:

uv pip install -e .

License

See root project LICENSE file.

Support

Report issues at: https://github.com/your-org/mgmt-plane/issues

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

tupl_sdk-1.2.0.tar.gz (37.4 kB view details)

Uploaded Source

Built Distribution

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

tupl_sdk-1.2.0-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

Details for the file tupl_sdk-1.2.0.tar.gz.

File metadata

  • Download URL: tupl_sdk-1.2.0.tar.gz
  • Upload date:
  • Size: 37.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for tupl_sdk-1.2.0.tar.gz
Algorithm Hash digest
SHA256 6303d74c2bb84d4aee5764d489c66c3be2816526aacf98cb7057034bfd070a37
MD5 bd70a53a83aa8a67275473a1908e61c3
BLAKE2b-256 fb03a030340da808616dc2150af950efa94ce563f122d0e941a1f36883298222

See more details on using hashes here.

File details

Details for the file tupl_sdk-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: tupl_sdk-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 33.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for tupl_sdk-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a93ba7a091fe0e8e69717ebce942058fc63e52a0a57c1d52b7ab09f24368d5a
MD5 49f76c64a6899ec451b62e79b9b9b1ed
BLAKE2b-256 57801259c8f12a7154a7b9c8c09ec10c61d4375d1ba4fe52dfedec154de9a798

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