Commit 8b06713
fix: IndexError in CodeAgent.run method when message.content is empty (#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 <team+codegenbot@codegen.sh>1 parent 58614c1 commit 8b06713
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
| |||
0 commit comments