Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class OpenAiChatAutoConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.ai.openai", name = { "api-key", "chat.api-key" },
matchIfMissing = false)
public OpenAiApi openAiApi(OpenAiConnectionProperties commonProperties, OpenAiChatProperties chatProperties,
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
ObjectProvider<WebClient.Builder> webClientBuilderProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,24 @@ void openAiApiBean() {
});
}

@Test
void imageOnlyApiKeyDoesNotFailChatAutoConfiguration() {
// Regression test for https://github.com/spring-projects/spring-ai/issues/1818
// When only spring.ai.openai.image.api-key is set (no chat or common api-key),
// OpenAiChatAutoConfiguration must not throw an IllegalArgumentException.
// The OpenAiApi bean for chat should simply not be created.
new ApplicationContextRunner()
.withPropertyValues("spring.ai.openai.image.api-key=IMAGE_API_KEY",
"spring.ai.openai.base-url=TEST_BASE_URL")
.withConfiguration(AutoConfigurations.of(OpenAiChatAutoConfiguration.class,
OpenAiImageAutoConfiguration.class, RestClientAutoConfiguration.class,
SpringAiRetryAutoConfiguration.class, ToolCallingAutoConfiguration.class,
WebClientAutoConfiguration.class))
.run(context -> {
assertThat(context).hasNotFailed();
assertThat(context.getBeansOfType(OpenAiChatModel.class)).isEmpty();
assertThat(context.getBeansOfType(OpenAiImageModel.class)).isNotEmpty();
});
}

}