Skip to content

Keep params and metadata in ChatClient.mutate() - #7322

Open
bellCold wants to merge 1 commit into
spring-projects:mainfrom
bellCold:fix/chatclient-mutate-params
Open

bellCold wants to merge 1 commit into
spring-projects:mainfrom
bellCold:fix/chatclient-mutate-params

Conversation

@bellCold

@bellCold bellCold commented Nov 4, 2026 •

Copy link
Copy Markdown

Problem

ChatClient.mutate() loses some default settings.
It copies them only under conditions that are not related to them:

Setting Copied only when
Advisor params the client has at least one advisor
User params, media and metadata the client has default user text
System params and metadata the client has default system text

So if you set one of these defaults on its own, the mutated client does not have it.

Example

ChatClient client = ChatClient.builder(chatModel)
	.defaultAdvisors(AdvisorParams.ENABLE_NATIVE_STRUCTURED_OUTPUT)
	.defaultUser(u -> u.param("lang", "fr").metadata("k", "v"))
	.build();

ChatClient mutated = client.mutate().build();
// In "mutated", the advisor params, user params and user metadata are all empty.

Real effects:

  • A client that enables native structured output with AdvisorParams.ENABLE_NATIVE_STRUCTURED_OUTPUT loses it after mutate().
  • A client that turns off AdvisorParams.toolCallingAdvisorAutoRegister(false) gets the ToolCallingAdvisor registered again after mutate().

Cause

The advisor params were added to mutate() in #3459, inside the existing check for advisors.
User and system params were already inside the checks for user and system text.

Fix

mutate() now always copies advisor params, user and system params, media and metadata.
It still sets the user and system text only when there is text.

Tests

  • The existing testMutate always set both advisors and text, so it did not catch this.
  • The new testMutateKeepsParamsWithoutAdvisorsOrText fails without this change and passes with it.
  • All unit tests in spring-ai-client-chat pass.

DefaultChatClientRequestSpec.mutate() copied advisor params only when
there were advisors, and user or system params, media and metadata only
when there was user or system text. Defaults set on their own were
lost: for example a client built with
AdvisorParams.ENABLE_NATIVE_STRUCTURED_OUTPUT and no advisors lost
native structured output once mutated.

Copy them whether or not advisors or text are set.

Signed-off-by: bellCold <whdcks420@gmail.com>
@bellCold
bellCold force-pushed the fix/chatclient-mutate-params branch from e3ece41 to 3dada7f Compare November 4, 2026 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants