fix: preserve interim content as fallback when retry produces empty response
Fixes regression from #825 where models that respond with final text directly (no tools) had their answer discarded by the retry mechanism. Closes #878
This commit is contained in:
@@ -185,6 +185,7 @@ class AgentLoop:
|
|||||||
final_content = None
|
final_content = None
|
||||||
tools_used: list[str] = []
|
tools_used: list[str] = []
|
||||||
text_only_retried = False
|
text_only_retried = False
|
||||||
|
interim_content: str | None = None # Fallback if retry produces nothing
|
||||||
|
|
||||||
while iteration < self.max_iterations:
|
while iteration < self.max_iterations:
|
||||||
iteration += 1
|
iteration += 1
|
||||||
@@ -231,9 +232,11 @@ class AgentLoop:
|
|||||||
else:
|
else:
|
||||||
final_content = self._strip_think(response.content)
|
final_content = self._strip_think(response.content)
|
||||||
# Some models send an interim text response before tool calls.
|
# Some models send an interim text response before tool calls.
|
||||||
# Give them one retry; don't forward the text to avoid duplicates.
|
# Give them one retry; save the content as fallback in case
|
||||||
|
# the retry produces nothing useful (e.g. model already answered).
|
||||||
if not tools_used and not text_only_retried and final_content:
|
if not tools_used and not text_only_retried and final_content:
|
||||||
text_only_retried = True
|
text_only_retried = True
|
||||||
|
interim_content = final_content
|
||||||
logger.debug("Interim text response (no tools used yet), retrying: {}", final_content[:80])
|
logger.debug("Interim text response (no tools used yet), retrying: {}", final_content[:80])
|
||||||
messages = self.context.add_assistant_message(
|
messages = self.context.add_assistant_message(
|
||||||
messages, response.content,
|
messages, response.content,
|
||||||
@@ -241,6 +244,9 @@ class AgentLoop:
|
|||||||
)
|
)
|
||||||
final_content = None
|
final_content = None
|
||||||
continue
|
continue
|
||||||
|
# Fall back to interim content if retry produced nothing
|
||||||
|
if not final_content and interim_content:
|
||||||
|
final_content = interim_content
|
||||||
break
|
break
|
||||||
|
|
||||||
return final_content, tools_used
|
return final_content, tools_used
|
||||||
|
|||||||
Reference in New Issue
Block a user