Sub topic 3
Designing provider-neutral request and response types
This chapter assumes you read and write Python, know HTTP and JSON, and have called a hosted chat completion API at least once. It also assumes you know what a Protocol or abstract class is.
What the neutral types are for
Every provider SDK has its own request and response shapes. If your application code builds those shapes directly, every call site depends on one vendor. Neutral types fix this. They are plain data classes that your application owns. Application code builds a ChatRequest and reads a ChatResponse. Only the adapter for each provider knows the vendor format.
Choose the common subset
The neutral types should hold what every provider you care about supports. For chat completions, that is a model name, a list of messages, a limit on output length, and an optional sampling temperature. The response needs the generated text, the model that answered, and token counts.
from dataclasses import dataclass, field
from typing import Protocol
@dataclass
class Message:
role: str # "system", "user" or "assistant"
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 = None
class ChatProvider(Protocol):
def complete(self, request: ChatRequest) -> ChatResponse: ...The ChatProvider protocol is the only surface that application code depends on. The next chapter covers it in detail. Here, focus on the design choices in the data types.
Design decisions
Keep roles as the lowest common denominator
Some APIs accept the system prompt as a message with the role system. Others take it as a separate top-level field. The neutral type uses a system message, because it is easy to represent. An adapter for a provider with a separate field pulls the system messages out of the list and moves them. The application never sees this.
Make optional fields truly optional
temperature defaults to None, not to a number. None means "the caller has no opinion". The adapter then leaves the field out and the provider applies its own default. If you filled in 0.7 yourself, you would silently override every provider's default, and the defaults differ.
The same reasoning applies to token counts in the response. Some providers report usage only in certain modes, so input_tokens and output_tokens can be None. Callers must handle that.
Always return the model that answered
ChatResponse.model is the model the provider reports, not the one you asked for. Providers may resolve an alias to a dated version. Logging the actual model helps when you compare outputs or debug a change in behavior.
Use your own types, not a vendor's
Do not reuse a vendor SDK class as your neutral type, even if it looks generic. The moment you do, the SDK becomes a dependency of every module that touches a request, and any change in that SDK reaches your whole codebase.
Leave out what you do not need yet
Fields such as streaming flags, tool definitions, and stop sequences are real features, but they differ a lot across providers. Add them when you have a use for them, and design each one against at least two providers. A field that fits only one vendor leaks that vendor's design into your types.
A quick check of the types
You can use the types with no provider and no network:
request = ChatRequest(
model="example-model",
messages=[
Message("system", "You are concise."),
Message("user", "Define a protocol in one sentence."),
],
max_tokens=100,
)
print(request.temperature) # None: the provider default applies
print(len(request.messages)) # 2Summary of the design rules
| Rule | Reason |
|---|---|
| Own your types | Vendor SDK changes stay inside adapters |
| Use `None` for "no preference" | Provider defaults are not overridden |
| Keep only the common subset | Every field must work on every adapter |
| Report the actual model and usage | Debugging and cost tracking |
Exercise
A new provider requires the system prompt in a separate top-level field, not in the message list. Which part of your code changes, and why is the neutral ChatRequest left as it is?
Show answer
Only the adapter for that provider changes. It filters the messages with role "system" out of the list and sends them in the separate field. The neutral ChatRequest stays the same because the system message is already representable, and application code should not know how a vendor wants it formatted.
Exercise
Why does ChatRequest.temperature default to None instead of 0.7?
Show answer
None means the caller has no preference, so the adapter omits the field and each provider uses its own default. A fixed 0.7 would override those defaults for every provider and make the choice invisible to the caller.