@@ -164,7 +164,7 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
164164
165165 if self .event_kind == "log" :
166166 _send_log_events_from_messages (self .event_logger , messages = messages , attributes = event_attributes )
167- else :
167+ elif span . is_recording () :
168168 try :
169169 span .add_event (EVENT_GEN_AI_CONTENT_PROMPT , attributes = {GEN_AI_PROMPT : json .dumps (messages )})
170170 except TypeError :
@@ -195,7 +195,8 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
195195
196196 logger .debug (f"openai.resources.chat.completions.Completions.create result: { result } " )
197197
198- _set_span_attributes_from_response (span , result .id , result .model , result .choices , result .usage )
198+ if span .is_recording ():
199+ _set_span_attributes_from_response (span , result .id , result .model , result .choices , result .usage )
199200
200201 _record_token_usage_metrics (self .token_usage_metric , span , result .usage )
201202 _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
@@ -205,7 +206,7 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
205206 _send_log_events_from_choices (
206207 self .event_logger , choices = result .choices , attributes = event_attributes
207208 )
208- else :
209+ elif span . is_recording () :
209210 # same format as the prompt
210211 completion = [_message_from_choice (choice ) for choice in result .choices ]
211212 try :
@@ -238,7 +239,7 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
238239
239240 if self .event_kind == "log" :
240241 _send_log_events_from_messages (self .event_logger , messages = messages , attributes = event_attributes )
241- else :
242+ elif span . is_recording () :
242243 try :
243244 span .add_event (EVENT_GEN_AI_CONTENT_PROMPT , attributes = {GEN_AI_PROMPT : json .dumps (messages )})
244245 except TypeError :
@@ -269,7 +270,8 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
269270
270271 logger .debug (f"openai.resources.chat.completions.AsyncCompletions.create result: { result } " )
271272
272- _set_span_attributes_from_response (span , result .id , result .model , result .choices , result .usage )
273+ if span .is_recording ():
274+ _set_span_attributes_from_response (span , result .id , result .model , result .choices , result .usage )
273275
274276 _record_token_usage_metrics (self .token_usage_metric , span , result .usage )
275277 _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
@@ -279,7 +281,7 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
279281 _send_log_events_from_choices (
280282 self .event_logger , choices = result .choices , attributes = event_attributes
281283 )
282- else :
284+ elif span . is_recording () :
283285 # same format as the prompt
284286 completion = [_message_from_choice (choice ) for choice in result .choices ]
285287 try :
@@ -314,7 +316,8 @@ def _embeddings_wrapper(self, wrapped, instance, args, kwargs):
314316 _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
315317 raise
316318
317- _set_embeddings_span_attributes_from_response (span , result .model , result .usage )
319+ if span .is_recording ():
320+ _set_embeddings_span_attributes_from_response (span , result .model , result .usage )
318321
319322 _record_token_usage_metrics (self .token_usage_metric , span , result .usage )
320323 _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
@@ -344,7 +347,8 @@ async def _async_embeddings_wrapper(self, wrapped, instance, args, kwargs):
344347 _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
345348 raise
346349
347- _set_embeddings_span_attributes_from_response (span , result .model , result .usage )
350+ if span .is_recording ():
351+ _set_embeddings_span_attributes_from_response (span , result .model , result .usage )
348352
349353 _record_token_usage_metrics (self .token_usage_metric , span , result .usage )
350354 _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
0 commit comments