Skip to main content

soulpolicy-langgraph

5 行给你的 LangGraph agent 加漂移监测。

安装

pip install soulpolicy soulpolicy-langgraph

用法

from langgraph.graph import StateGraph, END
from typing import TypedDict
from soulpolicy import Client
from soulpolicy_langgraph import drift_check_node, should_block_on_drift

class State(TypedDict, total=False):
    messages: list
    drift: dict | None

sp = Client(api_key="sk_live_xxx")

graph = StateGraph(State)
graph.add_node("agent", my_agent_step)
graph.add_node("drift", drift_check_node(sp, baseline="bl_xxx"))
graph.set_entry_point("agent")
graph.add_edge("agent", "drift")
graph.add_conditional_edges(
    "drift",
    should_block_on_drift("warn"),     # 路由谓词
    {"continue": "agent", "block": END},
)

5 行:1 个 client,1 个 node,1 个谓词,2 条边。其余照常写。

文本提取

默认从 state["messages"] 取最后一条 AI/assistant 消息。也支持 state["output"] / state["last_message"] 字符串。要自定义:

graph.add_node("drift", drift_check_node(
    sp,
    baseline="bl_xxx",
    state_text=lambda s: s["agent_reply"],
))

失败容忍

drift_check_node(sp, baseline="bl_xxx", on_error="skip")
# 调用失败时写入 state["drift"]=None + state["drift_error"]=str(e)
# 默认 "raise" 让 LangGraph retry/handle

路由

should_block_on_drift("warn") —— severity ≥ warn 路由到 "block"。可改:

should_block_on_drift("critical", continue_target="next", block_target="rollback")

Metadata

Release files for soulpolicy-langgraph 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for soulpolicy-langgraph 0.2.0
File Size Uploaded
soulpolicy_langgraph-0.2.0.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for soulpolicy-langgraph 0.2.0
File Interpreter ABI Platform
soulpolicy_langgraph-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.2 kB

Release files / soulpolicy_langgraph-0.2.0.tar.gz

Download URL soulpolicy_langgraph-0.2.0.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2cababda32bda0dc42aed82e42cfa86b3d9a0a016f4202eb57e0f66161d12369
BLAKE2b-256 checksum
How to use checksums
fa0630f25011aa97732fb1b6c1c2ce9a866606535bea8eed7df6ea95c7858a58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / soulpolicy_langgraph-0.2.0-py3-none-any.whl

Download URL soulpolicy_langgraph-0.2.0-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69d2a84e45971fae4e1cb40c544bdc461d1cbc37a04b6cb0eaccd9e1aa90b52b
BLAKE2b-256 checksum
How to use checksums
30883783350630935a5b240975819bc9ec59bd8f2fb9136d3a758e127a391d89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page