Skip to content

Conversation

ielatif
Copy link
Contributor

@ielatif ielatif commented Oct 4, 2025

Fixes openai auto-configurations for #4494

Changes

  • Removed unnecessary @ImportAutoConfiguration annotations from OpenAI auto-configuration classes.
  • Add common auto-configurations to context runner of unit and integration tests.

Refactoring suggestion

I did not use a BaseIT class:

  • Common auto-configurations are duplicated for each model.
  • Using BaseIT in unit tests introduce a coupling between unit tests and integration tests

I suggest to isolate and centralise common auto-configurations in one place (may be in spring-ai-test).
Something like this

public class SpringAiTestAutoConfigurations {  // we can use another name that reflects more the usage

   // Common spring ai auto-configurations
   static Class<?>[] commonAutoConfigurations = { SpringAiRetryAutoConfiguration.class, RestClientAutoConfiguration.class,
   		WebClientAutoConfiguration.class, ToolCallingAutoConfiguration.class};

   public static AutoConfigurations of(Class<?>... additionalConfigurations) {
   	Class<?>[] all = Stream.concat(Arrays.stream(commonAutoConfigurations), Arrays.stream(additionalConfigurations))
   			.toArray(Class<?>[]::new);
   	return AutoConfigurations.of(all);
   }

}

Then use it like this

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
		.withPropertyValues("spring.ai.openai.apiKey=" + System.getenv("OPENAI_API_KEY"),
				"spring.ai.openai.chat.options.model=gpt-4o")
		.withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiChatAutoConfiguration.class,
				ChatClientAutoConfiguration.class));

@Kehrlann your thoughts

Signed-off-by: Issam El-atif <[email protected]>
@centrumek
Copy link
Contributor

centrumek commented Oct 4, 2025

I like your idea @ielatif to use centralization for auto-configurations.

Same issue is in my PR #4535 and #4543

With one point I disagree,

 static Class<?>[] commonAutoConfigurations = { SpringAiRetryAutoConfiguration.class, RestClientAutoConfiguration.class,
   		WebClientAutoConfiguration.class, ToolCallingAutoConfiguration.class};

Not in every auto-configuration test we need all classes from common, e.g. in VertexAiTextEmbedding... I needed only SpringAiRetryAutoConfiguration

I think we can start with of method and SpringAiRetryAutoConfiguration inside

   public static AutoConfigurations of(Class<?>... additionalConfigurations) {
   	Class<?>[] all = Stream.concat(Arrays.stream(SpringAiRetryAutoConfiguration.class), Arrays.stream(additionalConfigurations))
   			.toArray(Class<?>[]::new);
   	return AutoConfigurations.of(all);
   }

@ilayaperumalg
Copy link
Member

@ielatif Thanks for the PR! I like your suggestion on having the centralised test configuration. @centrumek Good point as well. Perhaps, we can still centralise the auto-configurations by importing auto-configuration specific to tool calling tests. While this may not the scope of this PR, we can have it addressed separately once the related epic #4494 is closed.

@ielatif
Copy link
Contributor Author

ielatif commented Oct 7, 2025

@centrumek thanks for your feedback.

The reason why i suggest to put all commons configurations in one place is because they are conditionnal auto-configurations, the beans will get loaded only if the condition match.

  • SpringAiRetryAutoConfiguration @ConditionalOnClass(RetryUtils.class)
  • RestClientAutoConfiguration @ConditionalOnClass({RestClientBuilder.class})
  • WebClientAutoConfiguration @ConditionalOnClass({WebClient.class})
  • ToolCallingAutoConfiguration.class @ConditionalOnClass(ChatModel.class)

If VertexAiTextEmbedding use only SpringAiRetry classes like RestClientBuilder, WebClient and ChatModel shouldn't be in the classpath then auto-configurations RestClientAutoConfiguration, WebClientAutoConfiguration and ToolCallingAutoConfiguration will not be loaded in the applicationContext due to missing classes on the classpath

@ilayaperumalg yes sure! I'll be happy to address this seperately once #4494 is closed.

@ielatif
Copy link
Contributor Author

ielatif commented Oct 7, 2025

@ilayaperumalg i see that the build is falling for this PR with :

Error: Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.5.0:check (checkstyle-validation) on project spring-ai-autoconfigure-model-openai: Failed during checkstyle execution: There are 13 errors reported by Checkstyle 9.3 with src/checkstyle/checkstyle.xml ruleset. -> [Help 1]

I run mvn spring-javaformat:apply in the root module to fix checkstyle violations but nothing is getting fixed.
Can you help on it please?

@ilayaperumalg
Copy link
Member

@ielatif The check style errors need to be fixed manually. I will take care of it when merging the PR. Thanks again.

@ilayaperumalg
Copy link
Member

Rebased and merged as 0ae56f7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants