Index
engine
¶
Inference Engine primitive — LLM runtime management.
Classes¶
EngineConnectionError
¶
Bases: Exception
Raised when an engine is unreachable.
EngineContextLengthError
¶
Bases: EngineConnectionError
The prompt exceeds the served model's maximum context window.
Subclasses EngineConnectionError so existing except
EngineConnectionError handlers keep catching it, while callers that want a
distinct, user-facing "conversation too long" message can branch on this type
(or the is_context_length_error marker) instead of surfacing a generic
engine failure.
InferenceEngine
¶
Bases: ABC
Base class for all inference engine backends.
Subclasses must be registered via
@EngineRegistry.register("name") to become discoverable.
Methods:¶
generate
abstractmethod
¶
generate(
messages: Sequence[Message],
*,
model: str,
temperature: float = 0.7,
max_tokens: int = 1024,
**kwargs: Any,
) -> Dict[str, Any]
Synchronous completion — returns a dict with content and usage.
Source code in src/diapason/engine/_stubs.py
stream
abstractmethod
async
¶
stream(
messages: Sequence[Message],
*,
model: str,
temperature: float = 0.7,
max_tokens: int = 1024,
**kwargs: Any,
) -> AsyncIterator[str]
Yield token strings as they are generated.
Source code in src/diapason/engine/_stubs.py
stream_full
async
¶
stream_full(
messages: Sequence[Message],
*,
model: str,
temperature: float = 0.7,
max_tokens: int = 1024,
**kwargs: Any,
) -> AsyncIterator["StreamChunk"]
Yield full StreamChunks including tool_calls and finish_reason.
Default implementation wraps stream() for backward compatibility.
Engines with native tool-call streaming should override this.
Source code in src/diapason/engine/_stubs.py
list_models
abstractmethod
¶
health
abstractmethod
¶
can_serve
¶
Return True if this engine can serve model.
Defaults to True: local engines accept any model id (whether a
specific model is installed is a separate concern from engine
selection). Engines that multiplex provider-specific clients (e.g.
the cloud engine) override this so selection can skip an engine whose
client for the model's provider isn't configured (see #532).
Source code in src/diapason/engine/_stubs.py
close
¶
Functions:¶
looks_like_context_length_error
¶
True when text reads like a context-window overflow error.
The single shared heuristic for recognizing vendor context-overflow phrasings — used by the engine layer (typing upstream 400s), agent error classification, and the server stream bridge, so a new vendor phrasing only ever needs to be added here.
Source code in src/diapason/engine/_base.py
messages_to_dicts
¶
messages_to_dicts(
messages: Sequence[Message],
) -> List[Dict[str, Any]]
Convert Message objects to OpenAI-format dicts.
Source code in src/diapason/engine/_base.py
discover_engines
¶
discover_engines(
config: DiapasonConfig,
) -> List[Tuple[str, InferenceEngine]]
Probe registered engines and return [(key, instance)] for healthy ones.
Results are sorted with the config default engine first.
Source code in src/diapason/engine/_discovery.py
discover_models
¶
discover_models(
engines: List[Tuple[str, InferenceEngine]],
) -> Dict[str, List[str]]
Call list_models() on each engine and return a dict.
Source code in src/diapason/engine/_discovery.py
get_engine
¶
get_engine(
config: DiapasonConfig,
engine_key: str | None = None,
model: str | None = None,
) -> Tuple[str, InferenceEngine] | None
Get a specific engine by key, or the default with fallback.
When model is given, an engine is selected only if it can actually
serve that model (engine.can_serve(model)). This stops the cloud
fallback from being chosen — when the local engine is down — for a model
whose provider client is missing, which otherwise surfaces as a confusing
"OpenAI client not available" instead of a helpful "start your local
engine" message (see #532). When model is None selection stays
model-agnostic (unchanged behaviour).
Returns (key, engine_instance) or None if no engine is available.