Skip to content

Add StructuredOutputConverterFactory to ChatClient#6618

Open
JHPrk wants to merge 1 commit into
spring-projects:mainfrom
JHPrk:poc/structured-output-converter-factory
Open

Add StructuredOutputConverterFactory to ChatClient#6618
JHPrk wants to merge 1 commit into
spring-projects:mainfrom
JHPrk:poc/structured-output-converter-factory

Conversation

@JHPrk

@JHPrk JHPrk commented Jul 14, 2026

Copy link
Copy Markdown

Fixes #6616

Summary

This PR adds a configurable StructuredOutputConverterFactory for the
type-based ChatClient conversion methods.

The factory is used by the Class<T> and ParameterizedTypeReference<T>
variants of entity(...) and responseEntity(...), including their
EntityParamSpec overloads.

When no factory is configured, the existing behavior is preserved and
BeanOutputConverter remains the default. Overloads that receive an explicit
StructuredOutputConverter<T> continue to use that converter directly, and
plain response methods are unaffected.

Design

The factory preserves the relationship between the requested target type and
the converter type:

<T> StructuredOutputConverter<T> create(
        ParameterizedTypeReference<T> targetType);

StructuredOutputConverterFactory.beanOutputConverter(...) provides
convenience factory methods for applying a shared JsonMapper and optional
ResponseTextCleaner.

The configured factory is preserved through builder cloning, request copying,
and ChatClient.mutate(). The created converter also participates in
prompt-based formatting, provider-native structured output, and schema
validation.

The new ChatClient.Builder method has a default implementation that throws
UnsupportedOperationException. This avoids requiring existing third-party
builder implementations to add the new method while ensuring that unsupported
configuration is not silently ignored. DefaultChatClientBuilder overrides
the method with the supported implementation.

I am open to a different configuration point or compatibility strategy if it
fits the existing ChatClient API 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

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>
@JHPrk JHPrk changed the title Add StructuredOutputConverterFactory to ChatClientAdd structured output converter factory Add StructuredOutputConverterFactory to ChatClient Jul 14, 2026
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.

Support configurable StructuredOutputConverter resolution for type-based ChatClient methods

2 participants