SDKsPythonGuardrails SDK

Guardrails SDK (Python)

v4.0.1BetaLast updated: March 2026

The Python SDK provides guardrails support for real-time content scanning.

Quick Start

import thinkhive
 
thinkhive.init(api_key='th_your_api_key')
 
# Scan user input
result = thinkhive.guardrails.scan(
    input='My email is john@example.com and SSN is 123-45-6789',
    scanners=['pii'],
    config={'pii': {'action': 'redact'}}
)
 
print(result.action)         # 'redact'
print(result.redacted_input) # 'My email is [EMAIL] and SSN is [SSN]'

Scanning Content

# Scan with multiple scanners
result = thinkhive.guardrails.scan(
    input=user_message,
    output=agent_response,
    scanners=['pii', 'secrets', 'keywords'],
    config={
        'pii': {'action': 'redact', 'entities': ['email', 'phone']},
        'secrets': {'action': 'block'},
        'keywords': {'keywords': ['confidential', 'internal']}
    },
    options={'timeout': 3000, 'shortCircuit': True}  # options keys use camelCase
)
 
if result.action == 'block':
    raise ValueError(f'Content blocked: {result.action_reason}')
 
if result.action == 'redact':
    safe_input = result.redacted_input or user_message
    safe_output = result.redacted_output or agent_response

Using Policies

# Scan with a named policy
result = thinkhive.guardrails.scan(
    input=user_message,
    policy_id='policy_abc123'
)

Listing Scanners

scanners = thinkhive.guardrails.list_scanners()
for scanner in scanners:
    print(f'{scanner.name}: {scanner.description}')

Middleware Pattern

import thinkhive
 
thinkhive.init(api_key='th_your_api_key')
 
def process_message(user_message: str) -> dict:
    # 1. Scan input
    input_scan = thinkhive.guardrails.scan(
        input=user_message,
        policy_id='production-input-policy'
    )
 
    if input_scan.action == 'block':
        return {'error': 'Your message contains content that cannot be processed.'}
 
    safe_input = input_scan.redacted_input or user_message
 
    # 2. Get LLM response
    agent_response = call_llm(safe_input)
 
    # 3. Scan output
    output_scan = thinkhive.guardrails.scan(
        output=agent_response,
        policy_id='production-output-policy'
    )
 
    if output_scan.action == 'block':
        return {'error': 'The response was filtered for safety.'}
 
    return {'response': output_scan.redacted_output or agent_response}

Next Steps

The Guardrails API is in beta. Endpoints and response schemas may change.