Skip to content

chatModel cannot be null when adding SimpleLoggerAdvisor #2876

@mautini

Description

@mautini

Bug description
When creating a ChatClient without explicitly specifying a ChatModel (relying on auto-configuration), the call to the LLM fails if a default advisor is set (e.g., SimpleLoggerAdvisor). The following exception is thrown:

java.lang.IllegalArgumentException: chatModel cannot be null

Environment

  • Spring AI version: 1.0.0-SNAPSHOT
  • Java version: GraalVM 21.0.4 - VM 23.1.4
  • Impacted models: VertexAIGemini, Mistral

Steps to reproduce

  1. Create a ChatClient without specifying a ChatModel, using an injected ChatClient.Builder and calling chatClient.build().
  2. Add a default advisor (e.g., SimpleLoggerAdvisor).
  3. Call the LLM, specifying the model via the prompt.

Note: Everything works as expected if no default advisor is set.

Expected behavior
The call to the LLM should succeed, and the advisor should be invoked correctly during execution.

Minimal Complete Reproducible example

 package com.malt.platform.spring.test.controller

 import org.springframework.ai.chat.client.ChatClient
 import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor
 import org.springframework.ai.chat.prompt.ChatOptions
 import org.springframework.ai.chat.prompt.Prompt
 import org.springframework.web.bind.annotation.PostMapping
 import org.springframework.web.bind.annotation.RequestBody
 import org.springframework.web.bind.annotation.RequestMapping
 import org.springframework.web.bind.annotation.RestController

 @RestController
 @RequestMapping("/springai-test")
 class SpringAiTestController(
    chatClientBuilder: ChatClient.Builder,
 ) {
    private val chatClient: ChatClient = chatClientBuilder.defaultAdvisors(SimpleLoggerAdvisor()).build()

    @PostMapping("/springai")
    fun springAi(
        @RequestBody prompt: String,
    ): String? =
        chatClient
            .prompt(
                Prompt
                    .Builder()
                    .chatOptions(
                        ChatOptions.builder().model("gemini-2.0-flash-001").build(),
                    ).build(),
            ).user(prompt)
            .call()
            .content()
 }

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