Skip to content

Commit 76f97cf

Browse files
committed
GH-868: Fix inconsistency in autoconfigured beans
* Fixing auto configured beans where they are missing `@ConditionalOnMissingBean` * Add `matchIfMissing` on `@ConditionalOnProperty` where it is missing with value `true` Resolves #868
1 parent 6270d62 commit 76f97cf

File tree

8 files changed

+38
-13
lines changed

8 files changed

+38
-13
lines changed

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/azure/openai/AzureOpenAiAutoConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 - 2024 the original author or authors.
2+
* Copyright 2023-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@
4343

4444
/**
4545
* @author Piotr Olaszewski
46+
* @author Soby Chacko
4647
*/
4748
@AutoConfiguration
4849
@ConditionalOnClass({ OpenAIClientBuilder.class, AzureOpenAiChatModel.class })
@@ -95,6 +96,7 @@ public OpenAIClient openAIClientWithTokenCredential(AzureOpenAiConnectionPropert
9596
}
9697

9798
@Bean
99+
@ConditionalOnMissingBean
98100
@ConditionalOnProperty(prefix = AzureOpenAiChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
99101
matchIfMissing = true)
100102
public AzureOpenAiChatModel azureOpenAiChatModel(OpenAIClient openAIClient,
@@ -109,6 +111,7 @@ public AzureOpenAiChatModel azureOpenAiChatModel(OpenAIClient openAIClient,
109111
}
110112

111113
@Bean
114+
@ConditionalOnMissingBean
112115
@ConditionalOnProperty(prefix = AzureOpenAiEmbeddingProperties.CONFIG_PREFIX, name = "enabled",
113116
havingValue = "true", matchIfMissing = true)
114117
public AzureOpenAiEmbeddingModel azureOpenAiEmbeddingModel(OpenAIClient openAIClient,
@@ -126,6 +129,7 @@ public FunctionCallbackContext springAiFunctionManager(ApplicationContext contex
126129
}
127130

128131
@Bean
132+
@ConditionalOnMissingBean
129133
@ConditionalOnProperty(prefix = AzureOpenAiImageOptionsProperties.CONFIG_PREFIX, name = "enabled",
130134
havingValue = "true", matchIfMissing = true)
131135
public AzureOpenAiImageModel azureOpenAiImageClient(OpenAIClient openAIClient,
@@ -135,6 +139,7 @@ public AzureOpenAiImageModel azureOpenAiImageClient(OpenAIClient openAIClient,
135139
}
136140

137141
@Bean
142+
@ConditionalOnMissingBean
138143
@ConditionalOnProperty(prefix = AzureOpenAiAudioTranscriptionProperties.CONFIG_PREFIX, name = "enabled",
139144
havingValue = "true", matchIfMissing = true)
140145
public AzureOpenAiAudioTranscriptionModel azureOpenAiAudioTranscriptionModel(OpenAIClient openAIClient,

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/bedrock/anthropic/BedrockAnthropicChatAutoConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 - 2024 the original author or authors.
2+
* Copyright 2023-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,12 +38,14 @@
3838
*
3939
* @author Christian Tzolov
4040
* @author Wei Jiang
41+
* @author Soby Chacko
4142
* @since 0.8.0
4243
*/
4344
@AutoConfiguration
4445
@ConditionalOnClass(AnthropicChatBedrockApi.class)
4546
@EnableConfigurationProperties({ BedrockAnthropicChatProperties.class, BedrockAwsConnectionProperties.class })
46-
@ConditionalOnProperty(prefix = BedrockAnthropicChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true")
47+
@ConditionalOnProperty(prefix = BedrockAnthropicChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
48+
matchIfMissing = true)
4749
@Import(BedrockAwsConnectionConfiguration.class)
4850
public class BedrockAnthropicChatAutoConfiguration {
4951

@@ -58,6 +60,7 @@ public AnthropicChatBedrockApi anthropicApi(AwsCredentialsProvider credentialsPr
5860
}
5961

6062
@Bean
63+
@ConditionalOnMissingBean
6164
@ConditionalOnBean(AnthropicChatBedrockApi.class)
6265
public BedrockAnthropicChatModel anthropicChatModel(AnthropicChatBedrockApi anthropicApi,
6366
BedrockAnthropicChatProperties properties) {

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/bedrock/anthropic3/BedrockAnthropic3ChatAutoConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 - 2024 the original author or authors.
2+
* Copyright 2023-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,12 +38,14 @@
3838
*
3939
* @author Christian Tzolov
4040
* @author Wei Jiang
41+
* @author Soby Chacko
4142
* @since 0.8.0
4243
*/
4344
@AutoConfiguration
4445
@ConditionalOnClass(Anthropic3ChatBedrockApi.class)
4546
@EnableConfigurationProperties({ BedrockAnthropic3ChatProperties.class, BedrockAwsConnectionProperties.class })
46-
@ConditionalOnProperty(prefix = BedrockAnthropic3ChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true")
47+
@ConditionalOnProperty(prefix = BedrockAnthropic3ChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
48+
matchIfMissing = true)
4749
@Import(BedrockAwsConnectionConfiguration.class)
4850
public class BedrockAnthropic3ChatAutoConfiguration {
4951

@@ -58,6 +60,7 @@ public Anthropic3ChatBedrockApi anthropic3Api(AwsCredentialsProvider credentials
5860
}
5961

6062
@Bean
63+
@ConditionalOnMissingBean
6164
@ConditionalOnBean(Anthropic3ChatBedrockApi.class)
6265
public BedrockAnthropic3ChatModel anthropic3ChatModel(Anthropic3ChatBedrockApi anthropicApi,
6366
BedrockAnthropic3ChatProperties properties) {

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/bedrock/cohere/BedrockCohereChatAutoConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 - 2024 the original author or authors.
2+
* Copyright 2023-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,12 +36,14 @@
3636
*
3737
* @author Christian Tzolov
3838
* @author Wei Jiang
39+
* @author Soby Chacko
3940
* @since 0.8.0
4041
*/
4142
@AutoConfiguration
4243
@ConditionalOnClass(CohereChatBedrockApi.class)
4344
@EnableConfigurationProperties({ BedrockCohereChatProperties.class, BedrockAwsConnectionProperties.class })
44-
@ConditionalOnProperty(prefix = BedrockCohereChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true")
45+
@ConditionalOnProperty(prefix = BedrockCohereChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
46+
matchIfMissing = true)
4547
@Import(BedrockAwsConnectionConfiguration.class)
4648
public class BedrockCohereChatAutoConfiguration {
4749

@@ -56,6 +58,7 @@ public CohereChatBedrockApi cohereChatApi(AwsCredentialsProvider credentialsProv
5658
}
5759

5860
@Bean
61+
@ConditionalOnMissingBean
5962
@ConditionalOnBean(CohereChatBedrockApi.class)
6063
public BedrockCohereChatModel cohereChatModel(CohereChatBedrockApi cohereChatApi,
6164
BedrockCohereChatProperties properties) {

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/bedrock/cohere/BedrockCohereEmbeddingAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 - 2024 the original author or authors.
2+
* Copyright 2023-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,12 +37,14 @@
3737
*
3838
* @author Christian Tzolov
3939
* @author Wei Jiang
40+
* @author Soby Chacko
4041
* @since 0.8.0
4142
*/
4243
@AutoConfiguration
4344
@ConditionalOnClass(CohereEmbeddingBedrockApi.class)
4445
@EnableConfigurationProperties({ BedrockCohereEmbeddingProperties.class, BedrockAwsConnectionProperties.class })
45-
@ConditionalOnProperty(prefix = BedrockCohereEmbeddingProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true")
46+
@ConditionalOnProperty(prefix = BedrockCohereEmbeddingProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
47+
matchIfMissing = true)
4648
@Import(BedrockAwsConnectionConfiguration.class)
4749
public class BedrockCohereEmbeddingAutoConfiguration {
4850

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/bedrock/jurrasic2/BedrockAi21Jurassic2ChatAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 - 2024 the original author or authors.
2+
* Copyright 2023-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,13 +37,14 @@
3737
*
3838
* @author Ahmed Yousri
3939
* @author Wei Jiang
40+
* @author Soby Chacko
4041
* @since 1.0.0
4142
*/
4243
@AutoConfiguration
4344
@ConditionalOnClass(Ai21Jurassic2ChatBedrockApi.class)
4445
@EnableConfigurationProperties({ BedrockAi21Jurassic2ChatProperties.class, BedrockAwsConnectionProperties.class })
4546
@ConditionalOnProperty(prefix = BedrockAi21Jurassic2ChatProperties.CONFIG_PREFIX, name = "enabled",
46-
havingValue = "true")
47+
havingValue = "true", matchIfMissing = true)
4748
@Import(BedrockAwsConnectionConfiguration.class)
4849
public class BedrockAi21Jurassic2ChatAutoConfiguration {
4950

@@ -58,6 +59,7 @@ public Ai21Jurassic2ChatBedrockApi ai21Jurassic2ChatBedrockApi(AwsCredentialsPro
5859
}
5960

6061
@Bean
62+
@ConditionalOnMissingBean
6163
@ConditionalOnBean(Ai21Jurassic2ChatBedrockApi.class)
6264
public BedrockAi21Jurassic2ChatModel jurassic2ChatModel(Ai21Jurassic2ChatBedrockApi ai21Jurassic2ChatBedrockApi,
6365
BedrockAi21Jurassic2ChatProperties properties) {

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/bedrock/llama/BedrockLlamaChatAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
@AutoConfiguration
4545
@ConditionalOnClass(LlamaChatBedrockApi.class)
4646
@EnableConfigurationProperties({ BedrockLlamaChatProperties.class, BedrockAwsConnectionProperties.class })
47-
@ConditionalOnProperty(prefix = BedrockLlamaChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true")
47+
@ConditionalOnProperty(prefix = BedrockLlamaChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
48+
matchIfMissing = true)
4849
@Import(BedrockAwsConnectionConfiguration.class)
4950
public class BedrockLlamaChatAutoConfiguration {
5051

@@ -58,6 +59,7 @@ public LlamaChatBedrockApi llamaApi(AwsCredentialsProvider credentialsProvider,
5859
}
5960

6061
@Bean
62+
@ConditionalOnMissingBean
6163
@ConditionalOnBean(LlamaChatBedrockApi.class)
6264
public BedrockLlamaChatModel llamaChatModel(LlamaChatBedrockApi llamaApi, BedrockLlamaChatProperties properties) {
6365

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vertexai/gemini/VertexAiGeminiAutoConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 - 2024 the original author or authors.
2+
* Copyright 2023-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,12 +21,14 @@
2121
import com.google.auth.oauth2.GoogleCredentials;
2222
import com.google.cloud.vertexai.VertexAI;
2323

24+
import org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiChatProperties;
2425
import org.springframework.ai.model.function.FunctionCallback;
2526
import org.springframework.ai.model.function.FunctionCallbackContext;
2627
import org.springframework.ai.model.function.FunctionCallbackWrapper.Builder.SchemaType;
2728
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatModel;
2829
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2930
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3032
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3133
import org.springframework.context.ApplicationContext;
3234
import org.springframework.context.annotation.Bean;
@@ -38,6 +40,7 @@
3840
* Auto-configuration for Vertex AI Gemini Chat.
3941
*
4042
* @author Christian Tzolov
43+
* @author Soby Chacko
4144
* @since 0.8.0
4245
*/
4346
@ConditionalOnClass({ VertexAI.class, VertexAiGeminiChatModel.class })
@@ -74,6 +77,8 @@ public VertexAI vertexAi(VertexAiGeminiConnectionProperties connectionProperties
7477

7578
@Bean
7679
@ConditionalOnMissingBean
80+
@ConditionalOnProperty(prefix = VertexAiGeminiChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
81+
matchIfMissing = true)
7782
public VertexAiGeminiChatModel vertexAiGeminiChat(VertexAI vertexAi, VertexAiGeminiChatProperties chatProperties,
7883
List<FunctionCallback> toolFunctionCallbacks, ApplicationContext context) {
7984

0 commit comments

Comments
 (0)