PalveronPalveronDocs

LangChain

AI governance for LangChain pipelines — automatic policy checks, PII masking, and audit trails for every LLM call, tool invocation, and chain run.

LangChain Integration

Add a callback handler to your LangChain pipeline. Every prompt, tool call, and LLM output is automatically checked against your Palveron governance policies.

Installation

pip install palveron-langchain

Quickstart

from palveron_langchain import PalveronCallbackHandler
from langchain_openai import ChatOpenAI

handler = PalveronCallbackHandler(api_key="pv_live_xxx")
llm = ChatOpenAI(model="gpt-4o", callbacks=[handler])

# Every call is now governed
result = llm.invoke("Summarize the customer record for John Doe, [email protected]")
# → PalveronGovernanceError: Blocked — PII detected (email address)

What Is Checked

EventWhenWhat
on_llm_startBefore text completionPrompt text
on_chat_model_startBefore chat model callAll messages
on_tool_startBefore tool executionTool name + input
on_llm_endAfter generation (opt-in)LLM output text

Configuration

handler = PalveronCallbackHandler(
    api_key="pv_live_xxx",
    base_url="https://gateway.internal.company.com:8080",  # on-prem
    check_prompts=True,       # check inputs before LLM (default)
    check_outputs=False,      # check LLM outputs (opt-in)
    check_tools=True,         # check tool inputs (default)
    raise_on_block=True,      # raise on BLOCKED (default)
    fail_open=False,          # block on gateway outage (default)
    metadata={"team": "ml"},  # additional metadata per trace
)

With Agents

from langchain.agents import create_react_agent, AgentExecutor

handler = PalveronCallbackHandler(api_key="pv_live_xxx")
agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, callbacks=[handler])

# Both LLM calls AND tool executions are governed
result = executor.invoke({"input": "Delete all customer records"})

With LCEL Chains

from langchain_core.prompts import ChatPromptTemplate

handler = PalveronCallbackHandler(api_key="pv_live_xxx")
chain = ChatPromptTemplate.from_template("Summary: {text}") | llm

result = chain.invoke(
    {"text": sensitive_document},
    config={"callbacks": [handler]},
)

Governance Records

Programmatic access to the audit trail after execution:

print(f"Blocked: {handler.blocked_count}")
print(f"Trace IDs: {handler.trace_ids}")

for record in handler.records:
    print(f"{record.event}: {record.decision} ({record.latency_ms:.0f}ms)")

Error Handling

from palveron_langchain import PalveronCallbackHandler, PalveronGovernanceError

try:
    result = llm.invoke("Send SSN 12 345 678 A 90 to the customer")
except PalveronGovernanceError as e:
    print(e.decision)   # "BLOCKED"
    print(e.trace_id)   # "trc_abc123"
    print(e.reason)     # "PII detected: social security number"

Limitations

LangChain callbacks are observational — they cannot modify prompts in flight. The handler can block requests (by raising an exception) and log modifications, but cannot rewrite the actual prompt text before it reaches the LLM.

For full input rewriting with PII masking before the model sees the data, use the Palveron Gateway Proxy.

Source Code

Open source (MIT): github.com/palveron/adapter-langchain.

Next Steps

On this page