ValGuard + LangChain: validate chat model completions

Point ChatOpenAI (or any OpenAI-compatible LangChain chat model) at ValGuard. Same agent slug and rule packs across Python services.

Last verified:

LangChain chat models accept an OpenAI-compatible base_url. Point that URL at ValGuard so every chain, tool-calling step, and agent loop shares the same rule packs and audit rows.

When you need this

A LangChain router picks the wrong tool because the model returned plausible JSON with an invalid enum. ValGuard blocks or re-asks on the completion before bind_tools sees the payload.

Setup

  1. Create a ValGuard agent per role (router, extractor, writer).
  2. Attach schema and policy packs; start in shadow mode.
  3. Configure ChatOpenAI (or equivalent) with base_url=https://api.valguard.ai/v1, your ValGuard API key, and default_headers={"X-VG-Agent": "<slug>"}.
  4. Enforce when shadow metrics look stable.

Code

import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="openai/gpt-4o-mini",
    base_url="https://api.valguard.ai/v1",
    api_key=os.environ["VG_API_KEY"],
    default_headers={"X-VG-Agent": "langchain-router"},
)

resp = llm.invoke("Classify: duplicate charge on invoice 4412")

Honest limits

  • ValGuard validates LLM completions on the path you route. It does not validate arbitrary Python tool code unless that tool calls back through ValGuard.
  • Block and re-ask rules buffer streaming completions. See Trust.
  • For graph state machines with explicit nodes, also see LangGraph.

Related

Next step

Wire shadow mode with the quickstart, then read LLM output validation.