@@ -196,24 +196,35 @@ def _set_response_attributes(span, response, token_histogram):
196
196
span , SpanAttributes .LLM_USAGE_TOTAL_TOKENS , usage .get ("total_tokens" )
197
197
)
198
198
set_span_attribute (
199
- span ,
200
- SpanAttributes .LLM_USAGE_COMPLETION_TOKENS , completion_tokens
201
- )
202
- set_span_attribute (
203
- span , SpanAttributes .LLM_USAGE_PROMPT_TOKENS , prompt_tokens
199
+ span , SpanAttributes .LLM_USAGE_COMPLETION_TOKENS , completion_tokens
204
200
)
201
+ set_span_attribute (span , SpanAttributes .LLM_USAGE_PROMPT_TOKENS , prompt_tokens )
205
202
206
- if isinstance (prompt_tokens , int ) and prompt_tokens >= 0 and token_histogram is not None :
207
- token_histogram .record (prompt_tokens , attributes = {
208
- SpanAttributes .LLM_TOKEN_TYPE : "input" ,
209
- SpanAttributes .LLM_RESPONSE_MODEL : response .get ("model" )
210
- })
203
+ if (
204
+ isinstance (prompt_tokens , int )
205
+ and prompt_tokens >= 0
206
+ and token_histogram is not None
207
+ ):
208
+ token_histogram .record (
209
+ prompt_tokens ,
210
+ attributes = {
211
+ SpanAttributes .LLM_TOKEN_TYPE : "input" ,
212
+ SpanAttributes .LLM_RESPONSE_MODEL : response .get ("model" ),
213
+ },
214
+ )
211
215
212
- if isinstance (completion_tokens , int ) and completion_tokens >= 0 and token_histogram is not None :
213
- token_histogram .record (completion_tokens , attributes = {
214
- SpanAttributes .LLM_TOKEN_TYPE : "output" ,
215
- SpanAttributes .LLM_RESPONSE_MODEL : response .get ("model" )
216
- })
216
+ if (
217
+ isinstance (completion_tokens , int )
218
+ and completion_tokens >= 0
219
+ and token_histogram is not None
220
+ ):
221
+ token_histogram .record (
222
+ completion_tokens ,
223
+ attributes = {
224
+ SpanAttributes .LLM_TOKEN_TYPE : "output" ,
225
+ SpanAttributes .LLM_RESPONSE_MODEL : response .get ("model" ),
226
+ },
227
+ )
217
228
218
229
choices = response .get ("choices" )
219
230
if should_send_prompts () and choices :
@@ -575,7 +586,7 @@ def _instrument(self, **kwargs):
575
586
token_histogram ,
576
587
choice_counter ,
577
588
duration_histogram ,
578
- ) = (None , None , None , None )
589
+ ) = (None , None , None )
579
590
580
591
for wrapped_method in WRAPPED_METHODS :
581
592
wrap_package = wrapped_method .get ("package" )
0 commit comments