Skip to content

Add RestClient timeout support via spring-ai-autoconfigure-http-client module #2808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

drow724
Copy link

@drow724 drow724 commented Apr 19, 2025

Hi team,

This PR adds support for configuring RestClient timeouts via the new spring.ai.http.client.connection-timeout and spring.ai.http.client.read-timeout properties.

I understand that some of the previous timeout-related contributions were declined with the reasoning that RestClientCustomizer is the recommended way to control low-level HTTP behavior. However, I’d like to offer a slightly different perspective based on real-world usage and Spring Boot developer experience.

Spring AI clients such as OpenAiChatModel internally construct RestClient using RestClient.builder(), which makes it difficult for users to apply timeout settings without defining custom beans. For many users who just want to quickly call ChatGPT or OpenAI endpoints, defining a RestClientCustomizer bean solely to apply timeout settings is unexpected ceremony.

This PR follows the Spring Boot philosophy of externalized configuration with reasonable defaults, enabling users to declare timeout values in application.yml without writing additional Java code.

A new auto-configuration module (spring-ai-autoconfigure-http-client) is introduced to configure RestClientBuilder via RestClientCustomizer. The customizer is registered only if no other RestClientCustomizer is defined, using @ConditionalOnMissingBean, so it’s fully overridable. The timeout values are backed by a simple @ConfigurationProperties class (SpringAiHttpClientProperties) to keep things clean and idiomatic.

While WebClient timeout support would be useful as well, we chose not to include it yet because the most elegant and standardized way to configure timeouts (via ClientHttpConnectorSettings) is only available in Spring Boot 3.5+. Once Spring AI upgrades its baseline, we plan to extend this same configuration model to support WebClient as well.

This PR is designed to:

  • Keep configuration consistent with Spring Boot patterns
  • Reduce friction for users who use RestClient implicitly via Spring AI models
  • Respect existing extension points (RestClientCustomizer) without overriding user-defined beans

Looking forward to your feedback.

@drow724 drow724 force-pushed the feature/openai-timeout-support branch from 0dd110e to f828a35 Compare April 19, 2025 08:14
@markpollack
Copy link
Member

Super thanks. I've been prototyping some things in this direction. Will take a closer look.

@markpollack markpollack self-assigned this May 6, 2025
@drow724 drow724 force-pushed the feature/openai-timeout-support branch from 206238b to 50bab6f Compare May 7, 2025 00:48
@drow724
Copy link
Author

drow724 commented May 7, 2025

@markpollack Thanks for the feedback!

I’ve added the new spring-ai-autoconfigure-http-client module to the relevant starter pom.xml files where RestClient.Builder is actually used. This ensures that the timeout auto-configuration is available only where it's needed.

Please take a look when you have a moment

@markpollack
Copy link
Member

Thanks, trying to continue prototyping, you work is super useful, will get back to you asap.

@drow724 drow724 force-pushed the feature/openai-timeout-support branch from 35f36e9 to b97f94d Compare May 9, 2025 04:57
@markpollack markpollack added this to the 1.0.x milestone May 16, 2025
@markpollack
Copy link
Member

We are going to be circling back to this now that GA is over! plesae be patient.

@drow724 drow724 force-pushed the feature/openai-timeout-support branch from d0fb065 to c58b1c7 Compare May 21, 2025 07:29
@RyanHowell30
Copy link

RyanHowell30 commented May 21, 2025

Hey @markpollack @drow724 .

Would this PR solve connection timeout issues?

Currently using GRPC as the transport. I have a chat client which kicks off, based on the output of this response, I send this to another chat client.

Chat client 1 uses Gemini 2.0 flash. This executes well.

Chat client 2 uses Gemini 2.5 pro. Once I enter this chat client, I get a io.netty.handler.timeout.ReadTimeoutException

If there is a workaround for this, please let me know. The quality in responses when using Gemini 2.5 pro for my agent is night and day compared to both chat clients using 2.0 flash.

@drow724 drow724 force-pushed the feature/openai-timeout-support branch 2 times, most recently from ed022b7 to a812986 Compare May 22, 2025 01:32
@drow724
Copy link
Author

drow724 commented May 22, 2025

Hey @markpollack @drow724 .

Would this PR solve connection timeout issues?

Currently using GRPC as the transport. I have a chat client which kicks off, based on the output of this response, I send this to another chat client.

Chat client 1 uses Gemini 2.0 flash. This executes well.

Chat client 2 uses Gemini 2.5 pro. Once I enter this chat client, I get a io.netty.handler.timeout.ReadTimeoutException

If there is a workaround for this, please let me know. The quality in responses when using Gemini 2.5 pro for my agent is night and day compared to both chat clients using 2.0 flash.

Hey @RyanHowell30

This PR addresses connection timeout configuration only for HTTP clients that use Spring's RestClient (e.g., OpenAI, Mistral, Ollama, etc.).

Since Vertex AI uses gRPC-based transport under the hood via PredictionServiceClient (which in turn uses Netty), the timeout issue you're experiencing (io.netty.handler.timeout.ReadTimeoutException) would need to be addressed through gRPC channel settings, not through this PR.

Hope this helps clarify!

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

Successfully merging this pull request may close these issues.

3 participants