@@ -3365,6 +3365,7 @@ def set_last_text(out, text):
33653365 output = []
33663366
33673367 usage = None
3368+ prior_output = []
33683369
33693370 reasoning_tags_param = metadata .get ('params' , {}).get ('reasoning_tags' )
33703371 DETECT_REASONING_TAGS = reasoning_tags_param is not False
@@ -3399,14 +3400,10 @@ async def stream_body_handler(response, form_data):
33993400 nonlocal content
34003401 nonlocal usage
34013402 nonlocal output
3403+ nonlocal prior_output
34023404
34033405 response_tool_calls = []
34043406
3405- # Responses API output_index values are relative to the
3406- # current response (0, 1, ...). During re-invocations,
3407- # output has accumulated history, so we offset indices.
3408- responses_output_offset = len (output )
3409-
34103407 delta_count = 0
34113408 delta_chunk_size = max (
34123409 CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE ,
@@ -3475,15 +3472,12 @@ async def flush_pending_delta_data(threshold: int = 0):
34753472 )
34763473 # Check for Responses API events (type field starts with "response.")
34773474 elif data .get ('type' , '' ).startswith ('response.' ):
3478- # Offset output_index for re-invocations
3479- if responses_output_offset and 'output_index' in data :
3480- data = {** data , 'output_index' : data ['output_index' ] + responses_output_offset }
34813475
34823476 output , response_metadata = handle_responses_streaming_event (data , output )
34833477
34843478 processed_data = {
3485- 'output' : output ,
3486- 'content' : serialize_output (output ),
3479+ 'output' : prior_output + output ,
3480+ 'content' : serialize_output (prior_output + output ),
34873481 }
34883482
34893483 # print(data)
@@ -4233,17 +4227,17 @@ async def flush_pending_delta_data(threshold: int = 0):
42334227 )
42344228
42354229 if isinstance (res , StreamingResponse ):
4236- # Save output before re-invocation. Responses API
4237- # response.completed replaces the entire output
4238- # with only the new response's items, losing tool
4239- # call history. Restore previous items afterward.
4240- output_prefix = list (output )
4230+ # Save accumulated output and start fresh.
4231+ # Responses API output_index values are relative
4232+ # to the current response — a clean output list
4233+ # keeps indices aligned. The display prefix
4234+ # ensures the UI shows tool history during
4235+ # streaming.
4236+ prior_output = list (output )
4237+ output = []
42414238 await stream_body_handler (res , new_form_data )
4242- if output_prefix and (
4243- not output
4244- or output [0 ].get ('id' ) != output_prefix [0 ].get ('id' )
4245- ):
4246- output [:0 ] = output_prefix
4239+ output [:0 ] = prior_output
4240+ prior_output = []
42474241 else :
42484242 break
42494243 except Exception as e :
0 commit comments