Skip to content

fix: IndexError in CodeAgent.run method when message.content is empty #821

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

Merged
merged 1 commit into from
Mar 12, 2025

Conversation

codegen-sh[bot]
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.

@jayhack jayhack marked this pull request as ready for review March 12, 2025 23:08
@jayhack jayhack requested review from codegen-team and a team as code owners March 12, 2025 23:08
@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.

@jayhack jayhack changed the title Fix IndexError in CodeAgent.run method when message.content is empty fix: IndexError in CodeAgent.run method when message.content is empty Mar 12, 2025
@jayhack jayhack merged commit 8b06713 into develop Mar 12, 2025
15 of 18 checks passed
@jayhack jayhack deleted the gen/af42d041-a026-4dbc-b22f-ec14ac29d261 branch March 12, 2025 23:11
Copy link
Contributor

🎉 This PR is included in version 0.49.5 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

tkfoss pushed a commit that referenced this pull request Mar 14, 2025
…#821)

## 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:

```python
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.

Co-authored-by: codegen-bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants