跳过内容

pydantic_ai.exceptions

模型重试

基类: Exception

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

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

Source code in pydantic_ai_slim/pydantic_ai/exceptions.py
 8
 9
10
11
12
13
14
15
16
17
18
19
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)

消息 instance-attribute

message: str = message

要返回给模型的消息。

用户错误

基类: RuntimeError

错误由应用程序开发人员的使用错误引起 — 你!

Source code in pydantic_ai_slim/pydantic_ai/exceptions.py
22
23
24
25
26
27
28
29
30
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)

消息 instance-attribute

message: str = message

错误的描述。

代理运行错误

基类: RuntimeError

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

Source code in pydantic_ai_slim/pydantic_ai/exceptions.py
33
34
35
36
37
38
39
40
41
42
43
44
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

消息 instance-attribute

message: str = message

错误消息。

使用限制已超出

基类: AgentRunError

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

Source code in pydantic_ai_slim/pydantic_ai/exceptions.py
47
48
class UsageLimitExceeded(AgentRunError):
    """Error raised when a Model's usage exceeds the specified limits."""

意外模型行为

基础类: AgentRunError

由于意外的模型行为导致错误,例如意外的响应代码。

Source code in pydantic_ai_slim/pydantic_ai/exceptions.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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

消息 instance-attribute

message: str = message

意外行为的描述。

主体 instance-attribute

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

响应的主体,如果可用的话。