Skip to main content

Official Driftgard Python SDK — evaluate LLM interactions against your compliance policy

Project description

driftgard

Official Python SDK for Driftgard — evaluate LLM interactions against your compliance policy.

Install

pip install driftgard

Quick start

from driftgard import Driftgard

dg = Driftgard(api_key="your-api-key")

result = dg.evaluate(
    project_id="your-project-id",
    prompt="What stocks should I buy?",
    response="Based on current trends, you should invest in...",
    model_id="gpt-4o",
)

if result["evaluation"]["allowed"]:
    print("Safe to return to user")
else:
    # Use the fallback message if configured in your control pack
    if "fallback" in result:
        print("Show to user:", result["fallback"]["message"])
    print("Blocked:", result["evaluation"]["violations"])

Conversation tracking

Link evaluations within an agent session using session_id and parent_evaluation_id:

result = dg.evaluate(
    project_id="your-project-id",
    prompt="Transfer $500 to account 12345",
    response="I've initiated the transfer.",
    model_id="gpt-4o",
    session_id="sess_abc123",              # groups evals in a conversation
    parent_evaluation_id="eval_prev_id",   # chains to the previous eval
)

This enables chain depth protection (prevents infinite agent loops) and lets you trace evaluation lineage in the dashboard.

A/B experiments

Tag evaluations with an experiment_id to compare governance metrics across models:

result = dg.evaluate(
    project_id="your-project-id",
    prompt="Can I get a loan to invest in crypto?",
    response="Sure, taking out a personal loan to invest in crypto is a great way to maximise returns.",
    model_id="gpt-4o",
    experiment_id="financial-advisor-v1",  # optional
)

View experiment results on the Experiments page in the Driftgard dashboard.

Cost attribution

Pass optional usage metadata to track token consumption and cost per evaluation:

result = dg.evaluate(
    project_id="your-project-id",
    prompt="What stocks should I buy?",
    response="Based on current trends, you should invest in...",
    model_id="gpt-4o",
    usage={
        "prompt_tokens": 150,
        "completion_tokens": 320,
        "total_tokens": 470,
        "cost": 0.0047,  # USD
    },
)

All fields in usage are optional. When provided, token and cost data appears in the evaluation detail and is aggregated in experiment comparisons.

Features

  • Single evaluate() method — send prompt/response, get verdict
  • Auto-retry with exponential backoff on 5xx and network errors
  • Typed exceptions: AuthError, RateLimitError, FeatureNotAvailableError, ChainDepthExceededError
  • Works with Python 3.8+

Configuration

dg = Driftgard(
    api_key="your-api-key",                     # required
    base_url="https://api.driftgard.com",       # optional
    timeout=30,                                  # optional, seconds (default 30)
    max_retries=2,                               # optional (default 2)
)

Error handling

from driftgard import Driftgard, AuthError, RateLimitError, FeatureNotAvailableError, ChainDepthExceededError

try:
    result = dg.evaluate(...)
except AuthError:
    # Invalid or revoked API key (401)
    pass
except RateLimitError:
    # Too many requests (429)
    pass
except ChainDepthExceededError as e:
    # Agent loop detected — chain depth exceeded (429)
    print(f"Depth {e.depth} exceeds max {e.max_depth}")
except FeatureNotAvailableError as e:
    # API evaluate requires Compliance+ tier (403)
    print(e.tier)

Requirements

  • Python 3.8+
  • requests library
  • API key from Driftgard (Settings → API Keys)
  • Compliance or Enterprise tier for API evaluation

License

MIT

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

driftgard-1.5.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

driftgard-1.5.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file driftgard-1.5.0.tar.gz.

File metadata

  • Download URL: driftgard-1.5.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for driftgard-1.5.0.tar.gz
Algorithm Hash digest
SHA256 fa2fd458e7e55a395fc62fccf6da132d26eb5f6f81cf98b7bb15ee16bdaa804c
MD5 4a18ff542b511bcd7ead94a812596ec5
BLAKE2b-256 f2917229a71c347f27879660d7a42cdc3d9ca4add58fb4728d4cb5762e930a85

See more details on using hashes here.

File details

Details for the file driftgard-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: driftgard-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for driftgard-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 273b5b214e3183b09b20f0658370a0ec42377096a8ade38e1bc7d0448f7483ff
MD5 54b40acd0adba0ec9e4ad530d8df9e68
BLAKE2b-256 bd4d3773414f6a86718b2f92a78c7458189a3aa05f87926ded1fbf3bb0f00321

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