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

Tool Calling Running in Loop Until Max-Turn #191

Open
WSQsGithub opened this issue Mar 17, 2025 · 2 comments
Open

Tool Calling Running in Loop Until Max-Turn #191

WSQsGithub opened this issue Mar 17, 2025 · 2 comments
Labels
needs-more-info Waiting for a reply/more info from the author

Comments

@WSQsGithub
Copy link

WSQsGithub commented Mar 17, 2025

Please read this first

  • Have you read the docs?Agents SDK docs
  • Have you searched for related issues? Others may have faced similar issues.

Describe the bug

A clear and concise description of what the bug is.

The the agent is calling the one tool I provided for multiple times until the max_turn runs out. And each function calls have different search input parameters.

And from the stream events log, it seems the function call is sucessful because retrieved text appears in the raw_item output.

I tried to make the max_turn bigger (all the way to 100), but still the agent keeps calling the tools.

Debug information

  • Agents SDK version: (e.g. v0.0.3) 0.0.4
  • Python version (e.g. Python 3.10) Python 3.13.1

Repro steps

Ideally provide a minimal python script that can be run to reproduce the bug.

I implemented a tool like this that calls an api to retrieve documents from a database.

@function_tool
def similarity_search(chat_uuid: str, text: str) -> str:
"""
docstring ignored...
"""
    text_embeddings = TextEmbeddingService.similarity_search_sync(
        chat_uuid=chat_uuid,
        text=text,
        limit=LIMIT,
        threshold=THRESHOLD,
    )

    answer = ""
    for i, text_embedding in enumerate(text_embeddings):
        answer += f"[{i + 1}] {text_embedding.text}\n"

    return answer

And I list it as a tool for my search agent, where BaseAgent is simply a wrapper on Agent class with an extra parameter.

class SearchAgent(BaseAgent):
    def __init__(self, chat_uuid: str) -> None:
        super().__init__(chat_uuid=chat_uuid)
        self.name = "Search Agent"
        self.instructions = (
            "You are a search assistant."
            "Use your tools to help the user with their questions."
            "This chat_uuid is unique to you, and you can use it for function calls."
            f"chat_uuid='{self.chat_uuid}'"
            "Use the following routine to help the user."
            "1. Identify the question asked by the user."
            "2. Come up with ONE search input to  gather information for the question."
            "3. Call your search tools ONLY ONCE."
            "4. Write a short report on your findings."
            "5. Index and cite the sources you use in your answer as urls."
        )
        self.handoff_description = "A Search agent is a specialized agent that can search the internet or user's knowledge base for information."
        self.model_settings = ModelSettings(tool_choice="required", parallel_tool_calls=False)
        self.tools = [
            self.similarity_search,  # BUG: Stuck in a loop until max_turns is reached
        ]

Expected behavior

A clear and concise description of what you expected to happen.

The agent respond with a short paragraph with the retrieved results.

@WSQsGithub WSQsGithub added the bug Something isn't working label Mar 17, 2025
@WSQsGithub
Copy link
Author

Guys this issue only occurs when I set tool_choice="required" or "similarity_search". I removed it and it works fine now.

@rm-openai
Copy link
Collaborator

rm-openai commented Mar 17, 2025

Yeah, tool choice required can be an unexpected footgun. See #203 for something that will help. And let me know if you have follow up q.s

@rm-openai rm-openai added needs-more-info Waiting for a reply/more info from the author and removed bug Something isn't working labels Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-more-info Waiting for a reply/more info from the author
Projects
None yet
Development

No branches or pull requests

2 participants