spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

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

Open habuma opened this issue 10 months ago • 8 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.

habuma avatar Apr 08 '24 12:04 habuma

can i help

Johnnymic avatar Apr 15 '24 11:04 Johnnymic

@habuma implementation suggestions?

markpollack avatar Apr 17 '24 02:04 markpollack

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.

ChrisToplikar avatar Apr 17 '24 18:04 ChrisToplikar

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.

tzolov avatar Apr 30 '24 12:04 tzolov

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

iromu avatar Jun 28 '24 20:06 iromu

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

yueyangbo123 avatar Jun 29 '24 02:06 yueyangbo123

Related to #524

markpollack avatar Jul 22 '24 20:07 markpollack

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

yuge1805 avatar Jul 25 '24 07:07 yuge1805