Skip to content

Conversation

@ihower
Copy link
Contributor

@ihower ihower commented Nov 21, 2025

Resolved: #2110

This PR fixes a bug where using Agent.as_tool together with tool_use_behavior causes 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_tool and StopAtTools.

Root Cause

as_tool currently returns:

ItemHelpers.text_message_outputs(output.new_items)

This function only extracts text from output.new_items. But when a tool stops early, new_items contains no text messages, so the return value becomes an blank string "".

Solution

Return final_output instead. It already handles early tool-return scenarios correctly.

Behavior Changes

  1. text_message_outputs(output.new_items) includes the final assistant message and any intermediate preamble assistant messages.

    • Switching to final_output removes these preambles. Only final assistant message.

    • I think this is fine and reasonable because:

      1. preamble messages are rare unless explicitly prompted
      2. preambles are intended for human readability during tool-calling explanations, and they unnecessarily consume the lead agent’s context window
      3. when preambles exist and the tool terminates early, text_message_outputs(output.new_items) would return the preambles, not actual tool result, which is incorrect anyway.
  2. final_output may be structured output instead of a plain string.

    • This aligns with the new tool-call behavior, where tool outputs are no longer restricted to text value and can be structured data (i.e. image or files)

Repro Code

from agents import Agent, function_tool, Runner
from agents.agent import ToolsToFinalOutputResult
import random

@function_tool
def generate_random_number_tool() -> int:
    return random.randint(0, 100)


random_number_agent = Agent(
    name="Sub Agent",
    instructions="You are a helpful assistant that generate_random_number_tool",
    model="gpt-5-mini",
    tools=[generate_random_number_tool],
    tool_use_behavior="stop_on_first_tool"
)

main_agent = Agent(
    name="Main Agent",
    instructions="You are a helpful assistant. Must use the random_number_tool to generate a random number. If tool ouput is empty, just say N/A",
    model="gpt-5-mini",
    tools=[random_number_agent.as_tool(tool_name="random_number_tool", tool_description="A tool that generates a random number.")],
)


result = Runner.run_sync(main_agent, "What is the random number?")
print(result.final_output)

Before fix: You will get "N/A" instead of a random number

@ihower ihower changed the title ## Fix as_tool returning None on early tool termination Fix as_tool returning None on early tool termination Nov 21, 2025
@ihower ihower changed the title Fix as_tool returning None on early tool termination Fix as_tool returning blank string on early tool termination Nov 21, 2025
@seratch seratch added bug Something isn't working feature:core labels Nov 22, 2025
@seratch seratch added this to the 0.6.x milestone Nov 22, 2025
Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@seratch seratch merged commit a7c539f into openai:main Nov 22, 2025
9 checks passed
@xJkit
Copy link

xJkit commented Nov 23, 2025

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ToolsToFinalOutputResult does not work properly when the Agent is used as a tool function

3 participants