Role Validation & Boundaries — Messages API Core
Role Validation & Boundaries
The Strict Role Model
Anthropic enforces a strict alternating role contract within the messages array. Unlike other providers, you cannot send consecutive 'user' or 'assistant' messages. Every sequence must start with a user role. If your application logic requires multiple user interjections without assistant replies, you must concatenate these strings into a single content block or utilize the system prompt for persistent context.
Structural Partitioning
The system parameter is physically isolated from the messages array. This isn't just a naming convention-it is a security boundary that helps Claude distinguish between developer-mandated constraints and potentially untrusted user data. When building for production, always place critical behavioral rules in the system prompt to minimize the risk of 'prompt injection' where a user might attempt to override instructions within the conversation stream.
user content field becomes an array of typed blocks ("text", "image", or "document"), where visual/document blocks specify a structured source object (base64, url, or Files API file).Anthropic requires strictly alternating user and assistant messages. Submit an alternating multi-turn conversation with curl to verify the Messages API contract.