Skip to content

Conversation

@Nhahan
Copy link

@Nhahan Nhahan commented Nov 25, 2025

Fixes #4952

Approach

  • Fail fast on bad input by disallowing null context keys/values in ChatClientRequest.
  • Extend the same guardrails to ChatClientResponse for consistency, so direct response creation can’t sneak in nulls.
  • Keep advisor code unchanged; they can continue to use Map.copyOf safely.

Scope

  1. ChatClientRequest: constructor/builder enforce non-null keys and values (including single key/value adder).
  2. ChatClientResponse: constructor/builder enforce non-null keys and values for symmetry.
  3. Tests:
    • Requests: null keys/values throw clear IllegalArgumentException.
    • Responses: null keys/values throw clear IllegalArgumentException.
    • Valid inputs still succeed; immutability semantics remain.

Test plan

  • ./mvnw -pl spring-ai-client-chat -DskipITs test (module scope).
  • Unit tests in ChatClientRequestTests and ChatClientResponseTests cover null key/value rejection.

Backward compatibility

  • This change enforces non-null values in the context map.
  • Existing code that passes null values will now throw IllegalArgumentException immediately at construction time, rather than causing an NPE later in Advisors.

Add validation to reject null values in the context map of
ChatClientRequest and ChatClientResponse. This prevents
NullPointerException when advisors use Map.copyOf() to copy
the context, as Map.copyOf() does not allow null values.

- Add null value validation in record constructors
- Add null key and value validation in Builder.context(Map)
- Add null value validation in Builder.context(String, Object)
- Add corresponding tests for all new validations

Closes spring-projectsgh-4952

Signed-off-by: Nhahan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChatClient advisors NPE when context contains null values

1 participant