Fix OpenAI auto-config when only sub-prefix api-key is set
Summary
Closes #1818, closes #5988, closes #5989.
Why
Each of the six sub-model beans (chat / embedding / image / audio.speech / audio.transcription / moderation) is registered as soon as its spring.ai.model.<kind> selector picks openai, and registration runs the common spring.ai.openai.api-key check unconditionally. Sub-prefix-only credentials (and other valid sources — programmatic Credential, OPENAI_API_KEY, passwordless Foundry) are never consulted at the gate, so context startup aborts even when a usable credential exists.
Fix
Introduce OpenAiConnectionCondition with one nested class per sub-config and annotate each sub-model @Bean with @Conditional(OpenAiConnectionCondition.X.class). Each getMatchOutcome returns a match if any of the relevant credential sources is present: common api-key, sub-prefix api-key, programmatic Credential on common/sub *Properties, OPENAI_API_KEY env var, GitHub Models token, or Microsoft Foundry passwordless auth. Conditions sit on @Bean methods so @EnableConfigurationProperties and the existing @ConditionalOnProperty(spring.ai.model.<kind>) selector keep working unchanged.
Tests
Added regression coverage in OpenAiConnectionConditionAutoConfigurationTests for:
- common api-key enables all six sub-models
- each sub-prefix key enables only its own bean (the #1818 image case plus the five symmetric variants)
- with no api-key set, the context boots with zero model beans
spring.ai.model.chat=noneselector suppresses chat even when an api-key is set- user-defined
OpenAiChatModelbean is preserved when no api-key is set OPENAI_API_KEYenv var fallback enables all sixmicrosoft-foundry=trueenables chat without an api-key
./mvnw -pl auto-configurations/models/spring-ai-autoconfigure-model-openai test \
-Dtest='OpenAiConnectionConditionAutoConfigurationTests'
Scope
This PR covers the six OpenAI sub-model auto-configs. Other provider auto-configs with the same shape are outside this PR.
@sdeleuze Before further review, could I get a quick design call?
The PR adds OpenAiConnectionCondition as a 220-line file with six nested condition classes (one per sub-model bean). A lighter alternative — call it D — collapses these into a single top-level OnAvailableOpenAiConnection condition plus a credential-check helper on OpenAiAutoConfigurationUtil (or AbstractOpenAiProperties), with the sub-prefix resolved from the bean method's properties parameter type. ~80 lines, no per-sub-bean wrapper.
Trade-off: under D, @Conditional(OnAvailableOpenAiConnection.class) no longer names the sub-prefix at the call site, so the wiring is slightly less self-documenting than the current @Conditional(...Chat.class) shape.
Either fixes #5989. I'd rather not pick unilaterally since this introduces a new gating primitive that stays in the codebase. If D (or a third shape I missed) fits the codebase taste better, I'll push a D-shaped commit on top of this branch for direct comparison.
Pushed a Plan D variant on top of the original shape so the two can be compared side-by-side (ea8750f).
The variant collapses the six nested condition subclasses into a single OnAvailableOpenAiConnection and resolves the sub-prefix from the @Bean method's AbstractOpenAiProperties parameter type. Adding a new sub-bean now requires only an @Conditional(OnAvailableOpenAiConnection.class) annotation on the bean method, no wrapper class.
The trade-off is the same one noted above: the sub-prefix is inferred from the bean method signature instead of being named explicitly at the @Conditional call site.
Either shape works for me; let me know which one fits the codebase taste better.