Skip to content

Fix IndexError in CodeAgent.run method when message.content is empty#820

Draft
codegen-sh[bot] wants to merge 2 commits intodevelopfrom
gen/8adf4bcc-48de-430a-9fb9-49cb4329fba9
Draft

Fix IndexError in CodeAgent.run method when message.content is empty#820
codegen-sh[bot] wants to merge 2 commits intodevelopfrom
gen/8adf4bcc-48de-430a-9fb9-49cb4329fba9

Conversation

@codegen-sh
Copy link
Contributor

@codegen-sh codegen-sh bot commented Mar 12, 2025

Description

This PR fixes an IndexError that occurs in the CodeAgent.run() method when handling AI messages with empty content lists.

Problem

The error occurs when trying to access message.content[0] but message.content is an empty list:

IndexError: list index out of range

Stack Trace

File "/root/app/modal_app/gen/slack/handlers.py", line 46, in handle_app_mention
  agent.run(prompt)
File "/usr/local/lib/python3.12/site-packages/codegen/agents/code_agent.py", line 123, in run
  if isinstance(message, AIMessage) and isinstance(message.content, list) and "text" in message.content[0]:
                                                                                      ~~~~~~~~~~~~~~~^^^

Solution

Added a check to verify that message.content is not empty before trying to access its first element:

if isinstance(message, AIMessage) and isinstance(message.content, list) and len(message.content) > 0 and "text" in message.content[0]:

This prevents the IndexError when message.content is an empty list.

Testing

This fix should prevent the IndexError when the CodeAgent receives an AIMessage with an empty content list.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


codegen-bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant