Heuristic:2aronS Duel Agents Thin Subclass Over Wrapper
| Knowledge Sources | |
|---|---|
| Domains | Architecture, LLMs |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
When a framework already provides a capable class for a compatible wire protocol, subclass it and change only the defaults — do not wrap it and re-implement its surface area.
Description
Both `ChatDuel` (LangChain) and `DuelLLM` (LlamaIndex) integrate Duel into an existing framework by subclassing a base class whose wire protocol is already OpenAI-compatible: `ChatDuel extends ChatOpenAI` and `DuelLLM extends OpenAILike`. Instead of wrapping these classes and forwarding every method, the subclasses only change the defaults — the model name (`duel-auto`), the base URL (the Duel proxy), and the source of the API key (the `DUEL_API_KEY` env var). All of the framework's existing methods (`invoke`, `complete`, `stream`, batching, retries) are inherited untouched, so the integration is a few dozen lines rather than a full re-implementation. `ChatDuel` uses a Pydantic `model_validator(mode="before")` to set defaults before validation; `DuelLLM` does the same in `__init__` before calling `super().__init__`. In both cases the framework remains in charge of the actual HTTP call.
Usage
Use this heuristic when the upstream framework already speaks a wire protocol your service is compatible with (here, OpenAI chat completions), and you only need to redirect the endpoint and the credential source. It is the right choice when the value you add is defaults, not behavior.
Do not use it when you need to change the request/response shape, add new methods, or alter streaming semantics — at that point a wrapper or a from-scratch client is clearer than a subclass that overrides half the parent.
The Insight (Rule of Thumb)
- Action: Subclass the framework's base class; override only defaults (model, base URL, key source).
- Value: `class ChatDuel(ChatOpenAI)` with one `model_validator`; `class DuelLLM(OpenAILike)` with a default-setting `__init__`. No method forwarding, no re-declared fields.
- Trade-off: You inherit the parent's behavior wholesale, including its bugs and its opinionated defaults; if the parent changes a signature, your subclass must follow.
- Compatibility: Keep the framework in charge of the HTTP call so retry/streaming/batching behaviors come for free. Expose the credential as a framework-native secret (`lc_secrets` in LangChain) so it is redacted from serialized output.
Reasoning
A wrapper that re-implements `invoke`/`complete`/`stream` is hundreds of lines of forwarding code that must track every upstream change, and it denies the user any framework feature the wrapper did not explicitly expose. Subclassing inverts this: you get every current and future framework method for free, and your integration is small enough to audit in one screen. The risk — inheriting the parent's behavior — is acceptable precisely because the wire protocol is compatible: the parent's behavior is what you wanted anyway. The default-setting hook (`model_validator(mode="before")` for ChatDuel, `__init__` for DuelLLM) runs before the framework validates or sends, so by the time the parent sees the values they are already the Duel ones.
Code Evidence
Subclass with default-setting validator in `python/langchain-duel/langchain_duel/chat_models.py:15,63-83`:
class ChatDuel(ChatOpenAI):
"""Duel Agents chat model... builds on ChatOpenAI and only changes the defaults."""
@model_validator(mode="before")
@classmethod
def _set_duel_defaults(cls, values: Any) -> Any:
if not isinstance(values, dict):
return values
values.setdefault("model", DEFAULT_MODEL)
values.setdefault(
"base_url", os.getenv("DUEL_PROXY_URL", DUEL_API_BASE)
)
has_key = any(
values.get(field) for field in ("api_key", "openai_api_key")
)
if not has_key:
env_key = os.getenv("DUEL_API_KEY")
if env_key:
values["api_key"] = env_key
return values
Subclass with default-setting `__init__` in `python/llama-index-llms-duel/llama_index/llms/duel/base.py:12,35-52`:
class DuelLLM(OpenAILike):
"""Duel Agents LLM... builds on OpenAILike."""
def __init__(
self,
model: str = DEFAULT_MODEL,
api_base: str = DEFAULT_API_BASE,
api_key: Optional[str] = None,
is_chat_model: bool = True,
**kwargs: Any,
) -> None:
api_base = os.getenv("DUEL_PROXY_URL", api_base)
api_key = api_key or os.getenv("DUEL_API_KEY")
super().__init__(
model=model, api_base=api_base, api_key=api_key,
is_chat_model=is_chat_model, **kwargs,
)