Skip to content

fix: initialize user_assistant_msgs to prevent UnboundLocalError in compress_messages - #1960

Closed
alvinttang wants to merge 1 commit into
FoundationAgents:mainfrom
alvinttang:fix/compress-messages-unbound-variable
Closed

fix: initialize user_assistant_msgs to prevent UnboundLocalError in compress_messages#1960
alvinttang wants to merge 1 commit into
FoundationAgents:mainfrom
alvinttang:fix/compress-messages-unbound-variable

Conversation

@alvinttang

Copy link
Copy Markdown

Summary

  • BaseLLM.compress_messages() raises UnboundLocalError when all messages are system messages or the message list is empty (with compression enabled).

Root Cause

  • In compress_messages(), the variable user_assistant_msgs is only assigned inside the else branch of a for-loop (line 369). If all messages have role == "system", the else branch never executes, and user_assistant_msgs remains undefined. The subsequent code on lines 378 and 396 references this variable unconditionally, causing an UnboundLocalError.

Fix

  • Initialize user_assistant_msgs = [] before the loop so the variable always has a valid default value. This makes the compression logic safely handle edge cases (all-system messages, empty lists) by producing an empty compressed result instead of crashing.

Test

from metagpt.provider.base_llm import BaseLLM

# Before fix: raises UnboundLocalError
# After fix: returns the system messages without crashing
messages = [{"role": "system", "content": "You are helpful."}]
# compress_messages with any compress_type != NO_COMPRESS

…ompress_messages

When all messages are system messages or the message list is empty,
the for-loop never enters the else branch, leaving user_assistant_msgs
undefined. This causes an UnboundLocalError when the variable is
referenced in the subsequent compression logic.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Jah-yee pushed a commit to Jah-yee/MetaGPT that referenced this pull request Mar 13, 2026
Fixes FoundationAgents#1960 - compress_messages() raises UnboundLocalError when all
messages are system messages or the message list is empty (with compression
enabled).

Root cause: user_assistant_msgs is only assigned inside the else branch
of a for-loop. If all messages have role == 'system', the else branch
never executes, and user_assistant_msgs remains undefined.

Fix: Initialize user_assistant_msgs = [] before the loop so the variable
always has a valid default value.
Jah-yee added a commit to Jah-yee/MetaGPT that referenced this pull request Mar 19, 2026
Fixes FoundationAgents#1960 - compress_messages() raises UnboundLocalError when all
messages are system messages or the message list is empty.

Root cause: user_assistant_msgs is only assigned inside the else branch
of a for-loop. If all messages have role == 'system', the else branch
never executes and the variable remains undefined.

Fix: Initialize user_assistant_msgs = [] before the loop so the variable
always has a valid default value.
@alvinttang alvinttang closed this Jun 7, 2026
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