fix: skip OpenAiApi chat bean when no chat API key is configured#5763
Open
anuragg-saxenaa wants to merge 1 commit intospring-projects:mainfrom
Open
Conversation
…ing-projects#1818) Add @ConditionalOnProperty to the openAiApi bean inside OpenAiChatAutoConfiguration so it is only created when either spring.ai.openai.api-key or spring.ai.openai.chat.api-key is present. Previously, configuring only spring.ai.openai.image.api-key caused an IllegalArgumentException at startup because the chat auto-configuration always tried to resolve a chat API key. Adds a regression test that loads both chat and image auto-configurations with only the image API key set, and verifies the context starts successfully with the image model present and no chat model created. Signed-off-by: anuragg-saxenaa <anuragg.saxenaa@gmail.com>
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.
Problem
Fixes #1818
When a user configures only
spring.ai.openai.image.api-key(without settingspring.ai.openai.api-keyorspring.ai.openai.chat.api-key), the application fails to start with:This happens because
OpenAiChatAutoConfigurationis annotated withmatchIfMissing = trueat class level (so it always loads), but theopenAiApibean inside it callsresolveConnectionProperties(commonProperties, chatProperties, "chat")which throws when neither a common nor a chat-specific API key is present.Fix
Add
@ConditionalOnPropertyon theopenAiApibean insideOpenAiChatAutoConfiguration:This ensures the chat
OpenAiApibean is only created when the user has set at least one of the two relevant API key properties. Users who only need image (or embedding, or audio) but not chat will no longer get a startup failure.Test
Added
imageOnlyApiKeyDoesNotFailChatAutoConfigurationinOpenAiModelConfigurationTests:spring.ai.openai.image.api-key=IMAGE_API_KEYonlyOpenAiChatAutoConfigurationandOpenAiImageAutoConfigurationhasNotFailed())OpenAiImageModelis present andOpenAiChatModelis absentSigned-off-by: anuragg-saxenaa anuragg.saxenaa@gmail.com