ChatClient advisors NPE when context contains null values

Open Beginner friendly
#4,952 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
java
Domain
api

Research direction

Start with ChatClientRequest.java and inspect how context entries are validated, then review the Map.copyOf call sites in ChatModelStreamAdvisor.java, ChatModelCallAdvisor.java, and SafeGuardAdvisor.java. Add or update a test based on ChatClientRequestNullContextTests to verify that a null context value fails with a clear validation error before advisor execution.

Written by the indexing model from the issue text.

Description

status: to-discuss status: waiting-for-triage

Bug description

ChatClientRequest only forbids null keys, not null values. The advisors (ChatModelStreamAdvisor, ChatModelCallAdvisor, SafeGuardAdvisor) call Map.copyOf(request.context()), and that blows up with NullPointerException if any value is null. The call/stream fails before the model is invoked.

Steps to reproduce

  1. Build a ChatClientRequest with a null context value, e.g. context("tenantId", null).
  2. Use a ChatClient with default advisors (or directly use ChatModelStreamAdvisor/ChatModelCallAdvisor).
  3. Call .stream(...) or .call(...).
  4. Map.copyOf throws NPE inside the advisor.

Expected behavior

Either reject the request up front with a clear validation error, or avoid throwing NPE in the advisors when context contains null values.

Minimal complete reproducible example

class ChatClientRequestNullContextTests {

    @Test
    void nullContextValueTriggersNpeInAdvisor() {
        ChatClientRequest request = ChatClientRequest.builder()
            .prompt(new Prompt("hi"))
            .context("tenantId", null) // null value
            .build();

        // Same failure point as in the advisors:
        assertThatThrownBy(() -> Map.copyOf(request.context()))
            .isInstanceOf(NullPointerException.class);
    }
}

Affected code

  • spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/ChatModelStreamAdvisor.java
  • spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/ChatModelCallAdvisor.java
  • spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/SafeGuardAdvisor.java
  • spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/ChatClientRequest.java

Proposed resolution

Tighten ChatClientRequest validation to disallow null values in context, so invalid requests fail fast with a clear message, and the advisors can keep their immutable Map.copyOf calls.

Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 10h
Merged PRs (30d)
5

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.

More from spring-projects/spring-ai

All issues in spring-projects/spring-ai

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.