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 Factory

For more control over instantiation, pass a callable that receives LLMConfig, CanSendHttpRequests, and CanHandleEvents, and returns a CanProcessInferenceRequest:
This factory approach is particularly useful when you want to extend an existing driver with minimal code — for example, adding request logging or custom headers to an OpenAI-compatible endpoint.

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
Most bundled drivers follow this modular adapter pattern. See the OpenAIDriver or AnthropicDriver source code for reference implementations.

Custom Embeddings Drivers

Embeddings drivers implement the CanHandleVectorization interface:
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: