Skip to main content

A wrapper for LangGraph agents that handles observability, memory, and guardrails

Project description

graph-wrap

A local-first Python library that provides zero-config PostgreSQL checkpointing and telemetry logging for LangGraph agents simply by replacing your standard StateGraph import.


Installation

Core Observability and Checkpointing

To install the core package with database checkpointing and telemetry logging:

pip install graph-wrap

With Optional UI Console

To install the package along with the Streamlit-based visualization console:

pip install "graph-wrap[ui]"

Quick Start

Replace your standard LangGraph StateGraph import with graph_wrap:

from typing import Dict, Any
from typing_extensions import TypedDict
from graph_wrap import StateGraph

class AgentState(TypedDict):
    messages: list[str]

def my_node(state: AgentState) -> Dict[str, Any]:
    return {"messages": ["hello"]}

db_uri = "postgresql://postgres:postgres@localhost:5432/my_database"
graph = StateGraph(AgentState, db_uri=db_uri)
graph.add_node("my_node", my_node)
graph.set_entry_point("my_node")
graph.set_finish_point("my_node")

compiled = graph.compile()

When you call compiled.ainvoke, the library dynamically setups Postgres checkpoint tables and logs execution traces (chains, tools, and LLMs) into the database under the configured thread_id.


Observability UI Console

If you installed graph-wrap with the ui extra, you can launch the console to inspect threads, visualize trace events, and view state checkpoints.

Run the console via python:

python -m graph_wrap --db-uri postgresql://postgres:postgres@localhost:5432/my_database

Or run the command line executable:

graph-wrap-ui --db-uri postgresql://postgres:postgres@localhost:5432/my_database

Custom Guardrails

graph-wrap provides custom inbound and outbound safety guardrails. Evaluations for prompt injection, content safety, and hallucination are performed using LangChain's native structured output bindings for Ollama and OpenAI. Local regex-based PII redaction runs locally with zero LLM costs.

Quick Example

from typing_extensions import TypedDict
from graph_wrap import StateGraph, GuardrailConfig, GuardrailProvider

class AgentState(TypedDict):
    messages: list[str]
    context: str

safety_config = GuardrailConfig(
    inbound_provider=GuardrailProvider.OLLAMA,
    safety_model="llama3.1:8b",
    check_prompt_injection=True,
    
    outbound_provider=GuardrailProvider.OLLAMA,
    eval_model="llama3.1:8b",
    check_hallucination=True,
    
    redact_pii=True,
    fallback_message="I cannot answer that, please try to rephrase your question."
)

workflow = StateGraph(
    AgentState, 
    db_uri="postgresql://postgres:postgres@localhost:5432/my_database",
    guardrails=safety_config
)

Node-Specific Guardrails

To apply guardrails to specific nodes only, pass a dictionary mapping node names to GuardrailConfig objects:

workflow = StateGraph(
    AgentState,
    db_uri="postgresql://postgres:postgres@localhost:5432/my_database",
    guardrails={
        "Trader": GuardrailConfig(
            outbound_provider=GuardrailProvider.OLLAMA,
            eval_model="llama3.1:8b",
            check_hallucination=True
        )
    }
)

Config Options

Parameter Type Default Description
inbound_provider GuardrailProvider None Provider for inbound validation (OLLAMA or OPENAI).
safety_model str None Model name to use for inbound safety/prompt injection checks.
check_prompt_injection bool False Enable inbound prompt injection and jailbreak detection.
outbound_provider GuardrailProvider None Provider for outbound validation (OLLAMA or OPENAI).
eval_model str None Model name to use for outbound hallucination or safety checks.
check_hallucination bool False Enable outbound hallucination check comparing node outputs to graph context.
redact_pii bool False Enable local regex-based redaction of emails, credit cards, SSNs, and phone numbers.
fallback_message Optional[str] "I cannot answer..." The message returned when a guardrail is tripped. Set to None to raise a GuardrailValidationError instead.

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

graph_abstract-0.1.0.tar.gz (35.0 kB view details)

Uploaded Source

Built Distribution

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

graph_abstract-0.1.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file graph_abstract-0.1.0.tar.gz.

File metadata

  • Download URL: graph_abstract-0.1.0.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for graph_abstract-0.1.0.tar.gz
Algorithm Hash digest
SHA256 324efec8197886cd9d44a1cb5633c939ed51119c555063fe54312c965e56b3bc
MD5 3047f46d56fd2fee45d0d526ff6b0fca
BLAKE2b-256 71d5818edbac71f0321424eed5fbe28cb8316bf0a71d7beb7718e7e5ed2e6884

See more details on using hashes here.

File details

Details for the file graph_abstract-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: graph_abstract-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for graph_abstract-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 966909d534a15150edcf9c948511b38971ba6fea7066ec54036e3c7507f90f5f
MD5 8d55de9383b7d1929006b29df5915bc0
BLAKE2b-256 d8e72a55054c7e54a1745f2a211553a9e713cad3d6d5fec7b66cd55ef3df9fb8

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