LangChain Policy Guardrails
PolicyAware can wrap chain-style LLM calls with policy enforcement before model execution.
Install
pip install policyaware
Run The Example
git clone https://github.com/ktirupati/policyaware.git
cd policyaware/examples/langchain-policy-guardrails
python chain_demo.py
Expected Output
safe_prompt: model_called=True decision=allow
pii_prompt: model_called=True decision=conditional_allow actions=redact
secret_prompt: model_called=False decision=deny
Drop-In Callback Example
For an existing LangChain pipeline, use the lightweight callback handler to collect PolicyAware governance results without changing the model provider.
from policyaware.integrations.langchain import PolicyAwareCallbackHandler
policyaware_callback = PolicyAwareCallbackHandler(config="policyaware.yaml")
response = chain.invoke(
{"question": "Summarize this customer ticket."},
config={"callbacks": [policyaware_callback]},
)
result = policyaware_callback.last_result
print(result.policy_decision.decision)
print(result.risk.tier)
print(result.output_findings.contains_sensitive)
Streaming Token Aggregation
handler = PolicyAwareCallbackHandler(config="policyaware.yaml")
handler.on_llm_start(prompts=["Email jane@example.com with the ticket summary."])
for token in ["Safe ", "summary ", "without ", "private ", "data."]:
handler.on_llm_new_token(token)
result = handler.on_llm_end()
print(result.to_dict())
The callback stores prompt checks, risk tier, policy decision, output leakage checks, runtime evals, and estimated token counts in last_result.
Open the complete example folder on GitHub: LangChain policy guardrails.
Read the full callback guide: LangChain and LlamaIndex callback integrations.