Add StructuredOutputConverterFactory to ChatClient#6618
Open
JHPrk wants to merge 1 commit into
Open
Conversation
Add a configurable generic factory for type-based ChatClient entity conversion while preserving the existing BeanOutputConverter behavior. Propagate the factory through builder cloning, client mutation, and request copies. Preserve compatibility for third-party builder implementations with a default method that fails explicitly when the configuration is unsupported. Cover explicit converter bypass, plain responses, native structured output, schema validation, and unsupported builder behavior. See spring-projects#6616 Signed-off-by: Jaehyeon Park <qkrwogus1236@gmail.com>
StructuredOutputConverterFactory to ChatClientAdd structured output converter factoryStructuredOutputConverterFactory to ChatClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6616
Summary
This PR adds a configurable
StructuredOutputConverterFactoryfor thetype-based
ChatClientconversion methods.The factory is used by the
Class<T>andParameterizedTypeReference<T>variants of
entity(...)andresponseEntity(...), including theirEntityParamSpecoverloads.When no factory is configured, the existing behavior is preserved and
BeanOutputConverterremains the default. Overloads that receive an explicitStructuredOutputConverter<T>continue to use that converter directly, andplain response methods are unaffected.
Design
The factory preserves the relationship between the requested target type and
the converter type:
StructuredOutputConverterFactory.beanOutputConverter(...)providesconvenience factory methods for applying a shared
JsonMapperand optionalResponseTextCleaner.The configured factory is preserved through builder cloning, request copying,
and
ChatClient.mutate(). The created converter also participates inprompt-based formatting, provider-native structured output, and schema
validation.
The new
ChatClient.Buildermethod has a default implementation that throwsUnsupportedOperationException. This avoids requiring existing third-partybuilder implementations to add the new method while ensuring that unsupported
configuration is not silently ignored.
DefaultChatClientBuilderoverridesthe method with the supported implementation.
I am open to a different configuration point or compatibility strategy if it
fits the existing
ChatClientAPI better.Testing
./mvnw process-sources./mvnw -pl spring-ai-client-chat -am -Dtest=StructuredOutputConverterFactoryTests -Dsurefire.failIfNoSpecifiedTests=false test./mvnw -pl spring-ai-client-chat -am test./mvnw clean package