spring-projects / spring-projects/spring-ai
ChatClient advisors NPE when context contains null values
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 6
Description
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
- Build a
ChatClientRequestwith a null context value, e.g.context("tenantId", null). - Use a
ChatClientwith default advisors (or directly useChatModelStreamAdvisor/ChatModelCallAdvisor). - Call
.stream(...)or.call(...). Map.copyOfthrows 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.javaspring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/ChatModelCallAdvisor.javaspring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/SafeGuardAdvisor.javaspring-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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
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.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100