Skip to main content

Overview

The official LangChain integration provides native middleware for AI governance, compliance, and human-in-the-loop workflows. Add enterprise-grade controls to your LangChain agents with zero modifications to your existing code.

Key Features

Guardrail Middleware

Policy-based validation of agent responses (GDPR, EU AI Act, Bias & Fairness)

Human-in-the-Loop

Approval workflows for sensitive tool calls with multi-channel routing

Dashboard Configuration

All policies configured in Velatir Dashboard - no code changes needed

Smart Routing

Low-risk actions approved instantly, high-risk routed to humans

Installation

pip install langchain-velatir

Quick Start

Guardrail Middleware

Validate agent responses through Velatir’s policy engine:
from langchain_velatir import VelatirGuardrailMiddleware
from langchain_anthropic import ChatAnthropic
from langchain.agents import create_react_agent

# Initialize middleware (policies configured in Velatir Dashboard)
middleware = VelatirGuardrailMiddleware(
    api_key="your-api-key",
    mode="blocking",  # Block responses that fail policies
)

# Add to your agent
model = ChatAnthropic(model="claude-3-5-sonnet-20241022")
agent = create_react_agent(model, tools, middleware=[middleware])

Human-in-the-Loop Middleware

Require approval before executing tool calls:
from langchain_velatir import VelatirHITLMiddleware

# Initialize middleware
middleware = VelatirHITLMiddleware(
    api_key="your-api-key",
    timeout=600.0,  # 10 minutes max wait
)

# Optionally filter which tools require approval
middleware = VelatirHITLMiddleware(
    api_key="your-api-key",
    require_approval_for=["delete_user", "execute_payment"]
)

# Add to your agent
agent = create_react_agent(model, tools, middleware=[middleware])

How It Works

Guardrail Flow

Human-in-the-Loop Flow

Configuration

All policies and flows are configured in your Velatir Dashboard - no code changes needed when updating policies.

Middleware Options

Parameters:
  • api_key (required): Your Velatir API key
  • mode: "blocking" or "logging" (default: "blocking")
  • approval_timeout: Max seconds to wait for decision (default: 30.0)
  • polling_interval: Seconds between status checks (default: 2.0)
  • blocked_message: Custom message when response is blocked
  • metadata: Additional context for review tasks
Modes:
  • blocking: Stops execution when Velatir denies response
  • logging: Logs decisions but continues execution
Parameters:
  • api_key (required): Your Velatir API key
  • timeout: Max seconds to wait for approval (default: 600.0)
  • polling_interval: Seconds between status checks (default: 5.0)
  • require_approval_for: Optional list of tool names to check
  • metadata: Additional context for review tasks
Filtering:
  • If require_approval_for is None: All tools are evaluated
  • If specified: Only listed tools go through Velatir

Real-World Examples

Content Moderation Agent

from langchain_velatir import VelatirGuardrailMiddleware
from langchain_anthropic import ChatAnthropic
from langchain.agents import create_react_agent

# Guardrails automatically check GDPR, bias, and prompt injection
middleware = VelatirGuardrailMiddleware(
    api_key="your-api-key",
    mode="blocking",
    metadata={"agent_type": "content_moderator"}
)

model = ChatAnthropic(model="claude-3-5-sonnet-20241022")
agent = create_react_agent(model, content_tools, middleware=[middleware])

# Agent responses automatically validated through Velatir
result = agent.invoke({"input": "Review this user-generated content..."})

Financial Transaction Agent

from langchain_velatir import VelatirHITLMiddleware

# Require human approval for sensitive financial operations
middleware = VelatirHITLMiddleware(
    api_key="your-api-key",
    require_approval_for=[
        "execute_payment",
        "transfer_funds",
        "update_account_balance"
    ],
    timeout=600.0,
    metadata={"department": "finance", "environment": "production"}
)

agent = create_react_agent(model, financial_tools, middleware=[middleware])

# High-value transactions routed to humans automatically
result = agent.invoke({"input": "Transfer $50,000 to supplier account"})

Combined Guardrails + HITL

# Use both middlewares together
guardrail_middleware = VelatirGuardrailMiddleware(
    api_key="your-api-key",
    mode="blocking"
)

hitl_middleware = VelatirHITLMiddleware(
    api_key="your-api-key",
    require_approval_for=["delete_data", "send_email"]
)

# Middleware executes in order
agent = create_react_agent(
    model,
    tools,
    middleware=[hitl_middleware, guardrail_middleware]
)

Policy Configuration

Configure policies in your Velatir Dashboard without touching code:

Approval Channels

Human reviewers can approve/deny requests through multiple channels:

Advanced Usage

Custom Metadata

Add context to help with routing and decision-making:
middleware = VelatirGuardrailMiddleware(
    api_key="your-api-key",
    metadata={
        "agent_name": "customer_support_bot",
        "environment": "production",
        "department": "support",
        "priority": "high"
    }
)

Error Handling

from langchain_velatir import (
    VelatirApprovalDeniedError,
    VelatirTimeoutError,
    VelatirMiddlewareError
)

try:
    result = agent.invoke({"input": "Execute sensitive action"})
except VelatirApprovalDeniedError as e:
    print(f"Action denied: {e.requested_change}")
except VelatirTimeoutError as e:
    print(f"Approval timeout after {e.timeout_seconds}s")
except VelatirMiddlewareError as e:
    print(f"Velatir error: {e}")

Async Support

Both middlewares fully support async operations:
# Async works automatically with LangChain's async methods
result = await agent.ainvoke({"input": "Your request"})

Installation & Documentation

langchain-velatir on PyPI

Complete package with examples and type hints

GitHub Repository

Source code, examples, and issue tracking

Supported Python Versions

  • Python 3.10+
  • Python 3.11
  • Python 3.12
  • Python 3.13

Requirements

  • velatir>=1.0.0 - Velatir Python SDK
  • langchain-core>=0.3.0 - LangChain core library
  • pydantic>=2.0.0 - Data validation

I