Skip to content
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

How to use the Custom Web Search tool #170

Open
jqsl2012 opened this issue Mar 15, 2025 · 4 comments
Open

How to use the Custom Web Search tool #170

jqsl2012 opened this issue Mar 15, 2025 · 4 comments
Labels
question Question about using the SDK

Comments

@jqsl2012
Copy link

Question

I don't want to use the built-in WebSearchTool, I want to use a custom search tool.

After the program is run, it does output [debug] web_search_tool input query=xxx, but it also reports an error: agents.exceptions.MaxTurnsExceeded: Max turns (1) exceeded. How should I write my custom search function correctly?

@function_tool
def web_search_tool(query: str):
    print(f"[debug] web_search_tool input query={query}")
    return f"Search {query} result."


search_agent = Agent(
    name="Search agent",
    instructions=INSTRUCTIONS,
    tools=[web_search_tool],
    model_settings=ModelSettings(tool_choice="required"),
)
async def _search(self, item: WebSearchItem) -> str | None:
        input = f"Search term: {item.query}\nReason for searching: {item.reason}"
        try:
            result = await Runner.run(
                search_agent,
                input,
                max_turns=1
            )
            return str(result.final_output)
        except Exception:
            logger.exception("_search ex=")
            return None

Image

@jqsl2012 jqsl2012 added the question Question about using the SDK label Mar 15, 2025
@rm-openai
Copy link
Collaborator

You'll need max turns>1.

  1. Turn 1: agent calls the web search tool. Tool runs, and tool result is appended to the history
  2. Turn 2: agent receives the web search result and processes it. (Though in this case with tool_choice required, it will try to use the tool again)

@jqsl2012
Copy link
Author

@rm-openai Thank you, I changed max_turns to 3 and the program works normally.

In addition, I also found that when adding model_settings=ModelSettings(tool_choice="required") to Agent, MaxTurnsExceeded will still appear when max_turns=3. However, max_turns=3 remains unchanged, and the program works normally when model_settings=ModelSettings(tool_choice="required") is commented out. I don't know why this happens at the moment.

@jaijuneja
Copy link

As a few folks have mentioned, setting tool_choice="required" seems to force the agent to use the web_search_tool at every iteration rather than processing the result. Best bet is to remove that setting and instead give a clear instruction to the agent to only run the web_search_tool once, which I've found to work reliably.

@rm-openai
Copy link
Collaborator

Yes, tool_choice=required can be a footgun. #203 helps with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question about using the SDK
Projects
None yet
Development

No branches or pull requests

3 participants