Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:416rehman DeepZero Protocol Abstraction

From Leeroopedia
Knowledge Sources
Domains LLMs, Architecture
Last Updated 2026-10-27 00:00 GMT

Overview

Use `typing.Protocol` (`LLMProtocol`) to decouple pipeline stages from the concrete LLMProvider, so any object with a compatible `complete()` signature can be injected — including test mocks.

Description

The `GenericLLM` stage and the pipeline runner depend on an `LLMProtocol` — a `@runtime_checkable` `typing.Protocol` declaring the `complete()` method signature — rather than on the concrete `LLMProvider` class. This means the runner accepts any object that implements `complete(messages, max_retries, initial_backoff, max_backoff, backoff_decay, **kwargs) -> str`, whether it is the real LLMProvider, a future alternative, or a test mock. The Protocol is `runtime_checkable`, so `isinstance()` checks work at runtime without requiring inheritance.

Usage

Use this heuristic when designing interfaces between pipeline infrastructure and pluggable components. Define a `Protocol` with the minimal method signature needed, mark it `@runtime_checkable`, and depend on that Protocol — not the concrete class — in consumer code. This enables dependency injection for testing without coupling.

The Insight (Rule of Thumb)

  • Action: Define `LLMProtocol` as a `@runtime_checkable` Protocol with the `complete()` signature. Type pipeline components against `LLMProtocol | None`, not `LLMProvider | None`.
  • Value: `complete(messages: list[dict[str, str]], max_retries: int, initial_backoff: float, max_backoff: float, backoff_decay: float, **kwargs) -> str`
  • Trade-off: Protocols provide structural typing (duck typing with static checks) but no shared base class behavior. All logic must be in the concrete implementation, not inherited.
  • Compatibility: Works with any Python 3.8+ codebase; `runtime_checkable` adds negligible overhead.

Reasoning

Coupling the pipeline to the concrete `LLMProvider` class would force every test and alternative implementation to either inherit from it or monkey-patch it. A Protocol provides structural subtyping: any object with the right method signature satisfies the Protocol, no inheritance required. This is especially valuable for testing — a simple mock object with a `complete()` method that returns a fixed string is a valid `LLMProtocol`, no mocking framework needed. The `@runtime_checkable` decorator enables `isinstance(obj, LLMProtocol)` for defensive checks without importing the concrete class.

Code Evidence

Protocol definition from `stage.py:31-41`:

@runtime_checkable
class LLMProtocol(Protocol):
    def complete(
        self,
        messages: list[dict[str, str]],
        max_retries: int = ...,
        initial_backoff: float = ...,
        max_backoff: float = ...,
        backoff_decay: float = ...,
        **kwargs: Any,
    ) -> str: ...

Consumer uses Protocol, not concrete class from `runner.py:75`:

llm: LLMProtocol | None = None,

Stage uses Protocol from `stage.py:122`:

# llm provider if configured
llm: LLMProtocol | None

GenericLLM checks for None (not isinstance) from `stages/llm.py:54-55`:

def process(self, ctx: ProcessorContext, entry: ProcessorEntry) -> ProcessorResult:
    if ctx.llm is None:
        return ProcessorResult.fail("no llm provider configured for generic_llm")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment