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
Sure
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