|
14 | 14 |
|
15 | 15 | from agentops.logging import logger |
16 | 16 | from agentops.instrumentation.common.wrappers import _with_tracer_wrapper |
| 17 | +from agentops.instrumentation.openai.utils import is_metrics_enabled |
17 | 18 | from agentops.instrumentation.openai.wrappers.chat import handle_chat_attributes |
18 | 19 | from agentops.semconv import SpanAttributes, LLMRequestTypeValues, MessageAttributes |
19 | 20 |
|
@@ -195,22 +196,22 @@ def _finalize_stream(self) -> None: |
195 | 196 | if len(self._tool_calls) > 0: |
196 | 197 | for idx, tool_call in self._tool_calls.items(): |
197 | 198 | # Only set attributes if values are not None |
198 | | - if tool_call["id"]: |
| 199 | + if tool_call["id"] is not None: |
199 | 200 | self._span.set_attribute( |
200 | 201 | MessageAttributes.COMPLETION_TOOL_CALL_ID.format(i=0, j=idx), tool_call["id"] |
201 | 202 | ) |
202 | 203 |
|
203 | | - if tool_call["type"]: |
| 204 | + if tool_call["type"] is not None: |
204 | 205 | self._span.set_attribute( |
205 | 206 | MessageAttributes.COMPLETION_TOOL_CALL_TYPE.format(i=0, j=idx), tool_call["type"] |
206 | 207 | ) |
207 | 208 |
|
208 | | - if tool_call["function"]["name"]: |
| 209 | + if tool_call["function"]["name"] is not None: |
209 | 210 | self._span.set_attribute( |
210 | 211 | MessageAttributes.COMPLETION_TOOL_CALL_NAME.format(i=0, j=idx), tool_call["function"]["name"] |
211 | 212 | ) |
212 | 213 |
|
213 | | - if tool_call["function"]["arguments"]: |
| 214 | + if tool_call["function"]["arguments"] is not None: |
214 | 215 | self._span.set_attribute( |
215 | 216 | MessageAttributes.COMPLETION_TOOL_CALL_ARGUMENTS.format(i=0, j=idx), |
216 | 217 | tool_call["function"]["arguments"], |
@@ -339,6 +340,19 @@ def chat_completion_stream_wrapper(tracer, wrapped, instance, args, kwargs): |
339 | 340 | for key, value in request_attributes.items(): |
340 | 341 | span.set_attribute(key, value) |
341 | 342 |
|
| 343 | + # Add include_usage to get token counts for streaming responses |
| 344 | + if is_streaming and is_metrics_enabled(): |
| 345 | + # Add stream_options if it doesn't exist |
| 346 | + if "stream_options" not in kwargs: |
| 347 | + kwargs["stream_options"] = {"include_usage": True} |
| 348 | + logger.debug("[OPENAI WRAPPER] Adding stream_options.include_usage=True to get token counts") |
| 349 | + # If stream_options exists but doesn't have include_usage, add it |
| 350 | + elif isinstance(kwargs["stream_options"], dict) and "include_usage" not in kwargs["stream_options"]: |
| 351 | + kwargs["stream_options"]["include_usage"] = True |
| 352 | + logger.debug( |
| 353 | + "[OPENAI WRAPPER] Adding include_usage=True to existing stream_options to get token counts" |
| 354 | + ) |
| 355 | + |
342 | 356 | # Call the original method |
343 | 357 | response = wrapped(*args, **kwargs) |
344 | 358 |
|
@@ -395,6 +409,15 @@ async def async_chat_completion_stream_wrapper(tracer, wrapped, instance, args, |
395 | 409 | for key, value in request_attributes.items(): |
396 | 410 | span.set_attribute(key, value) |
397 | 411 |
|
| 412 | + # Add include_usage to get token counts for streaming responses |
| 413 | + if is_streaming and is_metrics_enabled(): |
| 414 | + # Add stream_options if it doesn't exist |
| 415 | + if "stream_options" not in kwargs: |
| 416 | + kwargs["stream_options"] = {"include_usage": True} |
| 417 | + # If stream_options exists but doesn't have include_usage, add it |
| 418 | + elif isinstance(kwargs["stream_options"], dict) and "include_usage" not in kwargs["stream_options"]: |
| 419 | + kwargs["stream_options"]["include_usage"] = True |
| 420 | + |
398 | 421 | # Call the original method |
399 | 422 | response = await wrapped(*args, **kwargs) |
400 | 423 |
|
|
0 commit comments