Fix as_tool returning blank string on early tool termination #2112
+5
−21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolved: #2110
This PR fixes a bug where using
Agent.as_tooltogether withtool_use_behaviorcauses the lead agent to receive blank string whenever the tool terminates early.This issue affects not only the case described in #2110 (ToolsToFinalOutputResult), but also
stop_on_first_toolandStopAtTools.Root Cause
as_toolcurrently returns:This function only extracts text from
output.new_items. But when a tool stops early,new_itemscontains no text messages, so the return value becomes an blank string"".Solution
Return
final_outputinstead. It already handles early tool-return scenarios correctly.Behavior Changes
text_message_outputs(output.new_items)includes the final assistant message and any intermediate preamble assistant messages.Switching to
final_outputremoves these preambles. Only final assistant message.I think this is fine and reasonable because:
text_message_outputs(output.new_items)would return the preambles, not actual tool result, which is incorrect anyway.final_outputmay be structured output instead of a plain string.Repro Code
Before fix: You will get "N/A" instead of a random number