> ## 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

Three ways to protect LangChain in Python, from least to most code. For LangChain.js, the callback handler is in the [Node.js SDK guide](/guides/node-sdk).

## Auto-instrumentation

LangChain's chat models call the provider SDKs underneath, so [auto-instrumentation](/integrations/auto-instrumentation) 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"}
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

<Warning>
  `PromptGuardCallbackHandler` from `promptguard.integrations.langchain` does not run under current `langchain-core`, up to and including `promptguard-sdk` 2.3.0. It does not define the `ignore_*` attributes LangChain's callback manager reads before every event, so the first callback raises `AttributeError` and the call fails. Use the inline guard or auto-instrumentation above until a release fixes it.
</Warning>

## Proxy

Point the chat model at the [proxy](/integrations/providers/openai) 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"]},
)
```


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