{"record":{"id":"779c98d554e14a11","repo":"iflytek/astron-agent","slug":"messages-must-end-with-user-type-content","errorCode":null,"errorMessage":"messages must end with user type content","messagePattern":"messages must end with user type content","errorType":"validation","errorClass":"RequestValidationError","httpStatus":422,"severity":"error","filePath":"core/agent/api/schemas/base_inputs.py","lineNumber":84,"sourceCode":"                # Wrong order\n                raise RequestValidationError(\n                    errors=[\n                        {\n                            \"type\": \"literal_error\",\n                            \"loc\": (\"body\", \"messages\", i, \"role\"),\n                            \"msg\": (\n                                \"messages role order must alternate \"\n                                \"between user and assistant\"\n                            ),\n                        }\n                    ]\n                )\n\n            next_role = \"assistant\" if next_role == \"user\" else \"user\"\n\n        if next_role != \"assistant\":\n            # Last message is not user\n            raise RequestValidationError(\n                errors=[\n                    {\n                        \"type\": \"literal_error\",\n                        \"loc\": (\"body\", \"messages\"),\n                        \"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","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/agent/api/schemas/base_inputs.py#L66-L102","documentation":"Pydantic-style RequestValidationError raised by validate_messages_params when the last message in the `messages` array does not have role 'user'. Chat completion APIs require the conversation to end with a user turn so the model has something to respond to; an assistant-terminated or empty-role-terminated list is rejected at schema validation time.","triggerScenarios":"Calling the chat endpoint with a messages array whose final element has role 'assistant' (or any non-'user' role), e.g. appending a pre-written assistant reply and sending it back, or building messages from templates that end with a system/assistant turn.","commonSituations":"Replaying stored conversation history that was saved after the assistant answered; client-side chat UIs that append assistant echoes into state and re-submit; prompt-chaining code that feeds assistant output forward without appending a new user prompt.","solutions":["Ensure the final element of the messages array has role set to 'user' before calling the API","Append a new user message (e.g. the actual question or 'continue') after any trailing assistant message","Strip trailing assistant/system messages from history before submission if they are not needed as context"],"exampleFix":"// before\nmessages = [\n  {\"role\": \"user\", \"content\": \"hi\"},\n  {\"role\": \"assistant\", \"content\": \"hello\"}\n]\nclient.chat(messages=messages)\n\n// after\nmessages.append({\"role\": \"user\", \"content\": \"tell me more\"})\nclient.chat(messages=messages)","handlingStrategy":"validation","validationCode":"def ends_with_user(messages):\n    return bool(messages) and messages[-1].get(\"role\") == \"user\"\n\nif not ends_with_user(messages):\n    messages.append({\"role\": \"user\", \"content\": user_input})","typeGuard":"def is_user_last(messages: list[dict]) -> bool:\n    return len(messages) > 0 and messages[-1].get(\"role\") == \"user\"","tryCatchPattern":null,"preventionTips":["Always append the user turn last when building message lists programmatically","Keep assistant echoes out of the submitted history or re-append a user prompt after them","Validate the messages shape client-side before each request"],"tags":["validation","chat","request-schema"],"backgroundTag":"schema-validation-failed","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"}