11import json
2+ import openai
23from typing import Any , Dict , List , Optional , Callable , Awaitable , Union
34from langtrace .trace_attributes import (
45 LLMSpanAttributes ,
4041)
4142
4243
44+ def filter_valid_attributes (attributes ):
45+ """Filter attributes where value is not None, not an empty string, and not openai.NOT_GIVEN."""
46+ return {
47+ key : value
48+ for key , value in attributes .items ()
49+ if value is not None and value != openai .NOT_GIVEN and value != ""
50+ }
51+
52+
4353def images_generate (version : str , tracer : Tracer ) -> Callable :
4454 """
4555 Wrap the `generate` method of the `Images` class to trace it.
@@ -57,7 +67,7 @@ def traced_method(
5767 ** get_extra_attributes (), # type: ignore
5868 }
5969
60- attributes = LLMSpanAttributes (** span_attributes )
70+ attributes = LLMSpanAttributes (** filter_valid_attributes ( span_attributes ) )
6171
6272 with tracer .start_as_current_span (
6373 name = get_span_name (APIS ["IMAGES_GENERATION" ]["METHOD" ]),
@@ -118,7 +128,7 @@ async def traced_method(
118128 ** get_extra_attributes (), # type: ignore
119129 }
120130
121- attributes = LLMSpanAttributes (** span_attributes )
131+ attributes = LLMSpanAttributes (** filter_valid_attributes ( span_attributes ) )
122132
123133 with tracer .start_as_current_span (
124134 name = get_span_name (APIS ["IMAGES_GENERATION" ]["METHOD" ]),
@@ -181,7 +191,7 @@ def traced_method(
181191 ** get_extra_attributes (), # type: ignore
182192 }
183193
184- attributes = LLMSpanAttributes (** span_attributes )
194+ attributes = LLMSpanAttributes (** filter_valid_attributes ( span_attributes ) )
185195
186196 with tracer .start_as_current_span (
187197 name = APIS ["IMAGES_EDIT" ]["METHOD" ],
@@ -268,7 +278,7 @@ def traced_method(
268278 ** get_extra_attributes (), # type: ignore
269279 }
270280
271- attributes = LLMSpanAttributes (** span_attributes )
281+ attributes = LLMSpanAttributes (** filter_valid_attributes ( span_attributes ) )
272282
273283 span = tracer .start_span (
274284 name = get_span_name (APIS ["CHAT_COMPLETION" ]["METHOD" ]),
@@ -356,7 +366,7 @@ async def traced_method(
356366 ** get_extra_attributes (), # type: ignore
357367 }
358368
359- attributes = LLMSpanAttributes (** span_attributes )
369+ attributes = LLMSpanAttributes (** filter_valid_attributes ( span_attributes ) )
360370
361371 span = tracer .start_span (
362372 name = get_span_name (APIS ["CHAT_COMPLETION" ]["METHOD" ]),
@@ -438,7 +448,7 @@ def traced_method(
438448 [kwargs .get ("input" , "" )]
439449 )
440450
441- attributes = LLMSpanAttributes (** span_attributes )
451+ attributes = LLMSpanAttributes (** filter_valid_attributes ( span_attributes ) )
442452
443453 with tracer .start_as_current_span (
444454 name = get_span_name (APIS ["EMBEDDINGS_CREATE" ]["METHOD" ]),
@@ -487,7 +497,7 @@ async def traced_method(
487497 ** get_extra_attributes (), # type: ignore
488498 }
489499
490- attributes = LLMSpanAttributes (** span_attributes )
500+ attributes = LLMSpanAttributes (** filter_valid_attributes ( span_attributes ) )
491501
492502 encoding_format = kwargs .get ("encoding_format" )
493503 if encoding_format is not None :
0 commit comments