@@ -269,7 +269,6 @@ def stream_event_to_chunk(
269269 we use our own implementation to support the responses api.
270270 """
271271 choices = []
272- model_response = None
273272
274273 if isinstance (event , ResponseTextDeltaEvent ):
275274 delta = Message (content = event .delta )
@@ -279,18 +278,23 @@ def stream_event_to_chunk(
279278 model_response = ModelResponse (
280279 stream = True , choices = choices , model = model , id = str (uuid .uuid4 ())
281280 )
281+ for chunk , _ in _model_response_to_chunk (model_response ):
282+ # delta text, not finish
283+ yield model_response , chunk , None
282284 elif isinstance (event , ResponseCompletedEvent ):
283285 response = event .response
284286 model_response = self .transform_response (response , stream = True )
285287 model_response = fix_model_response (model_response )
288+
289+ for chunk , finish_reason in _model_response_to_chunk (model_response ):
290+ if isinstance (chunk , TextChunk ):
291+ yield model_response , None , finish_reason
292+ else :
293+ yield model_response , chunk , finish_reason
286294 else :
287295 # Ignore other event types like ResponseOutputItemAddedEvent, etc.
288296 pass
289297
290- if model_response :
291- for chunk , finish_reason in _model_response_to_chunk (model_response ):
292- yield model_response , chunk , finish_reason
293-
294298
295299def fix_model_response (model_response : ModelResponse ) -> ModelResponse :
296300 """
0 commit comments