Langchain
Learn about using Sentry for Langchain.
Beta
The support for LangChain is in its beta phase.
We are working on supporting different AI libraries (see GitHub discussion).
If you want to try the beta features and are willing to give feedback, please let us know on Discord.
This integration connects Sentry with Langchain. The integration has been confirmed to work with Langchain 0.1.11.
Install sentry-sdk
from PyPI and the appropriate langchain packages:
pip install --upgrade 'sentry-sdk' 'langchain-openai' 'langchain-core'
If you have the langchain
package in your dependencies, the Langchain integration will be enabled automatically when you initialize the Sentry SDK.
An additional dependency, tiktoken
, is required to be installed if you want to calculate token usage for streaming chat responses.
from langchain_openai import ChatOpenAI
import sentry_sdk
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
enable_tracing=True,
traces_sample_rate=1.0,
send_default_pii=True, # send personally-identifiable information like LLM responses to sentry
)
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
Verify that the integration works by inducing an error:
from langchain_openai import ChatOpenAI
import sentry_sdk
sentry_sdk.init(...) # same as above
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0, api_key="bad API key")
with sentry_sdk.start_transaction(op="ai-inference", name="The result of the AI inference"):
response = llm.invoke([("system", "What is the capital of paris?")])
print(response)
After running this script, a transaction will be created in the Performance section of sentry.io. Additionally, an error event (about the bad API key) will be sent to sentry.io and will be connected to the transaction.
It may take a couple of moments for the data to appear in sentry.io.
The Langchain integration will connect Sentry with Langchain and automatically monitor all LLM, tool, and function calls.
All exceptions in the execution of the chain are reported.
Sentry considers LLM and tokenizer inputs/outputs as PII and, by default, does not include PII data. If you want include the data, then set
send_default_pii=True
in thesentry_sdk.init()
call. To explicitly exclude prompts and outputs despitesend_default_pii=True
, configure the integration withinclude_prompts=False
as shown in the Options section below.
By adding LangchainIntegration
to your sentry_sdk.init()
call explicitly, you can set options for LangchainIntegration
to change its behavior:
import sentry_sdk
from sentry_sdk.integrations.langchain import LangchainIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
enable_tracing=True,
send_default_pii=True,
traces_sample_rate=1.0,
integrations = [
LangchainIntegration(
include_prompts=False, # LLM/tokenizer inputs/outputs will be not sent to Sentry, despite send_default_pii=True
),
],
)
- Langchain: 0.1.11+
- tiktoken: 0.6.0+
- Python: 3.9+
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").