Sub topic 1
Why One Interface: The Cost of Calling Providers Directly
Prerequisites
Assume you can read Python, use JSON-based HTTP APIs, and understand interfaces or protocols. You should also know the basic structure of a chat completion request.
Direct Provider Calls Create Coupling
Provider coupling happens when application code depends directly on one provider's SDK or JSON format. A route handler might construct provider-specific messages, pass provider-specific options, and interpret provider-specific responses.
That code works at first. Later, changing providers requires edits in many places. Tests also need network calls or provider-specific mocks. Differences in authentication, errors, token counts, streaming, and tool calls spread through the application.
A single application-level interface limits this coupling. Calling code depends on your types and interface. Provider-specific code stays in adapters.
How Provider APIs Differ
Provider APIs often describe similar operations with different shapes and rules.
| Concern | Common difference |
|---|---|
| Messages | Role names, content formats, or multimodal parts differ |
| Options | Parameter names and supported ranges differ |
| Responses | Text, usage, finish reasons, and metadata use different fields |
| Errors | Status codes and retry behavior differ |
| Features | Streaming, tools, and structured output are not equally supported |
A neutral interface should hide mechanical differences, but it should not pretend that models behave identically. A model's quality, context limit, latency, and supported features remain provider-specific.
Designing Neutral Types
Define data types around what the application needs, rather than copying one provider's request schema. For example, an application can use messages, a model identifier, a token limit, and optional sampling control.
from dataclasses import dataclass
from typing import Protocol
@dataclass
class Message:
role: str
content: str
@dataclass
class ChatRequest:
model: str
messages: list[Message]
max_tokens: int = 512
temperature: float | None = None
@dataclass
class ChatResponse:
text: str
model: str
input_tokens: int | None = None
output_tokens: int | None = NoneDefining the Provider Interface
A provider interface specifies the operation that calling code may use. Python's Protocol supports structural typing: any class with the required method satisfies the interface.
class ChatProvider(Protocol):
def complete(self, request: ChatRequest) -> ChatResponse: ...An adapter translates ChatRequest into a provider's format and translates its response back into ChatResponse. The application calls complete without knowing which adapter is active.
A common error is leaking the provider's raw response through this method. That restores coupling and makes switching difficult. Keep translation at the boundary.
Exercise
Where should provider-specific message fields be translated?
Show answer
Inside the provider adapter, at the boundary between the neutral interface and the provider API.