Skip to main content

Datagusto safety middleware for LangChain/LangGraph agents

Project description

Datagusto SDK (Safety Middleware)

DatagustoSafetyMiddleware is a LangChain/LangGraph middleware that:

  • Extracts tool definitions from requests and (optionally) registers them to your Datagusto backend.
  • Submits user instructions for alignment.
  • Calls safety validation before and after tool execution, blocking execution when the backend returns should_proceed = false.

This package is published as datagusto-sdk and exposes DatagustoSafetyMiddleware.

Features

  • Tool definition extraction (names, descriptions, JSON schemas for input/output).
  • Automatic registration diffing (skips duplicate payloads).
  • Alignment submission for the latest user instruction.
  • Tool-level validation on start/end with structured payloads.
  • Verbose logging for request/response bodies (optional).

Requirements

  • Python >= 3.12
  • Dependencies: langchain, langgraph

Installation

pip install datagusto-sdk

Quickstart (LangChain agent)

from langchain.agents import create_agent
from langchain_core.tools import tool
from langchain.messages import HumanMessage
from datagusto_sdk import DatagustoSafetyMiddleware

@tool
def ping(msg: str) -> str:
    return f"pong: {msg}"

middleware = DatagustoSafetyMiddleware(
    verbose=True,
    include_schema=True,
    tool_filter=None,  # optional: list of tool names to register
)

agent = create_agent(
    model="claude-3-5-sonnet-latest",
    tools=[ping],
    middleware=[middleware],
)

agent.invoke({"messages": [HumanMessage("ping please")]})

Configuration

  • Environment variables (read automatically if args not provided):
    • SERVER_URL: Base URL for Datagusto backend (e.g., https://api.example.com)
    • API_KEY: Bearer token for API calls
  • Runtime options:
    • verbose (bool): log request/response bodies; default True.
    • include_schema (bool): include JSON schemas for tool IO; default True.
    • tool_filter (list[str] | None): only register tools with names in this list.

Behavior overview

  1. wrap_model_call
    • Extract tools from request.tools, build payload, hash it.
    • If server_url and api_key are set and payload hash changed, call register API.
    • Extract latest human message and (if changed) post alignment; store session_id for validation.
  2. wrap_tool_call
    • On each tool call, if session_id exists, call validate API with on_start.
    • If backend responds should_proceed = false, raises RuntimeError to block.
    • After tool runs, call validate API with on_end; may block further processing similarly.

Error handling

  • Network / HTTP errors are logged (when verbose=True) but do not crash the agent unless the backend explicitly blocks via should_proceed = false.
  • Validation blocks raise RuntimeError with the backend response for transparency.

License

MIT License. See LICENSE.

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

datagusto_sdk-0.1.7.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

datagusto_sdk-0.1.7-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file datagusto_sdk-0.1.7.tar.gz.

File metadata

  • Download URL: datagusto_sdk-0.1.7.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for datagusto_sdk-0.1.7.tar.gz
Algorithm Hash digest
SHA256 46d9eb57529adb4a30dfcb49216d6bb6dfaa4a7976dc2223b14a5a23791cd173
MD5 343683bd563d51bbe15adfc12934b7f5
BLAKE2b-256 16084edeaa8ef7453197d72f081acfd07f95d608d1131f851dca8e885fb2ed8d

See more details on using hashes here.

File details

Details for the file datagusto_sdk-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: datagusto_sdk-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for datagusto_sdk-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 8d96c867248919b0acedf90fc84977e87b501703af9e4f222f62a034f67a1567
MD5 48d015d5ab2863cf19f527f14ab60c5f
BLAKE2b-256 21b7dfc5952a08e8f0c443fdcad79839eb3e0a52f8d8d17ded77a20c6f6f6f28

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