Add Guardrail Sandwich LangGraph + LangChain reference implementations#1
Merged
Merged
Conversation
… loader - Bump requires-python to >=3.11 (langchain-oceanbase requirement) - Add [langgraph] optional deps: langchain, langgraph, langchain-openai, langchain-anthropic, langchain-oceanbase, langchain-dev-utils, python-dotenv - Add dev deps: nbmake, jupyterlab, ipykernel - Add .env.example with MODEL_PROVIDER/MODEL_NAME env vars, default ernie:ernie-5.1 via AI Studio, with key URLs for AI Studio, OpenAI, Anthropic, LangSmith - Add model_config.py: shared model loader using langchain-dev-utils register_model_provider + load_chat_model, with idempotent provider registration - Update .gitignore: seekdb_data.db, checkpoints.db, plan files
Shared by both langgraph/ and langchain/ notebooks:
- HookResult enum (PASS, BLOCK, WARN)
- HookPhase enum (PRE, POST) for langgraph's single-list design
- amount_threshold_hook, blocklist_hook, output_schema_hook factories
- Each returns a config dict with fn inlined: {name, fn, phase, priority, blocks, applies_to}
StateGraph implementation with explicit pre_hooks → execute_tool → post_hooks nodes and conditional routing. Includes: - SandwichState TypedDict with hook config, audit trail fields - run_single_hook dispatcher with fail-closed + shadow mode - ToolNode as the middle slice, graph visualization via draw_mermaid_png - 4 mock scenarios: pass, threshold block, blocklist block, bad receipt - Real backend with ernie-5.1 via model_config.py - Composing as subgraph section, further reading with full descriptions - HTML and Markdown exports committed with outputs
Middleware implementation using official langchain.agents.middleware API: - Style A: @wrap_tool_call decorator — single-function guard - Style B: GuardrailSandwichMiddleware subclassing AgentMiddleware - Both tested with create_agent + live LLM (ernie-5.1) - Agent graph visualization via draw_mermaid_png (CompiledStateGraph) - Shared hook factories imported from hooks.py - Stacking both styles, production shortcuts, further reading - HTML and Markdown exports committed with outputs
…tations Documents what's implemented (Guardrail Sandwich), the 8-wave roadmap for remaining 27 patterns, coming-soon items (Agent Chat UI, Agent Service Runtime), quick start guide, project structure, and test commands.
Owner
|
Excellent work, Haili — and an unusually strong opener for a 28-pattern series. A few things I want to put on record about this PR, since it sets the bar for the remaining 27:
CI green on 3.11 / 3.12, mergeable clean. Squashing and merging now. Looking forward to Wave 1. —— 顺便:感谢海立老师亲自给 ADPS 第一份代码 reference 立下这个标杆。这一 PR 会作为 ADPS-CORE 资产收录进 Master 控制板。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
StateGraphand LangChain middleware tutorial notebooks for the Guardrail Sandwich pattern — the first of 28 planned reference implementationsmodel_config.py(ernie provider via langchain-dev-utils),hooks.py(shared hook factories),.env.example,REFERENCE_IMPL.mdWhat's in each commit
hooks.pywith HookResult/HookPhase enums + 3 hook factories (amount_threshold, blocklist, output_schema), imported by both notebooksDesign decisions
pattern.pyfor the framework implementations; everything inline for teachinghooks.pyensures both notebooks use identical hook definitionswrap_tool_call,AgentMiddleware,create_agentfromlangchain.agents.middleware; no homebrew typesregister_model_provider("ernie", "openai-compatible", base_url=...)+load_chat_model("ernie:ernie-5.1")Test plan
uv sync --extra langgraph --extra devsucceedsuv run pytest --nbmake --nbmake-timeout=120 action/d-guardrail-sandwich/langgraph/tutorial.ipynb action/d-guardrail-sandwich/langchain/tutorial.ipynb— both passuv run pytest --import-mode=importlib -v— existing pure-Python tests still pass