iflytek/astron-agent · error · RequestValidationError

messages must end with user type content

Error message

messages must end with user type content

What it means

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.

Solutions

  1. Ensure the final element of the messages array has role set to 'user' before calling the API
  2. Append a new user message (e.g. the actual question or 'continue') after any trailing assistant message
  3. Strip trailing assistant/system messages from history before submission if they are not needed as context

Example fix

// before
messages = [
  {"role": "user", "content": "hi"},
  {"role": "assistant", "content": "hello"}
]
client.chat(messages=messages)

// after
messages.append({"role": "user", "content": "tell me more"})
client.chat(messages=messages)
Defensive patterns

Strategy: validation

Validate before calling

def ends_with_user(messages):
    return bool(messages) and messages[-1].get("role") == "user"

if not ends_with_user(messages):
    messages.append({"role": "user", "content": user_input})

Type guard

def is_user_last(messages: list[dict]) -> bool:
    return len(messages) > 0 and messages[-1].get("role") == "user"

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

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

                # Wrong order
                raise RequestValidationError(
                    errors=[
                        {
                            "type": "literal_error",
                            "loc": ("body", "messages", i, "role"),
                            "msg": (
                                "messages role order must alternate "
                                "between user and assistant"
                            ),
                        }
                    ]
                )

            next_role = "assistant" if next_role == "user" else "user"

        if next_role != "assistant":
            # Last message is not user
            raise RequestValidationError(
                errors=[
                    {
                        "type": "literal_error",
                        "loc": ("body", "messages"),
                        "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

View on GitHub (pinned to 5e758547a8)