spring-projects/spring-ai

Ensure that there is a RestClient.Builder in the application context

Closed this issue · 9 comments

When Spring AI auto-configures various client implementations, it depends upon a RestClient.Builder to create a RestClient for those client implementations. That works fine when the project uses the Spring MVC starter because a RestClient.Builder will be auto-configured. But when using the Spring WebFlux starter, no such RestClient.Builder is auto-configured and the application will fail to start.

AFAIK, this happens in all recent versions of Spring AI, including 0.8.1 and 1.0.0-SNAPSHOT.

To reproduce, create a simple Spring AI application with a dependency on the WebFlux starter and almost any model starter...such as the OpenAI starter. You do not need to write any code to reproduce this problem. Just create a new Spring Boot project with the aforementioned starters and then try to run it. The application will fail to start and you'll get the following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 2 of method openAiChatClient in org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration required a bean of type 'org.springframework.web.client.RestClient$Builder' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.web.client.RestClient$Builder' in your configuration.

If Spring AI were to ensure the existence of a RestClient.Builder then the application would start and function fine, even when using WebFlux.

can i help

@habuma implementation suggestions?

In case someone is looking for a temporary work around, adding org.springframework.boot:spring-boot-starter-web to the dependencies will resolve the issue.

It is not acceptable to add starters to utility/library. It is ok to add it to the end application, which is the current behaviour.
The goal is to have some defaults (non starter) in the library.

Should not spring-ai use HTTP Interface Client, and detect (and use) either WebClient or RestClient builders?

If you have configured "web-application-type", you can try to remove it and restart
from

server:
  port: 8091
spring:
  application:
    name: Spring-ai-demo
  main:
    web-application-type: reactive

to

server:
  port: 8091
spring:
  application:
    name: Spring-ai-demo

Related to #524

image
RestClient#Builder bean is only defined in web application, you need to define one yourself. I tried it in webflux and it worked fine

Duplicate of #524