LangChain
Track LangChain models, tools and retrievers with a callback handler.
Install
pip install "impraint-guard[langchain,gemini]"The handler itself only needs langchain-core (1.6.0 or newer). The gemini extra adds langchain-google-genai, which Vertex AI needs at version 4.0.0 or newer.
Attach the handler
Create an ImpraintGuardCallbackHandler from your Guard, then pass it as a callback.
import os
from impraint_guard import Guard
from impraint_guard.integrations.langchain import ImpraintGuardCallbackHandler
from langchain_google_genai import ChatGoogleGenerativeAI
guard = Guard(api_key=os.environ["IMPRAINT_API_KEY"], agent_id="langchain-agent")
handler = ImpraintGuardCallbackHandler(guard)
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash")
@guard.track
def ask(question: str) -> str:
result = llm.invoke(question, config={"callbacks": [handler]})
return result.contentPass callbacks at invoke time
Pass the handler in config={"callbacks": [handler]} when you invoke. That way it reaches every
nested run. Attaching it only when you construct a model covers just that one model.
Only inside @guard.track
The handler records steps onto the trace that is currently running. Outside a function decorated
with @guard.track, it silently does nothing.
What is tracked
- LLM calls: model, tokens and cost
- Tool calls: the tool name, input and output
- Retriever calls: the retriever and the query. Retrieved content is PII-scrubbed.
LangChain's internal chain steps are deliberately not tracked, because they would flood the trace with noise.
Prompt checks
By default, the handler also checks the text about to go into the model and reports what it finds (on_input="report"). To change this, pass on_input when you create it:
handler = ImpraintGuardCallbackHandler(guard, on_input="block") # stop the call if the prompt is found unsafe
handler = ImpraintGuardCallbackHandler(guard, on_input="off") # no prompt checksSee Content safety for what is checked and what each mode does.