@@ -218,50 +218,54 @@ def new_chat(*args, **kwargs):
218218 def new_iterator ():
219219 # type: () -> Iterator[V2ChatStreamResponse]
220220 collected_text = []
221- with capture_internal_exceptions () :
221+ try :
222222 for x in old_iterator :
223- if (
224- hasattr (x , "type" )
225- and x .type == "content-delta"
226- and hasattr (x , "delta" )
227- and x .delta is not None
228- ):
229- msg = getattr (x .delta , "message" , None )
230- if msg is not None :
231- content = getattr (msg , "content" , None )
232- if content is not None and hasattr (content , "text" ):
233- collected_text .append (content .text )
234-
235- if isinstance (x , MessageEndV2ChatStreamResponse ):
236- include_pii = (
237- should_send_default_pii ()
238- and integration .include_prompts
239- )
240- if include_pii and collected_text :
241- set_data_normalized (
242- span ,
243- SPANDATA .GEN_AI_RESPONSE_TEXT ,
244- ["" .join (collected_text )],
223+ with capture_internal_exceptions ():
224+ if (
225+ hasattr (x , "type" )
226+ and x .type == "content-delta"
227+ and hasattr (x , "delta" )
228+ and x .delta is not None
229+ ):
230+ msg = getattr (x .delta , "message" , None )
231+ if msg is not None :
232+ content = getattr (msg , "content" , None )
233+ if content is not None and hasattr (content , "text" ):
234+ collected_text .append (content .text )
235+
236+ if isinstance (x , MessageEndV2ChatStreamResponse ):
237+ include_pii = (
238+ should_send_default_pii ()
239+ and integration .include_prompts
245240 )
246- if hasattr (x , "id" ):
247- set_data_normalized (
248- span , SPANDATA .GEN_AI_RESPONSE_ID , x .id
249- )
250- if hasattr (x , "delta" ) and x .delta is not None :
251- if hasattr (x .delta , "finish_reason" ):
241+ if include_pii and collected_text :
252242 set_data_normalized (
253243 span ,
254- SPANDATA .GEN_AI_RESPONSE_FINISH_REASONS ,
255- x .delta .finish_reason ,
244+ SPANDATA .GEN_AI_RESPONSE_TEXT ,
245+ ["" .join (collected_text )],
246+ )
247+ if hasattr (x , "id" ):
248+ set_data_normalized (
249+ span , SPANDATA .GEN_AI_RESPONSE_ID , x .id
256250 )
257- if (
258- hasattr (x .delta , "usage" )
259- and x .delta .usage is not None
260- ):
261- _record_token_usage_v2 (span , x .delta .usage )
251+ if hasattr (x , "delta" ) and x .delta is not None :
252+ if hasattr (x .delta , "finish_reason" ):
253+ set_data_normalized (
254+ span ,
255+ SPANDATA .GEN_AI_RESPONSE_FINISH_REASONS ,
256+ x .delta .finish_reason ,
257+ )
258+ if (
259+ hasattr (x .delta , "usage" )
260+ and x .delta .usage is not None
261+ ):
262+ _record_token_usage_v2 (span , x .delta .usage )
262263 yield x
263-
264- span .__exit__ (None , None , None )
264+ except Exception as exc :
265+ _capture_exception (exc )
266+ raise
267+ finally :
268+ span .__exit__ (None , None , None )
265269
266270 return new_iterator ()
267271 elif isinstance (res , V2ChatResponse ):
0 commit comments