Skip to content

fix(spring-ai): order SpringAI auto-configuration after Spring AI mod… - #1505

Open
Lubaoshuai wants to merge 1 commit into
google:mainfrom
Lubaoshuai:fix/springai-autoconfig-ordering
Open

fix(spring-ai): order SpringAI auto-configuration after Spring AI mod…#1505
Lubaoshuai wants to merge 1 commit into
google:mainfrom
Lubaoshuai:fix/springai-autoconfig-ordering

Conversation

@Lubaoshuai

Copy link
Copy Markdown

Closes #1501

Root cause

SpringAIAutoConfiguration guards its @Bean methods with @ConditionalOnBean(ChatModel/StreamingChatModel/EmbeddingModel), but declared no ordering relative to the Spring AI model auto-configurations that register those beans. @ConditionalOnBean is evaluated when the configuration is processed, and in a real application the auto-configurations are otherwise sorted alphabetically, which puts com.google.adk.models.springai.autoconfigure.SpringAIAutoConfiguration before org.springframework.ai.model.*.autoconfigure.*AutoConfiguration — every condition misses, and all SpringAI/SpringAIEmbedding beans are silently skipped (the springAIEmbedding failure being fully silent, as noted in the issue).

Since this module intentionally compiles only against spring-ai-model (no provider modules on the compile classpath), the class-based after attribute is not usable; the string-based afterName attribute exists exactly for this case, and unknown class names are ignored, so versions that lack a listed configuration are unaffected. This mirrors the approach Spring AI 1.x's own ChatClientAutoConfiguration used for the identical @ConditionalOnBean(ChatModel) situation.

The list covers the chat/embedding auto-configurations that exist in Spring AI 2.0.x, verified against each module's META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports at v2.0.1:

  • chat: anthropic, bedrock converse, deepseek, google genai, mistralai, ollama, openai
  • embedding: bedrock cohere/titan, google genai, mistralai, ollama, openai, postgresml, transformers, vertex ai

Testing

New SpringAIAutoConfigurationOrderingTest builds an ApplicationContextRunner with AutoConfigurations.of(SpringAIAutoConfiguration.class, OpenAiChatAutoConfiguration.class, OpenAiEmbeddingAutoConfiguration.class, ToolCallingAutoConfiguration.class). AutoConfigurations applies the same AutoConfigurationSorter as a real application, so:

  • without the ordering declaration the test fails (the com.google.adk... configuration is processed before org.springframework.ai... and no SpringAI bean exists),
  • with the ordering it passes and both SpringAI and SpringAIEmbedding beans are created.

spring-ai-autoconfigure-model-openai and spring-ai-autoconfigure-model-tool are added as test-scoped dependencies to make the real provider configurations available to the runner.

Module test run: 2/2 passing; the ordering test was also verified to fail when the afterName declaration is reverted (test-first verification).

…el auto-configurations

The @ConditionalOnBean guards on the SpringAI bean methods were evaluated
before the Spring AI provider auto-configurations had run: with no
ordering declared, alphabetical sorting puts this configuration
(com.google.adk...) ahead of org.springframework.ai.model.*.autoconfigure
.*AutoConfiguration in real applications, so every condition misses and
the SpringAI/SpringAIEmbedding beans are silently never created (google#1501).

Declare afterName over the Spring AI 2.x chat and embedding model
auto-configurations (class names verified against each module's
AutoConfiguration.imports at v2.0.1). String-based afterName is required
because this module only compiles against spring-ai-model, and unknown
names are ignored, so versions without a listed configuration are
unaffected.

Add an ordering regression test that drives the real OpenAI chat and
embedding auto-configurations through AutoConfigurations (same sorter as
a real application): it fails while the ordering is missing and passes
with it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant