Skip to content

Add Guardrail Sandwich LangGraph + LangChain reference implementations#1

Merged
huangjia2019 merged 6 commits into
mainfrom
feat/guardrail-sandwich-reference-impl
Jun 10, 2026
Merged

Add Guardrail Sandwich LangGraph + LangChain reference implementations#1
huangjia2019 merged 6 commits into
mainfrom
feat/guardrail-sandwich-reference-impl

Conversation

@webup

@webup webup commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add LangGraph StateGraph and LangChain middleware tutorial notebooks for the Guardrail Sandwich pattern — the first of 28 planned reference implementations
  • Add shared infrastructure: model_config.py (ernie provider via langchain-dev-utils), hooks.py (shared hook factories), .env.example, REFERENCE_IMPL.md
  • Both notebooks are committed with executed outputs, HTML exports, and Markdown exports for GitHub viewing

What's in each commit

  1. Project setup — pyproject.toml deps (langgraph optional group + dev), .env.example with AI Studio default, model_config.py with register_model_provider + load_chat_model
  2. Shared hookshooks.py with HookResult/HookPhase enums + 3 hook factories (amount_threshold, blocklist, output_schema), imported by both notebooks
  3. LangGraph notebook — StateGraph with pre_hooks/execute_tool/post_hooks nodes, conditional routing, ToolNode, 4 mock scenarios + real LLM run, graph visualization
  4. LangChain notebook — @wrap_tool_call decorator + GuardrailSandwichMiddleware class, both tested with create_agent + live ernie-5.1, agent graph visualization
  5. REFERENCE_IMPL.md — overview doc with implementation status, 8-wave roadmap, coming-soon items (Agent Chat UI, Agent Service Runtime)
  6. CI fix — drop Python 3.10 from matrix to match requires-python >= 3.11

Design decisions

  • Notebooks as the primary artifact — no separate pattern.py for the framework implementations; everything inline for teaching
  • Shared hook factorieshooks.py ensures both notebooks use identical hook definitions
  • Official APIs only — LangChain notebook uses wrap_tool_call, AgentMiddleware, create_agent from langchain.agents.middleware; no homebrew types
  • langchain-dev-utils for model registration — register_model_provider("ernie", "openai-compatible", base_url=...) + load_chat_model("ernie:ernie-5.1")
  • Hook design aligned — same factory signatures, same config dict shape, same scenarios across both notebooks

Test plan

  • CI passes — Python 3.11 + 3.12 (pure-Python tests + examples)
  • uv sync --extra langgraph --extra dev succeeds
  • uv run pytest --nbmake --nbmake-timeout=120 action/d-guardrail-sandwich/langgraph/tutorial.ipynb action/d-guardrail-sandwich/langchain/tutorial.ipynb — both pass
  • uv run pytest --import-mode=importlib -v — existing pure-Python tests still pass
  • HTML exports render correctly in browser
  • Markdown exports render on GitHub

webup added 6 commits June 10, 2026 17:50
… 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.
@webup webup requested a review from huangjia2019 June 10, 2026 13:07
@huangjia2019

Copy link
Copy Markdown
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:

  • Shared hooks.py + model_config.py is the right architectural call. Keeping the hook factories identical across the LangGraph and LangChain notebooks means readers can compare frameworks without wondering whether differences come from the pattern or from implementation noise.
  • Dual-framework reference (StateGraph + middleware) in one folder is the teaching format I was hoping for. The visible graph vs. middleware-plugged-into-create_agent contrast does a lot of work for the reader.
  • Official APIs only — no homebrew types is exactly the standard ADPS needs to anchor against. Anyone reading this should be able to map directly to upstream LangChain / LangGraph docs.
  • The 8-wave roadmap + the Agent Chat UI / Agent Service Runtime "coming soon" note give the repo a future trajectory beyond static reference code. Both very welcome.

CI green on 3.11 / 3.12, mergeable clean. Squashing and merging now. Looking forward to Wave 1.

—— 顺便:感谢海立老师亲自给 ADPS 第一份代码 reference 立下这个标杆。这一 PR 会作为 ADPS-CORE 资产收录进 Master 控制板。

@huangjia2019 huangjia2019 merged commit ba141a9 into main Jun 10, 2026
2 checks passed
@huangjia2019 huangjia2019 deleted the feat/guardrail-sandwich-reference-impl branch June 10, 2026 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants