spring-projects / spring-projects/spring-ai

MistralAiChatModel returns an error if MessageChatMemoryAdvisor is used

Open
#2,380 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Bug description
If I use MistralAiChatModel with MessageChatMemoryAdvisor + InMemoryChatMemory, at the second iteration, I get the following erorr:
Unexpected role 'system' after role 'assistant'

At the second iteration the system role appears after the previous user role and assistant role messages and before the current user role message.

"messages": [
    {
      "content": "first user message is here",
      "role": "user"
    },
    {
      "content": "first assistant message is here",
      "role": "assistant"
    },
    {
      "content": "   You are an assistant helping people to buy tickets for trains, give the answer with the same language of the request.\n   The answer must be formated as MarkDown.\n",
      "role": "system"
    },
    {
      "content": "currentuser message is here",
      "role": "user"
    }
  ]

Environment
Spring AI 1.0.0-M6, Java version 21

Steps to reproduce
add the following fragment to the pom.xml

        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-mistral-ai-spring-boot-starter</artifactId>
        </dependency>

use the following fragment of code

ChatClient.Builder builder
        chatClient = builder
                .defaultAdvisors(new MessageChatMemoryAdvisor(new InMemoryChatMemory()))
                .build();

Expected behavior
MistralAi LLM wants the system role to be the first one and this can be a general rule for all LLM.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with MistralAiChatModel and the MessageChatMemoryAdvisor/InMemoryChatMemory combination described in the reproduction, using the spring-ai-mistral-ai-spring-boot-starter dependency from pom.xml. Reproduce the second chat iteration and inspect the message ordering. Done means the system message is placed first and the Mistral request no longer returns the unexpected-role error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.