spring-projects / spring-projects/spring-ai

Add ChatClient getChatModel() method

Open
#3,790 2 comments 0 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

Expected Behavior

When injecting the ChatClient in any service I want to log the actual underlying model being used. Because we have multiple ChatClients of different underlying models, we want to log the actual model being used in the service that uses the ChatClient. E.g.

public class QcrIntentAgent {

    @Autowired
    private ChatClient chatClient;


    public void doSomething(){
        log.debug("LLM being used: {}", chatClient.getChatModel().getModel());
       // E.g.: 
       // LLM being used: gpt-4.1-nano 
       // LLM being used: gpt-4o-mini 
       // LLM being used: mistral  
       //. LLM being used: llama3  
       chatClient.prompt(...);
    }

Current Behavior

There is no clean way of getting this info besides injecting the ChatModel itself. Resulting in either injecting both ChatModel and ChatClient OR injecting just the model and creating a new ChatClient here.

    @Autowired
    private ChatClient chatClient;

    @Autowired
    private ChatModel chatModel;

     public void doSomething(){
        log.debug("LLM being used: {}", chatModel.getDefaultOptions().getModel());
         chatClient.prompt(...);
     }

// OR


   @Autowired
    private ChatModel chatModel;

    public void doSomething(){
        log.debug("LLM being used: {}", chatModel.getDefaultOptions().getModel());
        ChatClient.builder(chatModel).build().prompt(...);
     }


Context

Alternatives are see Current Behavior.
Creating a utility class

@UtilityClass
public class ChatClientUtils {

   /**
    * Returns the model configured for the ChatClient.
    * This workaround is needed because Spring AI 1.0.0 does not expose the model via public API.
    * WARNING: This depends on the internal structure of DefaultChatClient.
    */
   public static String getModel(ChatClient chatClient) {
       if (chatClient instanceof DefaultChatClient defaultClient) {
           DefaultChatClient.DefaultChatClientRequestSpec requestSpec = (DefaultChatClient.DefaultChatClientRequestSpec) defaultClient.prompt("Hello");
           return requestSpec.getChatOptions().getModel();
       } else {
           throw new IllegalArgumentException("Unsupported ChatClient implementation: " + chatClient.getClass().getName());
       }
   }
}

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 the ChatClient and DefaultChatClient entry points, then inspect how the underlying ChatModel and model options are exposed. Done means callers can obtain and log the configured model through ChatClient without injecting a second ChatModel or depending on DefaultChatClient internals.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.