跳到内容

pydantic_ai.exceptions

ModelRetry

基类:Exception

当工具函数应重试时引发的异常。

代理将消息返回给模型,并要求它再次尝试调用函数/工具。

源代码位于 pydantic_ai_slim/pydantic_ai/exceptions.py
22
23
24
25
26
27
28
29
30
31
32
33
class ModelRetry(Exception):
    """Exception raised when a tool function should be retried.

    The agent will return the message to the model and ask it to try calling the function/tool again.
    """

    message: str
    """The message to return to the model."""

    def __init__(self, message: str):
        self.message = message
        super().__init__(message)

message instance-attribute

message: str = message

要返回给模型的消息。

UserError

基类:RuntimeError

由应用程序开发者(您!)的用法错误引起错误!

源代码位于 pydantic_ai_slim/pydantic_ai/exceptions.py
36
37
38
39
40
41
42
43
44
class UserError(RuntimeError):
    """Error caused by a usage mistake by the application developer — You!"""

    message: str
    """Description of the mistake."""

    def __init__(self, message: str):
        self.message = message
        super().__init__(message)

message instance-attribute

message: str = message

错误的描述。

AgentRunError

基类:RuntimeError

代理运行期间发生错误的基本类。

源代码位于 pydantic_ai_slim/pydantic_ai/exceptions.py
47
48
49
50
51
52
53
54
55
56
57
58
class AgentRunError(RuntimeError):
    """Base class for errors occurring during an agent run."""

    message: str
    """The error message."""

    def __init__(self, message: str):
        self.message = message
        super().__init__(message)

    def __str__(self) -> str:
        return self.message

message instance-attribute

message: str = message

错误消息。

UsageLimitExceeded

基类:AgentRunError

当模型的用量超过指定限制时引发的错误。

源代码位于 pydantic_ai_slim/pydantic_ai/exceptions.py
61
62
class UsageLimitExceeded(AgentRunError):
    """Error raised when a Model's usage exceeds the specified limits."""

UnexpectedModelBehavior

基类:AgentRunError

由意外的模型行为引起的错误,例如,意外的响应代码。

源代码位于 pydantic_ai_slim/pydantic_ai/exceptions.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class UnexpectedModelBehavior(AgentRunError):
    """Error caused by unexpected Model behavior, e.g. an unexpected response code."""

    message: str
    """Description of the unexpected behavior."""
    body: str | None
    """The body of the response, if available."""

    def __init__(self, message: str, body: str | None = None):
        self.message = message
        if body is None:
            self.body: str | None = None
        else:
            try:
                self.body = json.dumps(json.loads(body), indent=2)
            except ValueError:
                self.body = body
        super().__init__(message)

    def __str__(self) -> str:
        if self.body:
            return f'{self.message}, body:\n{self.body}'
        else:
            return self.message

message instance-attribute

message: str = message

意外行为的描述。

body instance-attribute

body: str | None = dumps(loads(body), indent=2)

响应的主体(如果可用)。

ModelHTTPError

基类:AgentRunError

当模型提供商响应的状态代码为 4xx 或 5xx 时引发。

源代码位于 pydantic_ai_slim/pydantic_ai/exceptions.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class ModelHTTPError(AgentRunError):
    """Raised when an model provider response has a status code of 4xx or 5xx."""

    status_code: int
    """The HTTP status code returned by the API."""

    model_name: str
    """The name of the model associated with the error."""

    body: object | None
    """The body of the response, if available."""

    message: str
    """The error message with the status code and response body, if available."""

    def __init__(self, status_code: int, model_name: str, body: object | None = None):
        self.status_code = status_code
        self.model_name = model_name
        self.body = body
        message = f'status_code: {status_code}, model_name: {model_name}, body: {body}'
        super().__init__(message)

message instance-attribute

message: str

包含状态代码和响应主体的错误消息(如果可用)。

status_code instance-attribute

status_code: int = status_code

API 返回的 HTTP 状态代码。

model_name instance-attribute

model_name: str = model_name

与错误关联的模型的名称。

body instance-attribute

body: object | None = body

响应的主体(如果可用)。

FallbackExceptionGroup

基类:ExceptionGroup

当所有回退模型都失败时可能引发的异常组。

源代码位于 pydantic_ai_slim/pydantic_ai/exceptions.py
114
115
class FallbackExceptionGroup(ExceptionGroup):
    """A group of exceptions that can be raised when all fallback models fail."""