ChatClient advisors NPE when context contains null values
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 74/100
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
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.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 5
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.
More from spring-projects/spring-ai
-
status: waiting-for-triage
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
spring-projects/spring-ai#7022 · 2 comments ·
-
status: waiting-for-triage
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
spring-projects/spring-ai#7000 ·
-
status: waiting-for-triage
Difficulty 1/5 Under an hour Newbie friendliness 90/100
spring-projects/spring-ai#6998 ·
-
[Bug - MCP server] @McpTool error messages are emitted twice when the thrown exception has no cause Openstatus: waiting-for-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
spring-projects/spring-ai#6948 · 1 comment · 1 reaction ·
-
status: waiting-for-triage
Difficulty 1/5 Under an hour Newbie friendliness 90/100
spring-projects/spring-ai#6940 · 1 comment ·
All issues in spring-projects/spring-ai
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
infinispan/infinispan#18150 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
opensearch-project/k-NN#3597 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100