Skip to main content
Polyglot ships with drivers for over 25 LLM providers and several embeddings providers. When you need to integrate a provider that is not bundled — or override the behavior of an existing one — the library exposes clean extension points for both inference and embeddings.

Custom Inference Drivers

Inference drivers implement the CanProcessInferenceRequest interface, which defines three methods:

Registering a Driver Class

The simplest approach is to provide a class string. Polyglot will instantiate it with the standard constructor signature ($config, $httpClient, $events):

Registering a Driver Spec

If your provider speaks the OpenAI wire protocol, you do not need a driver class. Register an InferenceDriverSpec naming the parts that differ; everything you leave out defaults to the OpenAI implementation:
The spec’s other fields — requestAdapter, responseAdapter, usageFormat, messageFormat — take the same treatment. All bundled providers use this same declarative shape. Providers whose wire protocol or endpoint differs name those provider-specific collaborators in the row; they do not need a provider driver class. To change behaviour rather than composition, subclass SpecifiedInferenceDriver and name it in the spec. The spec still assembles the five collaborators for it:

Registering a Driver Factory

withDriver() also accepts a class-string or any callable receiving LLMConfig, CanSendHttpRequests and CanHandleEvents and returning a CanProcessInferenceRequest. Use this when construction needs logic a spec cannot express — reading an environment variable, choosing between implementations, wiring a decorator:

Using the Registry with InferenceRuntime

You can pass the driver registry directly when building a runtime:
Or use the drivers parameter on Inference::fromConfig() or Inference::using():

Implementing a Full Driver

When building a driver from scratch, you will typically need to implement several adapter components:
  1. Request Adapter — transforms InferenceRequest into the provider’s HTTP request format
  2. Body Format — structures the request body according to the provider’s API schema
  3. Message Format — converts Polyglot’s message format to the provider’s format
  4. Response Adapter — parses the provider’s HTTP response into InferenceResponse
  5. Usage Format — extracts token usage information from the response
For the request adapter, extend BaseHttpRequestAdapter rather than implementing CanTranslateInferenceRequest directly. It owns the request-building skeleton and leaves you the only two methods that vary between providers:
All bundled providers follow this modular adapter pattern and are declared as an InferenceDriverSpec. OpenAI-compatible providers use the default adapters where possible; native protocols and providers with custom URLs or headers select bespoke request or response adapters in their spec row. See BundledInferenceDrivers::registry() for the complete table.

Custom Embeddings Drivers

Embeddings drivers implement the CanHandleVectorization interface:
The driver owns its complete provider boundary: translating the request, sending it, decoding the provider payload, and adapting that payload into an EmbeddingsResponse. Callers never receive the intermediate HTTP response.

Migrating a v2.6 Embeddings Driver

This signature changes in v2.7 and cannot be shimmed by PHP. Update custom implementations in the same deployment that upgrades Polyglot:
Move the HTTP response decoding and response-adapter call into handle(). Drivers extending BaseEmbedDriver inherit the v2.7 implementation unless they override handle() themselves. Register a custom embeddings driver using the BundledEmbeddingsDrivers registry, the same pattern used for inference drivers:
Like inference drivers, you can also pass a callable factory instead of a class string:
Note: The EmbeddingsDriverRegistry is immutable — each mutation returns a new instance, matching the same pattern as InferenceDriverRegistry.

Removing or Replacing Bundled Drivers

The InferenceDriverRegistry is immutable — each mutation returns a new instance. You can remove a bundled driver or replace it entirely:

Bundled Drivers

For reference, Polyglot bundles the following inference drivers: The full list is defined in BundledInferenceDrivers::registry(). Bundled embeddings drivers include: openai, azure, cohere, gemini, jina, mistral, and ollama.

Listening to Events

Both InferenceRuntime and EmbeddingsRuntime dispatch events at key lifecycle points. You can listen for specific events or wiretap all of them: