Skip to content

AdvisedRequest.userText is not populated under some ChatClient Configurations. #2408

Closed
@cpage-pivotal

Description

@cpage-pivotal

Bug description
Under some configurations of ChatClient, AdvisedRequest.userText is not populated, resulting in a message like this:

java.lang.IllegalArgumentException: Content must not be null for SYSTEM or USER messages
	at org.springframework.util.Assert.notNull(Assert.java:181) ~[spring-core-6.2.3.jar:6.2.3]
	at org.springframework.ai.chat.messages.AbstractMessage.<init>(AbstractMessage.java:69) ~[spring-ai-core-1.0.0-M6.jar:1.0.0-M6]
	at org.springframework.ai.chat.messages.UserMessage.<init>(UserMessage.java:62) ~[spring-ai-core-1.0.0-M6.jar:1.0.0-M6]
	at org.springframework.ai.chat.messages.UserMessage.<init>(UserMessage.java:49) ~[spring-ai-core-1.0.0-M6.jar:1.0.0-M6]
	at org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor.before(MessageChatMemoryAdvisor.java:97) ~[spring-ai-core-1.0.0-M6.jar:1.0.0-M6]

Environment
Spring ai 1.0.0-M6
Java 21

Steps to reproduce
This simple code reproduces the issue

    @Bean
    ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

    @Bean
    CommandLineRunner commandLineRunner(ApplicationContext ctx, ChatClient.Builder chatClientBuilder, ChatMemory chatMemory) {
        return args -> {
            ChatClient chatClient = chatClientBuilder.
                    defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory), new SimpleLoggerAdvisor()).
                    defaultAdvisors(a -> a.param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10)).
                    build();

            UserMessage userMessage = new UserMessage("Hello");
            SystemMessage systemMessage = new SystemMessage("You are a poet. Respond in rhymes.");
            Prompt prompt = new Prompt(userMessage,systemMessage);

            String content = chatClient
                    .prompt(prompt)
                    .call()
                    .content();
            System.out.println(content);
        };
    }

Expected behavior
Code should successfully complete chat request.

Minimal Complete Reproducible example
You can fetch and run this repo with the code: https://github.com/cpage-pivotal/advised-request-issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions