guard
AI Agent Global guard that blocks real model requests. A test suite that stubs the model in most places still bills a provider (and goes flaky) the moment one path slips through. This module is the single switch that turns that silent slip into a loud failure:: from praisonaiagents.model_harness import allow_model_requestsconftest.py — nothing in this suite may reach a provider
allow_model_requests(False) Once blocked, the two request paths an :class:~praisonaiagents.agent.Agent
uses — LLM/LiteLLM (Chat Completions and Responses, sync, async and
streaming) and the OpenAI-native client — raise :class:ModelRequestBlocked
before any network I/O, and the message names the call site in your code that
triggered it.
Scope: this covers the agent’s own turn-taking. Auxiliary model calls made by
other subsystems (memory scoring, embeddings) hold their own clients and are
not routed through these two paths.
A :class:~praisonaiagents.model_harness.ScriptedModel answers from its script
without touching either path, so scripted tests keep passing while the guard is
on. That is the intended pairing: block globally, script explicitly.
The initial state can also be set from the environment, which is handy for CI
without touching test code::
PRAISONAI_ALLOW_MODEL_REQUESTS=0 pytest
Design note — why :class:BaseException: the agent’s tool loop wraps model
calls in broad except Exception handlers that degrade a failure into a
None result. A guard that can be swallowed is not a guard, so this error
follows the precedent pytest sets with its own outcome exceptions and derives
from BaseException.
Import
Classes
ModelRequestBlocked
Raised when a real model request is attempted while requests are blocked.
Functions
allow_model_requests()
Allow or block real model requests process-wide.
model_requests_allowed()
Return whether real model requests are currently permitted.
no_model_requests()
Block real model requests for the duration of the
with block.check_model_request()
Raise :class:
ModelRequestBlocked if real model requests are blocked.
