Skip to content

fix: skip OpenAiApi chat bean when no chat API key is configured#5763

Open
anuragg-saxenaa wants to merge 1 commit intospring-projects:mainfrom
anuragg-saxenaa:fix/issue-1818-openai-image-api-key-autoconfigure
Open

fix: skip OpenAiApi chat bean when no chat API key is configured#5763
anuragg-saxenaa wants to merge 1 commit intospring-projects:mainfrom
anuragg-saxenaa:fix/issue-1818-openai-image-api-key-autoconfigure

Conversation

@anuragg-saxenaa
Copy link
Copy Markdown

Problem

Fixes #1818

When a user configures only spring.ai.openai.image.api-key (without setting spring.ai.openai.api-key or spring.ai.openai.chat.api-key), the application fails to start with:

java.lang.IllegalArgumentException: OpenAI API key must be set

This happens because OpenAiChatAutoConfiguration is annotated with matchIfMissing = true at class level (so it always loads), but the openAiApi bean inside it calls resolveConnectionProperties(commonProperties, chatProperties, "chat") which throws when neither a common nor a chat-specific API key is present.

Fix

Add @ConditionalOnProperty on the openAiApi bean inside OpenAiChatAutoConfiguration:

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.ai.openai", name = { "api-key", "chat.api-key" },
        matchIfMissing = false)
public OpenAiApi openAiApi(...) {

This ensures the chat OpenAiApi bean 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 imageOnlyApiKeyDoesNotFailChatAutoConfiguration in OpenAiModelConfigurationTests:

  • Loads context with spring.ai.openai.image.api-key=IMAGE_API_KEY only
  • Includes both OpenAiChatAutoConfiguration and OpenAiImageAutoConfiguration
  • Asserts context starts without errors (hasNotFailed())
  • Asserts OpenAiImageModel is present and OpenAiChatModel is absent

Signed-off-by: anuragg-saxenaa anuragg.saxenaa@gmail.com

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAiAutoConfiguration does not recognize the API Key configured via property key: 'spring.ai.openai.image.api-key'

1 participant