Skip to content

Support for function tool direct/structured output #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JRich3086 opened this issue Apr 4, 2025 · 4 comments
Closed

Support for function tool direct/structured output #436

JRich3086 opened this issue Apr 4, 2025 · 4 comments
Labels
enhancement New feature or request

Comments

@JRich3086
Copy link

Please read this first

  • Have you read the docs?Agents SDK docs - Yes
  • Have you searched for related issues? Others may have had similar requests - Yes

Describe the feature

Unless I've missed something, any agent tool output from final_output or new_item is forced to a string. I have a use case where it would be nice to be able to directly return an object from a function tool. For example, I am using Tavily search as a tool, which returns a dict, which I am converting to a Pydantic model. I know this can be done using an Agent as a tool and setting the output_type, but I'd prefer to just use the tool itself, if possible.

My workaround is as follows:

if new_item.type == 'tool_call_output_item':
    tav_json = eval(new_item.output)
    tav_response = PydanticModel.model_validate(tav_json)

But it would be nice to be able to convert the output directly in the function tool and return that:

@function_tool
def tav_search(query: str):
    """Search the web for information.
    
    Args:
        query: The user's query.
    """

    search_response =  TavilyClient.search(
        query=query,
        max_results=3,
        include_answer="advanced",
    )

    result = PydanticModel.model_validate(search_response)
    return result

Or possibly even set an output_type on the function tool itself.

@JRich3086 JRich3086 added the enhancement New feature or request label Apr 4, 2025
@rm-openai
Copy link
Collaborator

Yeah this should work as you described. The output does need to be stratified before we send it to the LLM, so you should ensure your object has a good __str__ (or repr) but you can return anything you like from here.

@JRich3086
Copy link
Author

Yeah this should work as you described. The output does need to be stratified before we send it to the LLM, so you should ensure your object has a good __str__ (or repr) but you can return anything you like from here.

Thank you. To clarify: this does work, but once the agent picks the tool result back up, the output is always forced to a string. And I don't want to strictly set a single output_type on this agent because it can also validly return strings when not calling this tool.

@rm-openai
Copy link
Collaborator

Thank you. To clarify: this does work, but once the agent picks the tool result back up, the output is always forced to a string. And I don't want to strictly set a single output_type on this agent because it can also validly return strings when not calling this tool.

Am i understanding correctly that you want the output of the tool to be the output of the agent? Maybe the agent's tool_use_behavior would be useful then, it lets you stop when a particular tool is called: https://openai.github.io/openai-agents-python/ref/agent/#agents.agent.Agent.tool_use_behavior

Let me know if there's a different desired behavior you want.

@JRich3086
Copy link
Author

Thank you. To clarify: this does work, but once the agent picks the tool result back up, the output is always forced to a string. And I don't want to strictly set a single output_type on this agent because it can also validly return strings when not calling this tool.

Am i understanding correctly that you want the output of the tool to be the output of the agent? Maybe the agent's tool_use_behavior would be useful then, it lets you stop when a particular tool is called: https://openai.github.io/openai-agents-python/ref/agent/#agents.agent.Agent.tool_use_behavior

Let me know if there's a different desired behavior you want.

Thank you. I think tool_use_behavior should accomplish what I am looking for. I had not yet updated my agents module so I didn't see this option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants