fix(spring-ai): declare ordering for SpringAIAutoConfiguration so @ConditionalOnBean matches Spring AI model beans - #1502
Open
kongxubihai wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
… auto-configurations
kongxubihai
force-pushed
the
fix/spring-ai-autoconfig-ordering
branch
from
September 11, 2026 05:35
7d52df7 to
4c434a6
Compare
Contributor
|
Hi @kongxubihai, thank you for your contribution. We appreciate you taking the time to submit this pull request. Currently this PR is under review by our team and we will keep you posted if any additional information is required. thank you. |
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 #1501
Problem
SpringAIAutoConfiguration(contrib/spring-ai) guards all four of its@Beanmethods with@ConditionalOnBean— threeSpringAIvariants onChatModel/StreamingChatModel, andspringAIEmbeddingonEmbeddingModel— but declares no ordering relative to theauto-configurations that register those beans.
The result of
@ConditionalOnBeandepends on what has been processed so far. With nodeclared ordering, evaluation order falls back to the alphabetical class-name sort, and
com.google.adk...sorts beforeorg.springframework.ai...— so ADK's conditions areevaluated before any Spring AI model bean definition exists. No
SpringAIbean isregistered and applications fail to start:
springAIEmbeddingis affected identically — silently: no error, just a missing bean.Fix
Declare
afterNameon@AutoConfiguration, listing the Spring AI model auto-configurationsthat may provide
ChatModel,StreamingChatModel, orEmbeddingModelbeans(7 chat + 9 embedding entries in the commit).
Design notes:
afterName(string) and notafter(Class): this module compiles only againstspring-ai-model; provider auto-configuration classes are not on the compile classpath.Names that do not exist on the classpath are ignored by the sorter, which makes string
form the intended mechanism here — the same pattern Spring AI 1.x's own
ChatClientAutoConfigurationused for the identical problem.META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports(the authoritative registration file — class-path/name-pattern guessing misses
nested packages such as
google.genai.autoconfigure.chatorvertexai.autoconfigure.embedding).javap-verified the bean type of every configuration whose name alone is notsufficient:
GoogleGenAiChatModel implements ChatModel;GoogleGenAiTextEmbeddingModel/VertexAiTextEmbeddingModelextendAbstractEmbeddingModel.*Connection*AutoConfiguration(google-genai, vertex-ai) — register connection-detailsbeans, not models;
VertexAiMultiModalEmbeddingAutoConfiguration— registers aDocumentEmbeddingModel,which does not satisfy
@ConditionalOnBean(EmbeddingModel);ElevenLabsAutoConfiguration— registers a text-to-speech model, not chat/embedding;OllamaApiAutoConfigurationand the image/audio/moderation/OCR configurations —unrelated interfaces.
same standing cost Spring AI 1.x accepted for this pattern. Modules introduced after
2.0.1 are ignored harmlessly until added.
Test
New
SpringAIAutoConfigurationOrderingTestplacesSpringAIAutoConfigurationfirst inAutoConfigurations.of(...)alongside the provider configurations andToolCallingAutoConfiguration(the latter provides theToolCallingManagerthatOpenAiChatAutoConfigurationrequires; a real application imports it automatically, thecontext runner must declare it explicitly).
AutoConfigurations.ofapplies the same ordering rules as production auto-configurationimport (
AutoConfigurationSorter#getInPriorityOrder), so:SpringAIAutoConfigurationbefore the provider configurations, so@ConditionalOnBeannever matches.
singleton pre-instantiation logs change from
springAIEmbeddingbeing created beforeopenAiChatModel(alphabetical order, conditions evaluated too early) to OpenAI beansbeing created first followed by
Auto-configuring SpringAI....Notes
An alternative fix — dropping
@ConditionalOnBeanin favor of@Beanmethod parameterinjection, as Spring AI 2.0's own
ChatClientAutoConfigurationdoes — would also removethe ordering sensitivity, but changes semantics: with no model present, failure moves from
a clear startup report to a less friendly bean-creation error. The
afterNamedeclarationis the smaller, behavior-preserving change.
Google CLA signed.