Custom Inference Drivers
Inference drivers implement theCanProcessInferenceRequest 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 anInferenceDriverSpec naming the parts that differ; everything you leave out defaults to the
OpenAI implementation:
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: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:- Request Adapter — transforms
InferenceRequestinto the provider’s HTTP request format - Body Format — structures the request body according to the provider’s API schema
- Message Format — converts Polyglot’s message format to the provider’s format
- Response Adapter — parses the provider’s HTTP response into
InferenceResponse - Usage Format — extracts token usage information from the response
BaseHttpRequestAdapter rather than implementing
CanTranslateInferenceRequest directly. It owns the request-building skeleton and leaves you
the only two methods that vary between providers:
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 theCanHandleVectorization interface:
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: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:
Note: TheEmbeddingsDriverRegistryis immutable — each mutation returns a new instance, matching the same pattern asInferenceDriverRegistry.
Removing or Replacing Bundled Drivers
TheInferenceDriverRegistry 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
BothInferenceRuntime and EmbeddingsRuntime dispatch events at key lifecycle points. You
can listen for specific events or wiretap all of them: