Skip to content

Commit 4ec6149

Browse files
feat(deepseek): add DeepSeek instrumentation with R1 reasoning_content support
Adds opentelemetry-instrumentation-deepseek package for tracing DeepSeek API calls. Closes #2597. DeepSeek has no native tracing support in OpenLLMetry. Users calling DeepSeek-R1 also lose visibility into reasoning_content (chain-of-thought output) which is not captured by the existing OpenAI instrumentation. - Patches openai.resources.chat.completions (DeepSeek is OpenAI-compatible) - Only activates when base_url contains "deepseek" via _is_deepseek_client() - Captures reasoning_content as gen_ai.deepseek.reasoning_content span attribute - Accumulates reasoning_content across streaming chunks - Follows the Groq instrumentation structure exactly
1 parent fc33f1c commit 4ec6149

26 files changed

Lines changed: 4315 additions & 71 deletions

.cz.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ version_files = [
1010
"packages/opentelemetry-instrumentation-mcp/opentelemetry/instrumentation/mcp/version.py",
1111
"packages/opentelemetry-instrumentation-groq/pyproject.toml:^version",
1212
"packages/opentelemetry-instrumentation-groq/opentelemetry/instrumentation/groq/version.py",
13+
"packages/opentelemetry-instrumentation-deepseek/pyproject.toml:^version",
14+
"packages/opentelemetry-instrumentation-deepseek/opentelemetry/instrumentation/deepseek/version.py",
1315
"packages/opentelemetry-instrumentation-agno/pyproject.toml:^version",
1416
"packages/opentelemetry-instrumentation-agno/opentelemetry/instrumentation/agno/version.py",
1517
"packages/opentelemetry-instrumentation-alephalpha/pyproject.toml:^version",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.10
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# OpenTelemetry DeepSeek Instrumentation
2+
3+
<a href="https://pypi.org/project/opentelemetry-instrumentation-deepseek/">
4+
<img src="https://badge.fury.io/py/opentelemetry-instrumentation-deepseek.svg" alt="PyPI version">
5+
</a>
6+
7+
This library allows tracing prompts and completions sent to [DeepSeek](https://platform.deepseek.com/) using the official [OpenAI SDK](https://github.com/openai/openai-python), since DeepSeek's API is OpenAI-compatible.
8+
9+
## Installation
10+
11+
```bash
12+
pip install opentelemetry-instrumentation-deepseek
13+
```
14+
15+
## Example usage
16+
17+
```python
18+
from opentelemetry.instrumentation.deepseek import DeepSeekInstrumentor
19+
20+
DeepSeekInstrumentor().instrument()
21+
```
22+
23+
This instrumentor patches `openai.resources.chat.completions.Completions.create` and `AsyncCompletions.create`, but only creates spans for requests made through a client configured with a DeepSeek `base_url`. Regular OpenAI clients are left untouched, so this instrumentation can be safely enabled alongside [opentelemetry-instrumentation-openai](https://pypi.org/project/opentelemetry-instrumentation-openai/).
24+
25+
```python
26+
from openai import OpenAI
27+
28+
client = OpenAI(
29+
api_key="<DEEPSEEK_API_KEY>",
30+
base_url="https://api.deepseek.com",
31+
)
32+
33+
response = client.chat.completions.create(
34+
model="deepseek-chat",
35+
messages=[{"role": "user", "content": "Hello!"}],
36+
)
37+
```
38+
39+
## DeepSeek-R1 reasoning content
40+
41+
The `deepseek-reasoner` model (DeepSeek-R1) returns its chain-of-thought in a `reasoning_content` field on the response message, in addition to the final `content`. This instrumentation captures `reasoning_content` as the `gen_ai.deepseek.reasoning_content` span attribute, for both streaming and non-streaming responses.
42+
43+
## Privacy
44+
45+
**By default, this instrumentation logs prompts, completions, and embeddings to span attributes**. This gives you a clear visibility into how your LLM application is working, and can make it easy to debug and evaluate the quality of the outputs.
46+
47+
However, you may want to disable this logging for privacy reasons, as they may contain highly sensitive data from your users. You may also simply want to reduce the size of your traces.
48+
49+
To disable logging, set the `TRACELOOP_TRACE_CONTENT` environment variable to `false`.
50+
51+
```bash
52+
TRACELOOP_TRACE_CONTENT=false
53+
```

0 commit comments

Comments
 (0)