@@ -102,6 +102,7 @@ def process_input(inp: ResponseInputParam) -> ResponseInputParam:
102
102
Process input parameters for OpenAI Responses API.
103
103
Ensures list inputs have proper type annotations for each item.
104
104
"""
105
+ logger .debug (f"process_input called with type: { type (inp )} , value: { inp [:100 ] if isinstance (inp , str ) else inp } " )
105
106
if not isinstance (inp , list ):
106
107
return inp
107
108
return [prepare_input_param (item ) for item in inp ]
@@ -366,6 +367,7 @@ def _process_chunk(self, chunk):
366
367
367
368
try :
368
369
parsed_chunk = parse_response (chunk )
370
+ logger .debug (f"ResponseStream chunk - id: { getattr (parsed_chunk , 'id' , None )} , has_output: { hasattr (parsed_chunk , 'output' )} , has_usage: { hasattr (parsed_chunk , 'usage' )} " )
369
371
370
372
# Update response_id if it becomes available
371
373
if parsed_chunk .id and not self ._traced_data .response_id :
@@ -382,6 +384,7 @@ def _process_chunk(self, chunk):
382
384
383
385
if hasattr (parsed_chunk , 'usage' ) and parsed_chunk .usage :
384
386
self ._traced_data .usage = parsed_chunk .usage
387
+ logger .debug (f"ResponseStream got usage data: { parsed_chunk .usage } " )
385
388
386
389
if hasattr (parsed_chunk , 'model' ) and parsed_chunk .model :
387
390
self ._traced_data .response_model = parsed_chunk .model
@@ -391,6 +394,7 @@ def _process_chunk(self, chunk):
391
394
if self ._traced_data .output_text is None :
392
395
self ._traced_data .output_text = ""
393
396
self ._traced_data .output_text += parsed_chunk .output_text
397
+ logger .debug (f"ResponseStream accumulated text from output_text: { len (parsed_chunk .output_text )} chars, total: { len (self ._traced_data .output_text )} " )
394
398
else :
395
399
# Try to extract and accumulate text from output blocks
396
400
try :
@@ -402,8 +406,9 @@ def _process_chunk(self, chunk):
402
406
if self ._traced_data .output_text is None :
403
407
self ._traced_data .output_text = ""
404
408
self ._traced_data .output_text += content_item .text
405
- except Exception :
406
- pass
409
+ logger .debug (f"ResponseStream accumulated text from content: { len (content_item .text )} chars, total: { len (self ._traced_data .output_text )} " )
410
+ except Exception as e :
411
+ logger .debug (f"ResponseStream error extracting text: { e } " )
407
412
408
413
# Update global dict with latest data
409
414
if self ._traced_data .response_id :
@@ -429,6 +434,7 @@ def _process_complete_response(self):
429
434
Process the complete streaming response.
430
435
Sets final span attributes, records metrics, and ends the span.
431
436
"""
437
+ logger .debug (f"ResponseStream _process_complete_response - input: { self ._traced_data .input } , output_text: { self ._traced_data .output_text [:100 ] if self ._traced_data .output_text else None } , usage: { self ._traced_data .usage } " )
432
438
if self ._span and self ._span .is_recording ():
433
439
set_data_attributes (self ._traced_data , self ._span )
434
440
@@ -514,6 +520,8 @@ def set_data_attributes(traced_response: TracedData, span: Span):
514
520
Set OpenTelemetry span attributes from traced response data.
515
521
Includes model info, usage stats, prompts, and completions.
516
522
"""
523
+ logger .debug (f"set_data_attributes - input: { traced_response .input } , output_text: { traced_response .output_text [:100 ] if traced_response .output_text else None } " )
524
+ logger .debug (f"set_data_attributes - usage: { traced_response .usage } " )
517
525
_set_span_attribute (span , GEN_AI_SYSTEM , "openai" )
518
526
_set_span_attribute (span , GEN_AI_REQUEST_MODEL , traced_response .request_model )
519
527
_set_span_attribute (span , GEN_AI_RESPONSE_ID , traced_response .response_id )
@@ -606,12 +614,14 @@ def set_data_attributes(traced_response: TracedData, span: Span):
606
614
prompt_index += 1
607
615
608
616
if isinstance (traced_response .input , str ):
617
+ logger .debug (f"Setting prompt as string: { traced_response .input [:100 ]} " )
609
618
_set_span_attribute (
610
619
span , f"{ GEN_AI_PROMPT } .{ prompt_index } .content" , traced_response .input
611
620
)
612
621
_set_span_attribute (span , f"{ GEN_AI_PROMPT } .{ prompt_index } .role" , "user" )
613
622
prompt_index += 1
614
- else :
623
+ elif traced_response .input :
624
+ logger .debug (f"Setting prompt as list with { len (traced_response .input ) if traced_response .input else 0 } items" )
615
625
for block in traced_response .input :
616
626
block_dict = model_as_dict (block )
617
627
if block_dict .get ("type" , "message" ) == "message" :
@@ -675,12 +685,17 @@ def set_data_attributes(traced_response: TracedData, span: Span):
675
685
)
676
686
prompt_index += 1
677
687
# TODO: handle other block types
688
+ else :
689
+ logger .debug (f"Input is neither string nor list: { type (traced_response .input )} " )
678
690
679
691
_set_span_attribute (span , f"{ GEN_AI_COMPLETION } .0.role" , "assistant" )
680
692
if traced_response .output_text :
693
+ logger .debug (f"Setting completion content: { traced_response .output_text [:100 ]} " )
681
694
_set_span_attribute (
682
695
span , f"{ GEN_AI_COMPLETION } .0.content" , traced_response .output_text
683
696
)
697
+ else :
698
+ logger .debug ("No output_text to set as completion content" )
684
699
tool_call_index = 0
685
700
if traced_response .output_blocks :
686
701
for block in traced_response .output_blocks .values ():
@@ -813,12 +828,16 @@ def responses_get_or_create_wrapper(
813
828
if response_id and response_id in responses :
814
829
existing_data = responses [response_id ].model_dump ()
815
830
831
+ input_data = process_input (
832
+ kwargs .get ("input" , existing_data .get ("input" , []))
833
+ )
834
+ logger .debug (f"SyncResponseStream init - input_data: { input_data } " )
835
+ logger .debug (f"SyncResponseStream init - kwargs keys: { kwargs .keys ()} " )
836
+
816
837
traced_data = TracedData (
817
838
start_time = time .time_ns (), # Use nanoseconds for TracedData
818
839
response_id = response_id or "" ,
819
- input = process_input (
820
- kwargs .get ("input" , existing_data .get ("input" , []))
821
- ),
840
+ input = input_data ,
822
841
instructions = kwargs .get (
823
842
"instructions" , existing_data .get ("instructions" )
824
843
),
@@ -972,12 +991,16 @@ async def async_responses_get_or_create_wrapper(
972
991
if response_id and response_id in responses :
973
992
existing_data = responses [response_id ].model_dump ()
974
993
994
+ input_data = process_input (
995
+ kwargs .get ("input" , existing_data .get ("input" , []))
996
+ )
997
+ logger .debug (f"AsyncResponseStream init - input_data: { input_data } " )
998
+ logger .debug (f"AsyncResponseStream init - kwargs keys: { kwargs .keys ()} " )
999
+
975
1000
traced_data = TracedData (
976
1001
start_time = time .time_ns (), # Use nanoseconds for TracedData
977
1002
response_id = response_id or "" ,
978
- input = process_input (
979
- kwargs .get ("input" , existing_data .get ("input" , []))
980
- ),
1003
+ input = input_data ,
981
1004
instructions = kwargs .get (
982
1005
"instructions" , existing_data .get ("instructions" , "" )
983
1006
),
0 commit comments