forked from lightspeed-core/lightspeed-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handler.py
More file actions
103 lines (89 loc) · 3.44 KB
/
Copy patherror_handler.py
File metadata and controls
103 lines (89 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Error mapping for agent inference failures to structured API error responses."""
from typing import TypeAlias
from ogx_client import APIConnectionError, APIStatusError
from pydantic_ai.exceptions import (
AgentRunError,
ContentFilterError,
IncompleteToolCall,
ModelAPIError,
ModelHTTPError,
UnexpectedModelBehavior,
UsageLimitExceeded,
)
from log import get_logger
from models.api.responses.error import (
AbstractErrorResponse,
InternalServerErrorResponse,
PromptTooLongResponse,
QuotaExceededResponse,
ServiceUnavailableResponse,
)
from utils.query import (
handle_known_apistatus_errors,
is_context_length_error,
)
AgentInferenceError: TypeAlias = (
AgentRunError | APIStatusError | APIConnectionError | RuntimeError
)
logger = get_logger(__name__)
def map_agent_inference_error(
exc: AgentInferenceError,
model_id: str,
) -> AbstractErrorResponse:
"""Map agent run failures from pydantic-ai or Llama Stack to an LCS error response.
Args:
exc: Agent, HTTP status, connection, or context-length runtime error.
model_id: Model identifier in provider/model format.
Returns:
Structured error response for HTTP or SSE error events.
Raises:
RuntimeError: Re-raised when ``exc`` is a non-agent ``RuntimeError`` that is
not a recognized context-length failure.
"""
match exc:
case AgentRunError() as agent_exc:
return map_pydantic_agent_run_error(agent_exc, model_id)
case APIStatusError() as status_exc:
return handle_known_apistatus_errors(status_exc, model_id)
case APIConnectionError() as connection_exc:
return ServiceUnavailableResponse(
backend_name="OGX",
cause=str(connection_exc),
)
case RuntimeError() as runtime_exc if is_context_length_error(str(runtime_exc)):
return PromptTooLongResponse(model=model_id)
case _:
return InternalServerErrorResponse.generic()
def map_pydantic_agent_run_error(
exc: AgentRunError, model_id: str
) -> AbstractErrorResponse:
"""Map pydantic-ai ``AgentRunError`` subclasses to LCS error responses.
Args:
exc: Agent exception to map.
model_id: Model identifier in provider/model format.
Returns:
Structured error response for HTTP or SSE error events.
"""
match exc:
case ContentFilterError() as filter_exc:
return InternalServerErrorResponse.query_failed(str(filter_exc))
case IncompleteToolCall():
return PromptTooLongResponse(model=model_id)
case UnexpectedModelBehavior():
logger.error("Unexpected model behavior: %s", exc, exc_info=True)
return InternalServerErrorResponse.generic()
case UsageLimitExceeded():
return QuotaExceededResponse.model(model_id)
case ModelHTTPError() as http_exc if is_context_length_error(str(http_exc)):
return PromptTooLongResponse(model=model_id)
case ModelHTTPError(status_code=429):
return QuotaExceededResponse.model(model_id)
case ModelHTTPError():
return InternalServerErrorResponse.generic()
case ModelAPIError() as api_exc:
return ServiceUnavailableResponse(
backend_name="OGX",
cause=str(api_exc),
)
case _:
return InternalServerErrorResponse.query_failed(str(exc))