@@ -340,13 +340,17 @@ async def _parse_dashscope_stream_response(
340340 metadata = None
341341 last_content = None
342342 usage = None
343+ response_id : str | None = None
343344
344345 async for chunk in giter (response ):
345346 if chunk .status_code != HTTPStatus .OK :
346347 raise RuntimeError (
347348 f"Failed to get response from _ API: { chunk } " ,
348349 )
349350
351+ if response_id is None :
352+ response_id = getattr (chunk , "request_id" , None )
353+
350354 message = chunk .output .choices [0 ].message
351355
352356 # Update reasoning content
@@ -451,11 +455,14 @@ async def _parse_dashscope_stream_response(
451455 )
452456
453457 if content_blocks :
454- parsed_chunk = ChatResponse (
455- content = content_blocks ,
456- usage = usage ,
457- metadata = metadata ,
458- )
458+ _kwargs : dict [str , Any ] = {
459+ "content" : content_blocks ,
460+ "usage" : usage ,
461+ "metadata" : metadata ,
462+ }
463+ if response_id :
464+ _kwargs ["id" ] = response_id
465+ parsed_chunk = ChatResponse (** _kwargs )
459466 yield parsed_chunk
460467 last_content = copy .deepcopy (content_blocks )
461468
@@ -473,11 +480,14 @@ async def _parse_dashscope_stream_response(
473480 if structured_model :
474481 metadata = input_obj
475482
476- yield ChatResponse (
477- content = last_content ,
478- usage = usage ,
479- metadata = metadata ,
480- )
483+ _final_kwargs : dict [str , Any ] = {
484+ "content" : last_content ,
485+ "usage" : usage ,
486+ "metadata" : metadata ,
487+ }
488+ if response_id :
489+ _final_kwargs ["id" ] = response_id
490+ yield ChatResponse (** _final_kwargs )
481491
482492 async def _parse_dashscope_generation_response (
483493 self ,
@@ -574,13 +584,16 @@ async def _parse_dashscope_generation_response(
574584 metadata = response .usage ,
575585 )
576586
577- parsed_response = ChatResponse (
578- content = content_blocks ,
579- usage = usage ,
580- metadata = metadata ,
581- )
587+ resp_kwargs : dict [str , Any ] = {
588+ "content" : content_blocks ,
589+ "usage" : usage ,
590+ "metadata" : metadata ,
591+ }
592+ response_id = getattr (response , "request_id" , None )
593+ if response_id :
594+ resp_kwargs ["id" ] = response_id
582595
583- return parsed_response
596+ return ChatResponse ( ** resp_kwargs )
584597
585598 def _format_tools_json_schemas (
586599 self ,
0 commit comments