Skip to content

Commit 3396a39

Browse files
committed
openai-v2: handle choice count and stop sequences attributes
1 parent 46a0d1d commit 3396a39

File tree

4 files changed

+369
-0
lines changed

4 files changed

+369
-0
lines changed

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,19 @@ def get_llm_request_attributes(
222222
}
223223
)
224224

225+
if (choice_count := kwargs.get("n")) is not None:
226+
if isinstance(choice_count, int) and choice_count != 1:
227+
attributes[GenAIAttributes.GEN_AI_REQUEST_CHOICE_COUNT] = (
228+
choice_count
229+
)
230+
231+
if (stop_sequences := kwargs.get("stop")) is not None:
232+
if isinstance(stop_sequences, str):
233+
stop_sequences = [stop_sequences]
234+
attributes[GenAIAttributes.GEN_AI_REQUEST_STOP_SEQUENCES] = (
235+
stop_sequences
236+
)
237+
225238
if (response_format := kwargs.get("response_format")) is not None:
226239
# response_format may be string or object with a string in the `type` key
227240
if isinstance(response_format, Mapping):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
interactions:
2+
- request:
3+
body: |-
4+
{
5+
"messages": [
6+
{
7+
"role": "user",
8+
"content": "Say this is a test"
9+
}
10+
],
11+
"model": "gpt-4o-mini",
12+
"stop": "stop"
13+
}
14+
headers:
15+
Accept:
16+
- application/json
17+
Accept-Encoding:
18+
- gzip, deflate
19+
Connection:
20+
- keep-alive
21+
Content-Length:
22+
- '105'
23+
Content-Type:
24+
- application/json
25+
Host:
26+
- api.openai.com
27+
User-Agent:
28+
- OpenAI/Python 1.109.1
29+
X-Stainless-Arch:
30+
- x64
31+
X-Stainless-Async:
32+
- 'false'
33+
X-Stainless-Lang:
34+
- python
35+
X-Stainless-OS:
36+
- Linux
37+
X-Stainless-Package-Version:
38+
- 1.109.1
39+
X-Stainless-Runtime:
40+
- CPython
41+
X-Stainless-Runtime-Version:
42+
- 3.12.12
43+
authorization:
44+
- Bearer test_openai_api_key
45+
x-stainless-read-timeout:
46+
- '600'
47+
x-stainless-retry-count:
48+
- '0'
49+
method: POST
50+
uri: https://api.openai.com/v1/chat/completions
51+
response:
52+
body:
53+
string: |-
54+
{
55+
"id": "chatcmpl-Clubs1bbZwGUeDKpnPUWDMEhSbquh",
56+
"object": "chat.completion",
57+
"created": 1765535060,
58+
"model": "gpt-4o-mini-2024-07-18",
59+
"choices": [
60+
{
61+
"index": 0,
62+
"message": {
63+
"role": "assistant",
64+
"content": "This is a test. How can I assist you further?",
65+
"refusal": null,
66+
"annotations": []
67+
},
68+
"logprobs": null,
69+
"finish_reason": "stop"
70+
}
71+
],
72+
"usage": {
73+
"prompt_tokens": 12,
74+
"completion_tokens": 12,
75+
"total_tokens": 24,
76+
"prompt_tokens_details": {
77+
"cached_tokens": 0,
78+
"audio_tokens": 0
79+
},
80+
"completion_tokens_details": {
81+
"reasoning_tokens": 0,
82+
"audio_tokens": 0,
83+
"accepted_prediction_tokens": 0,
84+
"rejected_prediction_tokens": 0
85+
}
86+
},
87+
"service_tier": "default",
88+
"system_fingerprint": "fp_11f3029f6b"
89+
}
90+
headers:
91+
CF-RAY:
92+
- 9acc82e96fde4bf3-MXP
93+
Connection:
94+
- keep-alive
95+
Content-Type:
96+
- application/json
97+
Date:
98+
- Fri, 12 Dec 2025 10:24:20 GMT
99+
Server:
100+
- cloudflare
101+
Set-Cookie: test_set_cookie
102+
Strict-Transport-Security:
103+
- max-age=31536000; includeSubDomains; preload
104+
Transfer-Encoding:
105+
- chunked
106+
X-Content-Type-Options:
107+
- nosniff
108+
access-control-expose-headers:
109+
- X-Request-ID
110+
alt-svc:
111+
- h3=":443"; ma=86400
112+
cf-cache-status:
113+
- DYNAMIC
114+
content-length:
115+
- '852'
116+
openai-organization: test_openai_org_id
117+
openai-processing-ms:
118+
- '500'
119+
openai-project:
120+
- proj_Pf1eM5R55Z35wBy4rt8PxAGq
121+
openai-version:
122+
- '2020-10-01'
123+
x-envoy-upstream-service-time:
124+
- '826'
125+
x-openai-proxy-wasm:
126+
- v0.1
127+
x-ratelimit-limit-requests:
128+
- '10000'
129+
x-ratelimit-limit-tokens:
130+
- '10000000'
131+
x-ratelimit-remaining-requests:
132+
- '9999'
133+
x-ratelimit-remaining-tokens:
134+
- '9999993'
135+
x-ratelimit-reset-requests:
136+
- 6ms
137+
x-ratelimit-reset-tokens:
138+
- 0s
139+
x-request-id:
140+
- req_993163a7581641b7b9aee6e03ef4ca3a
141+
status:
142+
code: 200
143+
message: OK
144+
version: 1
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
interactions:
2+
- request:
3+
body: |-
4+
{
5+
"messages": [
6+
{
7+
"role": "user",
8+
"content": "Say this is a test"
9+
}
10+
],
11+
"model": "gpt-4o-mini",
12+
"n": 1
13+
}
14+
headers:
15+
Accept:
16+
- application/json
17+
Accept-Encoding:
18+
- gzip, deflate
19+
Connection:
20+
- keep-alive
21+
Content-Length:
22+
- '97'
23+
Content-Type:
24+
- application/json
25+
Host:
26+
- api.openai.com
27+
User-Agent:
28+
- OpenAI/Python 1.109.1
29+
X-Stainless-Arch:
30+
- x64
31+
X-Stainless-Async:
32+
- 'false'
33+
X-Stainless-Lang:
34+
- python
35+
X-Stainless-OS:
36+
- Linux
37+
X-Stainless-Package-Version:
38+
- 1.109.1
39+
X-Stainless-Runtime:
40+
- CPython
41+
X-Stainless-Runtime-Version:
42+
- 3.12.12
43+
authorization:
44+
- Bearer test_openai_api_key
45+
x-stainless-read-timeout:
46+
- '600'
47+
x-stainless-retry-count:
48+
- '0'
49+
method: POST
50+
uri: https://api.openai.com/v1/chat/completions
51+
response:
52+
body:
53+
string: |-
54+
{
55+
"id": "chatcmpl-ClubqNLub25QPdqxjOslny04PLCYZ",
56+
"object": "chat.completion",
57+
"created": 1765535058,
58+
"model": "gpt-4o-mini-2024-07-18",
59+
"choices": [
60+
{
61+
"index": 0,
62+
"message": {
63+
"role": "assistant",
64+
"content": "This is a test. How can I assist you today?",
65+
"refusal": null,
66+
"annotations": []
67+
},
68+
"logprobs": null,
69+
"finish_reason": "stop"
70+
}
71+
],
72+
"usage": {
73+
"prompt_tokens": 12,
74+
"completion_tokens": 12,
75+
"total_tokens": 24,
76+
"prompt_tokens_details": {
77+
"cached_tokens": 0,
78+
"audio_tokens": 0
79+
},
80+
"completion_tokens_details": {
81+
"reasoning_tokens": 0,
82+
"audio_tokens": 0,
83+
"accepted_prediction_tokens": 0,
84+
"rejected_prediction_tokens": 0
85+
}
86+
},
87+
"service_tier": "default",
88+
"system_fingerprint": "fp_11f3029f6b"
89+
}
90+
headers:
91+
CF-RAY:
92+
- 9acc82ddddd4edba-MXP
93+
Connection:
94+
- keep-alive
95+
Content-Type:
96+
- application/json
97+
Date:
98+
- Fri, 12 Dec 2025 10:24:19 GMT
99+
Server:
100+
- cloudflare
101+
Set-Cookie: test_set_cookie
102+
Strict-Transport-Security:
103+
- max-age=31536000; includeSubDomains; preload
104+
Transfer-Encoding:
105+
- chunked
106+
X-Content-Type-Options:
107+
- nosniff
108+
access-control-expose-headers:
109+
- X-Request-ID
110+
alt-svc:
111+
- h3=":443"; ma=86400
112+
cf-cache-status:
113+
- DYNAMIC
114+
content-length:
115+
- '850'
116+
openai-organization: test_openai_org_id
117+
openai-processing-ms:
118+
- '405'
119+
openai-project:
120+
- proj_Pf1eM5R55Z35wBy4rt8PxAGq
121+
openai-version:
122+
- '2020-10-01'
123+
x-envoy-upstream-service-time:
124+
- '529'
125+
x-openai-proxy-wasm:
126+
- v0.1
127+
x-ratelimit-limit-requests:
128+
- '10000'
129+
x-ratelimit-limit-tokens:
130+
- '10000000'
131+
x-ratelimit-remaining-requests:
132+
- '9999'
133+
x-ratelimit-remaining-tokens:
134+
- '9999993'
135+
x-ratelimit-reset-requests:
136+
- 6ms
137+
x-ratelimit-reset-tokens:
138+
- 0s
139+
x-request-id:
140+
- req_2b15771f24a4465fb5fc43b797f39e04
141+
status:
142+
code: 200
143+
message: OK
144+
version: 1

0 commit comments

Comments
 (0)