iflytek/astron-agent · error · AgentInternalExc

Messages list is empty, cannot get last message content

Error message

Messages list is empty, cannot get last message content

What it means

AgentInternalExc raised by get_last_message_content when self.messages is empty. The helper assumes at least one message exists so it can index [-1]; an empty list would otherwise raise a bare IndexError, so the code raises an explicit internal exception indicating a caller bug rather than a user-input problem.

Solutions

  1. Populate the messages list with at least one user message before invoking the chat completion path
  2. Use get_last_message_content_safe(default=...) when an empty list is a legitimate state
  3. Run validate_messages_params before calling this helper so empty input is rejected with a clear 4xx error

Example fix

# before
content = request.get_last_message_content()

# after
if not request.messages:
    return  # or return a 400 response
content = request.get_last_message_content()
Defensive patterns

Strategy: type-guard

Validate before calling

if not request.messages:
    raise ValueError("messages must contain at least one message")

Type guard

def has_messages(req) -> bool:
    return bool(getattr(req, "messages", None))

Try / catch

try:
    content = req.get_last_message_content()
except AgentInternalExc:
    content = req.get_last_message_content_safe("")

Prevention

When it happens

Trigger: Calling custom_chat_completions (or any code path that calls get_last_message_content) with a request whose messages array is [] after validation, bypassing validate_messages_params.

Common situations: Internal callers constructing request objects programmatically with no messages; tests or tools that skip the validation layer; race/logic bugs that drain the message list before reading the last message.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/75880239d3713ed9. Report an issue: GitHub.

Appendix: source

Thrown at core/agent/api/schemas/base_inputs.py:107

                        "msg": "messages must end with user type content",
                    }
                ]
            )

        return values

    def get_last_message_content(self) -> str:
        """
        Safely get the content of the last message.

        Returns:
            str: Content of the last message

        Raises:
            AgentInternalExc: If messages list is empty
        """
        if not self.messages:
            raise AgentInternalExc(
                "Messages list is empty, cannot get last message content"
            )
        return self.messages[-1].content

    def get_last_message_content_safe(self, default: str = "") -> str:
        """
        Safely get the content of the last message with a default value.

        Args:
            default: Default value to return if messages list is empty

        Returns:
            str: Content of the last message or default value
        """
        if not self.messages:
            return default
        return self.messages[-1].content

View on GitHub (pinned to 5e758547a8)