HTTP client abstraction for LangChain4j with synchronous/asynchronous execution and Server-Sent Events (SSE) streaming support
See HttpClientBuilder Interface in Core Interfaces for the complete API.
public interface HttpClientBuilder {
Duration connectTimeout();
HttpClientBuilder connectTimeout(Duration timeout);
Duration readTimeout();
HttpClientBuilder readTimeout(Duration timeout);
HttpClient build();
}The connect timeout determines the maximum time to wait when establishing a connection to the server. This includes:
When connect timeout is exceeded:
RuntimeException is thrown from execute() methodsSocketTimeoutException or similarRecommended values:
The read timeout determines the maximum time to wait between receiving data packets from the server. It does NOT represent the total time for the request.
Important: For each chunk of data received, the timeout is reset.
When read timeout is exceeded:
RuntimeException is thrown from execute() methodsServerSentEventListener.onError() is called with the timeout exceptionRecommended values:
The HttpClientBuilder is obtained via the Service Provider Interface pattern. Implementations must:
HttpClientBuilderFactory interfaceMETA-INF/services/dev.langchain4j.http.client.HttpClientBuilderFactoryWhen multiple HTTP client implementations are available in the classpath:
langchain4j.http.clientBuilderFactory to specify which one to useIllegalStateException is thrown# Example: Specify which HTTP client implementation to use
java -Dlangchain4j.http.clientBuilderFactory=com.example.MyHttpClientFactory ...HttpClientBuilder instances can be used to create multiple clients:
HttpClientBuilder builder = HttpClientBuilderLoader.loadHttpClientBuilder();
// Create client with short timeout
HttpClient fastClient = builder
.connectTimeout(Duration.ofSeconds(5))
.readTimeout(Duration.ofSeconds(10))
.build();
// Create another client with long timeout
HttpClient slowClient = builder
.connectTimeout(Duration.ofSeconds(30))
.readTimeout(Duration.ofMinutes(2))
.build();Note: The behavior of builder reuse depends on the specific implementation. Some implementations may share state between builds, while others create independent configurations.
Install with Tessl CLI
npx tessl i tessl/maven-dev-langchain4j--langchain4j-http-client