OpenAI
Learn about using Sentry for OpenAI.
Beta
The support for OpenAI 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 the OpenAI Python SDK. The integration has been confirmed to work with OpenAI 1.13.3.
Once you've installed this SDK, you can use Sentry LLM Monitoring, a Sentry dashboard that helps you understand what's going on with your AI pipelines.
Sentry LLM Monitoring will automatically collect information about prompts, tokens, and models from providers like OpenAI. Learn more about it here.
Install sentry-sdk
from PyPI with the openai
extra:
pip install --upgrade 'sentry-sdk[openai]'
If you have the openai
package in your dependencies, the OpenAI integration will be enabled automatically when you initialize the Sentry SDK.
An additional dependency, tiktoken
, is required if you want to calculate token usage for streaming chat responses.
import sentry_sdk
from openai import OpenAI
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
enable_tracing=True,
traces_sample_rate=1.0,
)
client = OpenAI()
Verify that the integration works by creating an AI pipeline. The resulting data should show up in your LLM monitoring dashboard.
import sentry_sdk
from sentry_sdk.ai.monitoring import ai_track
from openai import OpenAI
sentry_sdk.init(...) # same as above
client = OpenAI(api_key="(your OpenAI key)")
@ai_track("My AI pipeline")
def my_pipeline():
with sentry_sdk.start_transaction(op="ai-inference", name="The result of the AI inference"):
print(
client.chat.completions.create(
model="gpt-3.5", messages=[{"role": "system", "content": "say hello"}]
)
.choices[0]
.message.content
)
After running this script, a pipeline will be created in the LLM Monitoring section of the Sentry dashboard. The pipeline will have an associated OpenAI span for the chat.completions.create
operation.
It may take a couple of moments for the data to appear in sentry.io.
The OpenAI integration will connect Sentry with all supported OpenAI methods automatically.
All exceptions leading to an OpenAIException are reported.
The supported modules are currently
chat.completions.create
andembeddings.create
.Sentry considers LLM and tokenizer inputs/outputs as PII and doesn't include PII data by default. If you want to include the data, 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 OpenAIIntegration
to your sentry_sdk.init()
call explicitly, you can set options for OpenAIIntegration
to change its behavior:
import sentry_sdk
from sentry_sdk.integrations.openai import OpenAIIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
enable_tracing=True,
send_default_pii=True,
traces_sample_rate=1.0,
integrations = [
OpenAIIntegration(
include_prompts=False, # LLM/tokenizer inputs/outputs will be not sent to Sentry, despite send_default_pii=True
),
],
)
- OpenAI: 1.0+
- 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").