Coverage for /home/admin/Documents/AI/applications/lexigram-dev/lexigram/experimental/ai/lexigram-ai-guard/src/lexigram/ai/guard/output/base.py: 87%

15 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 07:19 +0800

1"""Abstract base class for output guards.""" 

2 

3from __future__ import annotations 

4 

5from abc import ABC, abstractmethod 

6from typing import TYPE_CHECKING, Any 

7 

8if TYPE_CHECKING: 

9 from lexigram.contracts.ai.exceptions import GuardError 

10 from lexigram.contracts.ai.guards import GuardResultProtocol 

11 from lexigram.result import Result 

12 

13 

14class AbstractOutputGuard(ABC): 

15 """Base class for all output content guards. 

16 

17 Subclasses implement :meth:`check` to evaluate LLM response content 

18 and return a :class:`~lexigram.ai.guard.pipeline.result.GuardCheckResult`. 

19 

20 Args: 

21 action: Default action to take when this guard triggers 

22 (``"block"``, ``"warn"``, or ``"redact"``). 

23 """ 

24 

25 def __init__(self, action: str = "block") -> None: 

26 """Initialise the guard with a default action. 

27 

28 Args: 

29 action: Action taken when the guard triggers. 

30 """ 

31 self._action = action 

32 

33 @property 

34 def name(self) -> str: 

35 """GuardProtocol identifier derived from the class name.""" 

36 return type(self).__name__ 

37 

38 @property 

39 def action(self) -> str: 

40 """Configured action for this guard.""" 

41 return self._action 

42 

43 @abstractmethod 

44 async def check( 

45 self, 

46 content: str, 

47 *, 

48 original_input: str | None = None, 

49 metadata: dict[str, Any] | None = None, 

50 ) -> Result[GuardResultProtocol, GuardError]: 

51 """Evaluate the LLM response and return a result. 

52 

53 Args: 

54 content: LLM response text to evaluate. 

55 original_input: The original user input for context. 

56 metadata: Optional metadata (model, provider, etc.). 

57 

58 Returns: 

59 Result indicating the outcome. 

60 """ 

61 

62 

63__all__ = ["AbstractOutputGuard"]