Ever tried integrating an AI into your app and suddenly hit this odd message: “Agent 1: Last message was not an assistant message”? First, don’t panic. It happens more often than you think. This error usually pops up when something’s off in the way you’re structuring your messages in the API call.
TLDR: This error means that your last message was not marked as being from the “assistant”, even though the system expected it to be. It’s a message sequencing issue. To fix it, make sure your chat messages follow the right pattern, usually starting with the system or user and then alternating with the assistant. Let’s walk through what that looks like and how to fix it step by step.
What Does This Error Actually Mean?
When you communicate with OpenAI’s chat models, like GPT-3.5 or GPT-4, you send a list of messages. Each message has a role: system, user, or assistant.
The error “Agent 1: Last message was not an assistant message” usually shows up in setups that simulate conversations between different agents or AI assistants. One agent is expecting a reply from another, but the message it sees isn’t marked as coming from an assistant.
This messes up the flow of the conversation. Think of it like trying to play catch with no ball in motion. 🥎
Why It Happens
Let’s break it down:
- You’re using a multi-agent setup (often in function calling or multi-turn chats).
- One AI agent sends a message, expecting a reply from another.
- But your code sent a message labeled as user instead of assistant.
The system is strict about this because it needs to know who said what.
How to Fix It
This is the fun part! You don’t need to rewrite your codebase. Just follow these simple steps:
Step 1: Understand the Proper Message Format
A valid conversation should look like this:
[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What's the weather today?"},
{"role": "assistant", "content": "It's sunny and 75°F."},
{"role": "user", "content": "Should I bring an umbrella?"}
]
If your message list jumps from user to user or system to user with no assistant message in between, you’ll run into problems.
Step 2: Check How You’re Generating Messages
If you’re using a loop or dynamically building your message history, check the roles of each entry.
Here’s what to look out for:
- All AI outputs should be marked as assistant.
- User inputs should always be user.
- No AI reply? Then your next agent won’t know how to respond.
Remember, structure matters!
Step 3: Fix the Message Role
Here’s a quick way to fix the issue:
- Find the part of your code where you append new messages.
- If the message is coming from an AI agent, set
role: "assistant". - If it’s from a user or external input, set
role: "user".
Example fix:
# BEFORE
messages.append({"role": "user", "content": result_from_agent})
# AFTER
messages.append({"role": "assistant", "content": result_from_agent})
Bonus Tip: Use Logs to Trace Chat Flow
Sometimes your message stack grows and problems sneak in quietly. Logging helps!
Add a simple print or log just before submitting your messages:
for message in messages:
print(f"[{message['role']}] {message['content']}")
This gives you a readable view of who said what—and when.
Checklist: Make Sure You’re Doing It Right
Before sending messages to the API, check these:
- ✅ Messages alternate correctly (user, assistant, user, assistant…)
- ✅ No two users or two assistants in a row unless absolutely intended
- ✅ Each agent response is properly labeled
- ✅ You’re not skipping assistant replies in multi-agent setups
Real Example: Before and After
Before (Broken)
[
{"role": "user", "content": "Hello!"},
{"role": "user", "content": "Can you help me?"}
]
This will give you the error. Why? Because nothing has responded yet!
After (Fixed)
[
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi! How can I help you?"},
{"role": "user", "content": "Can you help me?"}
]
Now the assistant has spoken. The flow is restored. 🎉
Using Multi-Agent or Tool-Using Prompts?
This error is even more common when chaining assistant calls or using APIs like function_call or tools. Each tool or agent is expected to be a self-contained assistant.
If you’re orchestrating multiple agents, make sure each one’s output is marked correctly:
{"role": "assistant", "content": "Agent A recommends Plan X."}
Not marking it as an assistant message means the next agent gets confused and yells, “That wasn’t from my AI buddy!”
Still Getting the Error?
Here are a few last things to check:
- You’re using the latest OpenAI API version. Old SDKs might handle things differently.
- If using a framework or wrapper, double-check how it’s labeling roles internally.
- Try simplifying: reduce your conversation to 3-4 messages and test from there.
Conclusion
The “Agent 1: Last message was not an assistant message” error sounds scary, but it’s just a labeling issue. Once you form the habit of alternating user and assistant messages correctly, you’ll rarely see it again.
Your friendly AI agents will be chatting like pros—and errors like these will be a thing of the past.
Happy fixing! 🚀