High severity beginner · Fix: 2-5 min

InvalidRequestError

anthropic.errors.InvalidRequestError

What this error means
Anthropic API rejects chat requests with an invalid or unexpected sequence of message roles in the conversation history.

Stack trace

traceback
anthropic.errors.InvalidRequestError: Invalid role sequence in messages: system role must not appear in messages array; use system parameter instead
    at anthropic.client.messages.create (client.py:123)
    at main.py:45
QUICK FIX
Remove 'system' role messages from messages list and pass system prompt via the system parameter in client.messages.create().

Why it happens

Anthropic's chat API expects the system prompt to be passed separately via the 'system' parameter, not as a message with role 'system' in the messages list. Including a 'system' role message in the messages array violates the expected role sequence and triggers this error.

Detection

Validate that your messages array contains only 'user' and 'assistant' roles before sending the request, and ensure the system prompt is passed via the dedicated 'system' parameter.

Causes & fixes

1

Including a message with role='system' inside the messages list instead of using the system parameter.

✓ Fix

Remove any 'system' role messages from the messages list and pass the system prompt text via the 'system' argument in client.messages.create().

2

Mixing Anthropic SDK usage patterns by passing system prompt both as a message and as the system parameter.

✓ Fix

Use only the 'system' parameter for system prompts and keep messages list roles strictly as 'user' or 'assistant'.

3

Copying OpenAI chat message format directly, which includes 'system' role in messages, incompatible with Anthropic SDK v0.20+.

✓ Fix

Adapt your code to Anthropic's SDK by removing 'system' role messages and using the separate system parameter.

Code: broken vs fixed

Broken - triggers the error
python
from anthropic import Anthropic
import os

client = Anthropic(api_key=os.environ['ANTHROPIC_API_KEY'])

messages = [
    {"role": "system", "content": "You are a helpful assistant."},  # This causes the error
    {"role": "user", "content": "Hello!"}
]

response = client.messages.create(
    model="claude-2",
    messages=messages  # InvalidRequestError triggered here
)
print(response)
Fixed - works correctly
python
from anthropic import Anthropic
import os

client = Anthropic(api_key=os.environ['ANTHROPIC_API_KEY'])

messages = [
    {"role": "user", "content": "Hello!"}
]

response = client.messages.create(
    model="claude-2",
    system="You are a helpful assistant.",  # Moved system prompt here
    messages=messages  # Only user and assistant roles here
)
print(response)  # Fixed: no InvalidRequestError
Removed 'system' role message from messages list and passed system prompt via the dedicated system parameter as required by Anthropic SDK.

Workaround

Catch InvalidRequestError and strip out any 'system' role messages from the messages list before retrying the request with the system prompt passed separately.

Prevention

Always use the 'system' parameter for system prompts with Anthropic's chat API and keep messages list roles limited to 'user' and 'assistant' to avoid role sequence errors.

Python 3.9+ · anthropic >=0.20.0 · tested on 0.20.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.