@@ -305,6 +305,7 @@ async def reply( # pylint: disable=too-many-branches
305305 # -------------- The reasoning-acting loop --------------
306306 # Cache the structured output generated in the finish function call
307307 structured_output = None
308+ reply_msg = None
308309 for _ in range (self .max_iters ):
309310 # -------------- The reasoning process --------------
310311 msg_reasoning = await self ._reasoning (tool_choice )
@@ -339,12 +340,13 @@ async def reply( # pylint: disable=too-many-branches
339340 if msg_reasoning .has_content_blocks ("text" ):
340341 # Re-use the existing text response if any to avoid
341342 # duplicate text generation
342- return Msg (
343+ reply_msg = Msg (
343344 self .name ,
344345 msg_reasoning .get_content_blocks ("text" ),
345346 "assistant" ,
346347 metadata = structured_output ,
347348 )
349+ break
348350
349351 # Generate a textual response in the next iteration
350352 msg_hint = Msg (
@@ -383,11 +385,15 @@ async def reply( # pylint: disable=too-many-branches
383385 # Exit the loop when no structured output is required (or
384386 # already satisfied) and only text response is generated
385387 msg_reasoning .metadata = structured_output
386- return msg_reasoning
388+ reply_msg = msg_reasoning
389+ break
387390
388391 # When the maximum iterations are reached
389- reply_msg = await self ._summarizing ()
390- reply_msg .metadata = structured_output
392+ # and no reply message is generated
393+ if reply_msg is None :
394+ reply_msg = await self ._summarizing ()
395+ reply_msg .metadata = structured_output
396+ await self .memory .add (reply_msg )
391397
392398 # Post-process the memory, long-term memory
393399 if self ._static_control :
@@ -399,7 +405,6 @@ async def reply( # pylint: disable=too-many-branches
399405 ],
400406 )
401407
402- await self .memory .add (reply_msg )
403408 return reply_msg
404409
405410 # pylint: disable=too-many-branches
0 commit comments