{"record":{"id":"75880239d3713ed9","repo":"iflytek/astron-agent","slug":"messages-list-is-empty-cannot-get-last-message-content","errorCode":null,"errorMessage":"Messages list is empty, cannot get last message content","messagePattern":"Messages list is empty, cannot get last message content","errorType":"exception","errorClass":"AgentInternalExc","httpStatus":null,"severity":"error","filePath":"core/agent/api/schemas/base_inputs.py","lineNumber":107,"sourceCode":"                        \"msg\": \"messages must end with user type content\",\n                    }\n                ]\n            )\n\n        return values\n\n    def get_last_message_content(self) -> str:\n        \"\"\"\n        Safely get the content of the last message.\n\n        Returns:\n            str: Content of the last message\n\n        Raises:\n            AgentInternalExc: If messages list is empty\n        \"\"\"\n        if not self.messages:\n            raise AgentInternalExc(\n                \"Messages list is empty, cannot get last message content\"\n            )\n        return self.messages[-1].content\n\n    def get_last_message_content_safe(self, default: str = \"\") -> str:\n        \"\"\"\n        Safely get the content of the last message with a default value.\n\n        Args:\n            default: Default value to return if messages list is empty\n\n        Returns:\n            str: Content of the last message or default value\n        \"\"\"\n        if not self.messages:\n            return default\n        return self.messages[-1].content\n","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/agent/api/schemas/base_inputs.py#L89-L125","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Populate the messages list with at least one user message before invoking the chat completion path","Use get_last_message_content_safe(default=...) when an empty list is a legitimate state","Run validate_messages_params before calling this helper so empty input is rejected with a clear 4xx error"],"exampleFix":"# before\ncontent = request.get_last_message_content()\n\n# after\nif not request.messages:\n    return  # or return a 400 response\ncontent = request.get_last_message_content()","handlingStrategy":"type-guard","validationCode":"if not request.messages:\n    raise ValueError(\"messages must contain at least one message\")","typeGuard":"def has_messages(req) -> bool:\n    return bool(getattr(req, \"messages\", None))","tryCatchPattern":"try:\n    content = req.get_last_message_content()\nexcept AgentInternalExc:\n    content = req.get_last_message_content_safe(\"\")","preventionTips":["Prefer get_last_message_content_safe() in non-critical paths","Run validate_messages_params before reading messages","Never construct chat request objects with an empty messages list"],"tags":["internal-error","empty-list","chat"],"backgroundTag":"empty-required-field","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}