Skip to main content
Polyglot creates an HTTP client for you by default using the HttpClientBuilder. In most cases this is all you need. However, if your application already owns the HTTP transport concern — for example, you need custom timeouts, middleware, or a shared client instance — you can build your own HTTP client and inject it into the runtime.

Injecting an HTTP Client

The InferenceRuntime::fromConfig() method accepts an optional httpClient parameter. Build an HTTP client with HttpClientBuilder, configure it to your needs, and pass it in:
When no HTTP client is provided, Polyglot creates a default one with sensible timeouts. The custom client you inject will be used for all requests made through that runtime.

HTTP Client Configuration Options

The HttpClientConfig class accepts these parameters:

Choosing an HTTP Driver

Polyglot supports multiple HTTP drivers. The default curl driver works without additional dependencies. If your project already uses Guzzle or Symfony HttpClient, you can reuse them:
You can also inject a pre-configured client instance from your application’s service container:
This is particularly useful when your application requires proxy configuration, custom SSL certificates, or other transport-level settings.

Adding Middleware

The HttpClientBuilder supports a middleware stack for cross-cutting concerns like retries, circuit breaking, and request logging. Middleware is applied in the order it is added.

Retry Policy

Automatically retry failed requests with exponential backoff:

Circuit Breaker

Protect your application from cascading failures by stopping requests to a failing provider:

Combining Multiple Middleware

Stack retry and circuit breaker policies together for robust error handling:

Custom Middleware

You can also add your own middleware for logging, metrics, or request transformation:

Using with Embeddings

The same pattern works for the embeddings runtime. Pass your custom HTTP client when building an EmbeddingsRuntime:

Sharing an HTTP Client Across Runtimes

If your application uses both inference and embeddings, you can share a single HTTP client between them to reuse connection pools and middleware configuration:
This ensures both services share the same retry policy, circuit breaker state, and connection pool configuration.