> ## Documentation Index
> Fetch the complete documentation index at: https://docs.promptguard.co/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain

> Scan LangChain chains and agents with auto-instrumentation, an inline LCEL guard, or the PromptGuard proxy

Four ways to protect LangChain in Python. For LangChain.js, the callback handler is in the [Node.js SDK guide](https://docs.promptguard.co/guides/node-sdk.md).

## Auto-instrumentation

LangChain's chat models call the provider SDKs underneath, so [auto-instrumentation](https://docs.promptguard.co/integrations/auto-instrumentation.md) scans them with no LangChain-specific code. `ChatOpenAI` goes through the `openai` package; `ChatAnthropic` through `anthropic`.

```python theme={"system"}
import promptguard
promptguard.init()  # reads PROMPTGUARD_API_KEY; call before creating models

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-5-nano")
llm.invoke("Hello!")  # scanned; raises PromptGuardBlockedError on a block in enforce mode
```

## Inline guard in a chain

`PromptGuardRunnable` is a step you compose into an LCEL chain with `|`. Because it sits in the data flow, it can do what a callback cannot: on a **redact** decision it passes the redacted text on to the next step. On a block it raises `PromptGuardBlockedError`.

```bash theme={"system"}
# Installs the latest release
pip install "promptguard-sdk[langchain]"
```

```python theme={"system"}
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

from promptguard.integrations.langchain import PromptGuardRunnable

input_guard = PromptGuardRunnable().as_runnable()  # reads PROMPTGUARD_API_KEY
output_guard = PromptGuardRunnable(direction="output").as_runnable()

prompt = ChatPromptTemplate.from_template("Answer briefly: {question}")
chain = (
    {"question": input_guard}
    | prompt
    | ChatOpenAI(model="gpt-5-nano")
    | StrOutputParser()
    | output_guard
)
chain.invoke("What is prompt injection?")
```

A string, a dict with an `input`, `query`, `question`, `text`, `content` or `prompt` field, or a list of message dicts can be redacted in place. For any other value, a redact decision blocks in enforce mode rather than pass on content it could not rewrite. `mode="monitor"` logs instead of blocking or redacting.

## Callback handler

`PromptGuardCallbackHandler` scans each model call's prompt before it is sent and, unless you pass `scan_responses=False`, its response afterwards. It needs `promptguard-sdk` 2.4.0 or later: earlier releases failed under LangChain's callback manager.

```bash theme={"system"}
# Installs the latest release, and refuses one older than 2.4.0
pip install "promptguard-sdk[langchain]>=2.4.0"
```

```python theme={"system"}
from langchain_openai import ChatOpenAI
from promptguard.integrations.langchain import PromptGuardCallbackHandler

handler = PromptGuardCallbackHandler(mode="enforce")  # reads PROMPTGUARD_API_KEY

llm = ChatOpenAI(model="gpt-5-nano")
llm.invoke("Hello!", config={"callbacks": [handler]})  # raises PromptGuardBlockedError on a block
```

Pass it in `config={"callbacks": [...]}` on `invoke()` to cover every model call in a chain or agent run. A callback observes content but cannot change it, so in enforce mode a **redact** decision blocks; use the inline guard above when you want the redacted text passed on. A `thread_id`, `session_id` or `conversation_id` in the run's metadata is sent as the session id.

## Proxy

Point the chat model at the [proxy](https://docs.promptguard.co/integrations/providers/openai.md) instead. Nothing runs in your process, and it works the same in any language.

```python theme={"system"}
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-5-nano",
    base_url="https://api.promptguard.co/api/v1",
    api_key=os.environ["OPENAI_API_KEY"],  # forwarded to OpenAI
    default_headers={"X-API-Key": os.environ["PROMPTGUARD_API_KEY"]},
)
```

## Next steps

> [card] [LangChain agents cookbook](https://docs.promptguard.co/cookbooks/langchain-agents.md)
>
> Scan an agent's prompts, tool calls and chains

> [card] [LangGraph](https://docs.promptguard.co/integrations/frameworks/langgraph.md)
>
> Scan the model calls inside a graph

> [card] [Auto-instrumentation](https://docs.promptguard.co/integrations/auto-instrumentation.md)
>
> Which provider calls `init()` patches, and which it does not


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.