Skip to main content
Polyglot does not implement its own HTTP transport. It builds on the shared HTTP package, keeping transport concerns separate from request and response normalization. This separation means you can swap HTTP implementations, add middleware, or inject test doubles without touching the driver layer.

The Transport Contract

All HTTP communication flows through a single contract:
Inference, embeddings, and Decision drivers receive a CanSendHttpRequests implementation. Each driver translates its domain request into an HttpRequest, resolves the PendingHttpResponse, and normalizes the result into its operation-specific response.

Default Client

When a runtime is created from config without an HTTP client, Polyglot creates a default one using HttpClientBuilder. This applies to InferenceRuntime, EmbeddingsRuntime, and DecisionRuntime:
The builder selects an appropriate underlying HTTP library (Guzzle, Symfony HttpClient, or Laravel’s HTTP client) based on what is available in your project. You can inject your own client when you need specific configuration:
Pass the same client through DecisionRuntime::fromConfig(..., httpClient: $httpClient) when transport customization or mock HTTP behavior is required. Decision owns its retry loop, so avoid wrapping Decision calls in a second HTTP retry middleware.

HttpRequest and HttpResponse

These data objects represent the HTTP layer’s request and response. Drivers create HttpRequest objects through their request adapters and read HttpResponse objects through their response adapters.

HttpRequest

HttpResponse

The HttpResponse interface provides access to the response data:
For streaming, the stream() method returns a Generator that yields chunks as they arrive from the provider. The driver’s response adapter parses these chunks into SSE events and then into PartialInferenceDelta objects.

Middleware

The HTTP client supports a middleware stack for cross-cutting concerns like logging, retries, caching, and authentication. Middleware implements the HttpMiddleware interface:
The BaseMiddleware abstract class provides convenient hooks so you do not need to manage the chain manually:

Managing the Middleware Stack

The MiddlewareStack supports named middleware for easy manipulation:
Middleware runs in stack order: middleware added with prepend() runs before middleware added with append(). The name parameter is optional but recommended — it allows you to replace or remove middleware later without tracking references.

Stream Cache Manager

For advanced use cases, Polyglot supports stream caching through the CanManageStreamCache contract. When provided, the stream cache manager can record and replay streaming responses, which is useful for testing and development:
The cache behavior is controlled per-request through the ResponseCachePolicy enum on the InferenceRequest. You can set this through the facade:

Shared Event Dispatcher

The HTTP client shares the same event dispatcher as the runtime that created it. This means HTTP-level events (connection errors, timeouts, etc.) flow through the same event system as inference events, providing a unified observability pipeline.