Loading Python for AI Engineering...
Using typing.Protocol to define compile-time verified interfaces without requiring explicit inheritance. Allows swapping vector databases, embedding providers, and LLM backends via structural subtyping.
{
"query": "vector index latency",
"confidence": 0.94,
"is_urgent": true
}“A Protocol is a job description: if a candidate has a valid passport and knows Python, they qualify for the job, regardless of what school they graduated from.”
Forcing classes to inherit from monolithic ABCs (Abstract Base Classes), causing brittle diamond inheritance hierarchies.
from typing import Protocol, runtime_checkable
@runtime_checkable
class LLMProvider(Protocol):
async def generate(self, prompt: str, temperature: float = 0.7) -> str: ...
# Any class matching the signature satisfies the Protocol automatically:
class MockLocalLLM:
async def generate(self, prompt: str, temperature: float = 0.7) -> str:
return "Deterministic test response"
def execute_pipeline(model: LLMProvider):
assert isinstance(model, LLMProvider) # Verified!