Bug description
Using MessageChatMemoryAdvisor
with a default system prompt causes "conversation roles must alternate user/assistant/user/assistant" error. This appears to be the same issue as reported in #2216 which was marked as fixed in 1.0.0-M8
but can be reproduced in 1.0.1
.
Environment
- Spring AI version: 1.0.1
- Spring Boot version: 3.5.4
- Java version: 21.0.2
- AI Provider: OpenAI (Docker Model Runner with
ai/mistral:latest
model for testing) - Dependencies: spring-ai-starter-model-chat-memory, spring-ai-starter-model-openai
Steps to reproduce
- Create a
ChatClient
withdefaultSystem()
prompt andMessageChatMemoryAdvisor
configured - Make a chat call with a conversation id
- Attempt a second chat call with the same conversation id
- The second call fails with error "After the optional system message, conversation roles must alternate user/assistant/user/assistant..."
The error occurs during the second chatClient.prompt().call().content()
call. Upon debugging the MessageChatMemoryAdvisor#before
, I can see that messages are collected in the wrong order:
UserMessage
AssistantMessage
SystemMessage
UserMessage
Expected behavior
The MessageChatMemoryAdvisor
should properly order the system prompt and conversation context to allow for continued dialogue with alternating user/assistant roles without errors.
I would expect messages to be collected in the following order instead which I believe should allow the request to succeed:
SystemMessage
UserMessage
AssistantMessage
UserMessage
Minimal Complete Reproducible example
https://github.com/sjohnr/spring-ai-chat-memory-bug