Coverage for /home/admin/Documents/AI/applications/lexigram-dev/lexigram/experimental/ai/lexigram-ai-llm/src/lexigram/ai/llm/exceptions.py: 100%

38 statements  

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

1"""LLM package exceptions.""" 

2 

3from __future__ import annotations 

4 

5from lexigram.contracts.ai.exceptions import ( 

6 ExtractionError as _ContractsExtractionError, 

7) 

8from lexigram.contracts.ai.exceptions import LLMError as _ContractsLLMError 

9 

10__all__ = [ 

11 "ExtractionError", 

12 "ExtractionMaxRetriesError", 

13 "ExtractionParseError", 

14 "ExtractionValidationError", 

15 "InvalidRequestError", 

16 "LLMAuthenticationError", 

17 "LLMContentFilterError", 

18 "LLMError", 

19 "LLMModelNotFoundError", 

20 "LLMQuotaExceededError", 

21 "LLMRateLimitError", 

22 "LLMTimeoutError", 

23 "ModelNotFoundError", 

24 "ModelRevisionMismatchError", 

25 "ProviderConnectionError", 

26 "StreamError", 

27 "TokenLimitError", 

28] 

29 

30 

31class LLMError(_ContractsLLMError): 

32 """Base exception for all LLM-domain errors in lexigram-ai-llm.""" 

33 

34 _code: str = "LEX_ERR_LLM_002" 

35 

36 

37class LLMAuthenticationError(LLMError): 

38 """Invalid API key or credentials — infrastructure error, raised not wrapped. 

39 

40 Raised as an exception (NOT wrapped in ``Result``). 

41 Indicates a misconfiguration the application cannot route around. 

42 """ 

43 

44 _code: str = "LEX_ERR_LLM_003" 

45 

46 

47class InvalidRequestError(LLMError): 

48 """Error raised when a request to an LLM provider is invalid.""" 

49 

50 _code: str = "LEX_ERR_LLM_004" 

51 

52 

53class ModelNotFoundError(LLMError): 

54 """Model unavailable or not found — recoverable via fallback routing.""" 

55 

56 _code: str = "LEX_ERR_LLM_005" 

57 

58 

59class LLMModelNotFoundError(ModelNotFoundError): 

60 """Model unavailable or not found — recoverable via fallback routing. 

61 

62 Returned as ``Err`` from ``LLMClientProtocol.complete()`` / ``stream_chat()``. 

63 The caller should route to a different model or provider. 

64 """ 

65 

66 _code: str = "LEX_ERR_LLM_006" 

67 

68 

69class LLMRateLimitError(LLMError): 

70 """Rate limit exceeded — recoverable via backoff/retry. 

71 

72 Returned as ``Err`` from ``LLMClientProtocol.complete()`` / ``stream_chat()``. 

73 The caller should implement exponential backoff or route to another provider. 

74 """ 

75 

76 _code: str = "LEX_ERR_LLM_007" 

77 

78 

79class LLMQuotaExceededError(LLMError): 

80 """API quota or billing limit exceeded — recoverable by routing elsewhere. 

81 

82 Returned as ``Err`` from ``LLMClientProtocol.complete()`` / ``stream_chat()``. 

83 The caller should route the request to a different provider or account. 

84 """ 

85 

86 _code: str = "LEX_ERR_LLM_008" 

87 

88 

89class LLMContentFilterError(LLMError): 

90 """Content blocked by provider safety filter — recoverable via reformulation. 

91 

92 Returned as ``Err`` from ``LLMClientProtocol.complete()`` / ``stream_chat()``. 

93 The caller should reformulate the prompt or inform the user. 

94 """ 

95 

96 _code: str = "LEX_ERR_LLM_009" 

97 

98 

99class TokenLimitError(LLMError): 

100 """Error raised when the token limit for a request is exceeded.""" 

101 

102 _code: str = "LEX_ERR_LLM_010" 

103 

104 

105class ProviderConnectionError(LLMError): 

106 """Error raised when connection to an LLM provider fails.""" 

107 

108 _code: str = "LEX_ERR_LLM_011" 

109 

110 

111class StreamError(LLMError): 

112 """Error raised during LLM response streaming.""" 

113 

114 _code: str = "LEX_ERR_LLM_012" 

115 

116 

117class ExtractionError(_ContractsExtractionError): 

118 """Base class for structured extraction errors in lexigram-ai-llm.""" 

119 

120 _code: str = "LEX_ERR_LLM_013" 

121 

122 

123class ExtractionParseError(ExtractionError): 

124 """Error raised when extraction response cannot be parsed as JSON.""" 

125 

126 _code: str = "LEX_ERR_LLM_014" 

127 

128 

129class ExtractionValidationError(ExtractionError): 

130 """Error raised when parsed extraction response fails schema validation.""" 

131 

132 _code: str = "LEX_ERR_LLM_015" 

133 

134 

135class ExtractionMaxRetriesError(ExtractionError): 

136 """Error raised when extraction max retries are exhausted.""" 

137 

138 _code: str = "LEX_ERR_LLM_016" 

139 

140 

141class ModelRevisionMismatchError(LLMError): 

142 """Raised when the provider returns a model revision that violates the pin policy.""" 

143 

144 _code: str = "LEX_ERR_LLM_017" 

145 

146 

147class LLMTimeoutError(LLMError): 

148 """Request exceeded the client timeout — recoverable by routing elsewhere. 

149 

150 Returned as ``Err`` from ``LLMClientProtocol.complete()`` / ``stream_chat()``. 

151 Not retried in place: the request already consumed a full timeout window, 

152 so the caller (router cascade) should advance to another provider. 

153 """ 

154 

155 _code: str = "LEX_ERR_LLM_018"