llama_index: [Bug]: Repeated/Infinite tools calls in AgentWorkflow

Bug Description

I am building an agent workflow that interacts with some of the google apis say gmail and while it does work, it does inconsistently. I say this as when I ask it to send emails it could send anywhere from 1 to a never ending amount of emails that are identically the same. From the logs it makes a call to Gemini twice at the very start. Also it says it logs the second last statement in mail.py which is immediately followed by a return statement, but it doesn’t return anything in that moment, and instead calls on the LLM again. I am aware that Gemini and asynchronous code don’t work the best together. However, I seem to have the best chances of reducing the number of calls when some the functions I use become async.

Version

0.12.37

Steps to Reproduce

app.py

@app.post('/api/chat', response_model=ChatResponse)
async def chat(request: ChatRequest):
  response = await agent_workflow.run(request.message, memory=user_memory)

  return response

agents.py

email_agent = FunctionAgent(
  name="email_agent",
  description="An intelligent email agent that can read, send, and reply to emails using Gmail integration.",
  system_prompt=mail_system_prompt,
  llm=Settings.llm,
  tools=[get_unread_emails, send_email, reply_to_email],
)

orchestrator_agent = FunctionAgent(
   name="orchestrator_agent",
    description="An intelligent orchestrator that can handle complex requests requiring both email and calendar operations, and routes simpler requests to specialized agents.",
  system_prompt=system_prompt,
  llm=Settings.llm,
  tools=[],
  can_handoff_to=[email_agent.name]
)

agent_workflow = AgentWorkflow(
    agents=[email_agent, orchestrator_agent],
    root_agent=orchestrator_agent.name,
)

mail.py

async def send_email(
    to_names: list[str],
    subject: str,
    body: str,
    cc_names: Optional[list[str]] = None,
    bcc_names: Optional[list[str]] = None
):
    """
    Send an email using Gmail API. This function resolves recipient names to email addresses automatically.
    
    Use this function when the user asks you to send an email, compose a message, or contact someone.
    The function will automatically resolve names like "John" or "Sarah" to their actual email addresses
    using the user's contacts stored in Supabase.
    
    When this function succeeds, simply return its output directly to the user without suggesting other tools.
    
    ONLY USE THIS FUNCTION ONCE FOR EACH USER REQUEST.
    
    Args:
        to_names: List of recipient names or email addresses (e.g., ["John Smith", "sarah@example.com"])
        subject: Email subject line
        body: Email body content
        cc_names: Optional list of CC recipient names/emails
        bcc_names: Optional list of BCC recipient names/emails
    
    Returns:
        str: Success message showing the email content and recipients that were sent to
        
    Example:
        send_email(["John Smith", "Sarah"], "Meeting Tomorrow", "Hi, let's meet at 2pm tomorrow.")
    """
    try:
        # Add debug logging to track function calls
        logger.info(f"πŸš€ send_email called with to_names: {to_names}, subject: {subject}")
        
        # Resolve names to email addresses
        logger.info(f"πŸ” Resolving attendee names...")
        to_emails = await resolve_attendee_names(to_names)
        cc_emails = await resolve_attendee_names(cc_names) if cc_names else []
        bcc_emails = await resolve_attendee_names(bcc_names) if bcc_names else []
        
        if not to_emails:
            logger.error("❌ No valid email addresses found for recipients")
            return "Error: No valid email addresses found for recipients"
        
        logger.info(f"βœ… Resolved emails: {to_emails}")
        
        # Create properly formatted email message
        msg = MIMEMultipart('alternative')
        msg['To'] = ', '.join(to_emails)
        msg['Subject'] = subject
        
        if cc_emails:
            msg['Cc'] = ', '.join(cc_emails)
        if bcc_emails:
            msg['Bcc'] = ', '.join(bcc_emails)
        
        # Create plain text version (same as original)
        # plain_text = body.replace('\n', '\r\n')
        plain_text = body
        
        # Create Gmail-style HTML version
        html_body = create_gmail_html(body)
        
        # Attach plain text first, then HTML (Gmail order)
        msg.attach(MIMEText(plain_text, 'plain', 'utf-8'))
        msg.attach(MIMEText(html_body, 'html', 'utf-8'))
        
        # Get Gmail service and send
        logger.info(f"πŸ“€ Sending email to Gmail API...")
        service = build_email_service()
        raw_message = base64.urlsafe_b64encode(msg.as_bytes()).decode('utf-8')
        message = {'raw': raw_message}
        
        service.users().messages().send(userId='me', body=message).execute()
        logger.info(f"βœ… Email successfully sent via Gmail API")
        
        # Create a comprehensive success message for the user
        recipient_info = []
        for name, email in zip(to_names, to_emails):
            if name != email:
                recipient_info.append(f"{name} ({email})")
            else:
                recipient_info.append(email)
        
        logger.info(f"πŸŽ‰ send_email completed successfully")
        return f"Email sent successfully to {', '.join(recipient_info)}"
    
    except Exception as e:
        logger.error(f"❌ Error sending email: {str(e)}")
        return f"Error sending email: {str(e)}"

Relevant Logs/Tracbacks

INFO:httpx:HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:streamGenerateContent?alt=sse "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:streamGenerateContent?alt=sse "HTTP/1.1 200 OK"
INFO:tools.mail:πŸš€ send_email called with to_names: NAME, subject: Inquiry Regarding Refactoring Status
INFO:tools.mail:πŸ” Resolving attendee names...
INFO:tools.contacts:Loading contacts for user USERID from path: PATH
INFO:httpx:HTTP Request: GET S3-BUCKET "HTTP/2 200 OK"
INFO:tools.contacts:Loaded 2 contacts for user USERIF
INFO:tools.contacts:Resolved 1 names to 2 unique email addresses
INFO:tools.mail:βœ… Resolved emails:[]
INFO:tools.mail:πŸ“€ Sending email to Gmail API...
INFO:tools.mail:βœ… Email successfully sent via Gmail API
INFO:tools.mail:πŸŽ‰ send_email completed successfully
INFO:httpx:HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:streamGenerateContent?alt=sse "HTTP/1.1 200 OK"
INFO:tools.mail:πŸš€ send_email called with to_names: NAME, subject: Inquiry Regarding Refactoring Status
INFO:tools.mail:πŸ” Resolving attendee names...
INFO:tools.contacts:Loading contacts for user USERID from path: PATH
INFO:httpx:HTTP Request: GET S3-BUCKET "HTTP/2 200 OK"
INFO:tools.contacts:Loaded 2 contacts for user USERID
INFO:tools.contacts:Resolved 1 names to 2 unique email addresses
INFO:tools.mail:βœ… Resolved emails: []
INFO:tools.mail:πŸ“€ Sending email to Gmail API...
INFO:tools.mail:βœ… Email successfully sent via Gmail API
INFO:tools.mail:πŸŽ‰ send_email completed successfully
INFO:httpx:HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:streamGenerateContent?alt=sse "HTTP/1.1 200 OK"

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 17

Most upvoted comments

Sure

class CustomWorkflow(AgentWorkflow):
   def __init__(self, user_id: str = None, *args, **kwargs):
      super().__init__(*args, **kwargs)
      self.last_tool_call_output = None
      self.last_tool_call_name = None

   @step
   async def call_tool(self, ctx: Context, ev: ToolCall) -> ToolCallResult:
      """Calls the tool and handles the result and also prevents excess tool calls"""
      if ev.tool_name != "handoff" and ev.tool_name == self.last_tool_call_name:
         return StopEvent(result=self.last_tool_call_output)
      
      ctx.write_event_to_stream(
         ToolCall(
            tool_name=ev.tool_name,
            tool_kwargs=ev.tool_kwargs,
            tool_id=ev.tool_id,
         )
      )
      
      current_agent_name = await ctx.get("current_agent_name")
      tools = await self.get_tools(current_agent_name, ev.tool_name)
      tools_by_name = {tool.metadata.name: tool for tool in tools}
      if ev.tool_name not in tools_by_name:
         tool = None
         result = ToolOutput(
            content=f"Tool {ev.tool_name} not found. Please select a tool that is available.",
            tool_name=ev.tool_name,
            raw_input=ev.tool_kwargs,
            raw_output=None,
            is_error=True,
         )
      else:
         tool = tools_by_name[ev.tool_name]
         result = await self._call_tool(ctx, tool, ev.tool_kwargs)
         
      result_ev = ToolCallResult(
         tool_name=ev.tool_name,
         tool_kwargs=ev.tool_kwargs,
         tool_id=ev.tool_id,
         tool_output=result,
         return_direct=tool.metadata.return_direct if tool else False,
      )
      
      ctx.write_event_to_stream(result_ev)
      
      self.last_tool_call_name = ev.tool_name
      self.last_tool_call_output = result_ev.tool_output
      
      return result_ev

Depending on your use case and how you want to handle the different types of events, you might want to change the actual handling, but this is what I mentioned above and what I use in my codebase