Debian Codegraph @

src/anthropic/types/message.py

raw
1 from typing import List, Optional
2 from typing_extensions import Literal
3
4 from .model import Model
5 from .usage import Usage
6 from .._models import BaseModel
7 from .container import Container
8 from .diagnostics import Diagnostics
9 from .stop_reason import StopReason
10 from .content_block import ContentBlock, ContentBlock as ContentBlock
11 from .message_param import MessageParam
12 from .refusal_stop_details import RefusalStopDetails
13
14 __all__ = ["Message"]
15
16
17 class Message(BaseModel):
18 id: str
19 """Unique object identifier.
20
21 The format and length of IDs may change over time.
22 """
23
24 container: Optional[Container] = None
25 """Information about the container used in this request.
26
27 This will be non-null if a container tool (e.g. code execution) was used.
28 """
29
30 content: List[ContentBlock]
31 """Content generated by the model.
32
33 This is an array of content blocks, each of which has a `type` that determines
34 its shape.
35
36 Example:
37
38 ```json
39 [{ "type": "text", "text": "Hi, I'm Claude." }]
40 ```
41
42 If the request input `messages` ended with an `assistant` turn, then the
43 response `content` will continue directly from that last turn. You can use this
44 to constrain the model's output.
45
46 For example, if the input `messages` were:
47
48 ```json
49 [
50 {
51 "role": "user",
52 "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
53 },
54 { "role": "assistant", "content": "The best answer is (" }
55 ]
56 ```
57
58 Then the response `content` might be:
59
60 ```json
61 [{ "type": "text", "text": "B)" }]
62 ```
63 """
64
65 diagnostics: Optional[Diagnostics] = None
66 """Request-level diagnostics.
67
68 `null` when the request did not supply `diagnostics`, or when it did and no
69 prompt-cache divergence was detected.
70 """
71
72 model: Model
73 """The model that will complete your prompt.
74
75 See [models](https://docs.anthropic.com/en/docs/models-overview) for additional
76 details and options.
77 """
78
79 role: Literal["assistant"]
80 """Conversational role of the generated message.
81
82 This will always be `"assistant"`.
83 """
84
85 stop_details: Optional[RefusalStopDetails] = None
86 """Structured information about why model output stopped.
87
88 This is `null` when the `stop_reason` has no additional detail to report.
89 """
90
91 stop_reason: Optional[StopReason] = None
92 """The reason that we stopped.
93
94 This may be one the following values:
95
96 - `"end_turn"`: the model reached a natural stopping point
97 - `"max_tokens"`: we exceeded the requested `max_tokens` or the model's maximum
98 - `"stop_sequence"`: one of your provided custom `stop_sequences` was generated
99 - `"tool_use"`: the model invoked one or more tools
100 - `"pause_turn"`: we paused a long-running turn. You may provide the response
101 back as-is in a subsequent request to let the model continue.
102 - `"refusal"`: when streaming classifiers intervene to handle potential policy
103 violations
104 - `"model_context_window_exceeded"`: we exceeded the model's context window
105
106 In non-streaming mode this value is always non-null. In streaming mode, it is
107 null in the `message_start` event and non-null otherwise.
108 """
109
110 stop_sequence: Optional[str] = None
111 """Which custom stop sequence was generated, if any.
112
113 This value will be a non-null string if one of your custom stop sequences was
114 generated.
115 """
116
117 type: Literal["message"]
118 """Object type.
119
120 For Messages, this is always `"message"`.
121 """
122
123 usage: Usage
124 """Billing and rate-limit usage.
125
126 Anthropic's API bills and rate-limits by token counts, as tokens represent the
127 underlying cost to our systems.
128
129 Under the hood, the API transforms requests into a format suitable for the
130 model. The model's output then goes through a parsing stage before becoming an
131 API response. As a result, the token counts in `usage` will not match one-to-one
132 with the exact visible content of an API request or response.
133
134 For example, `output_tokens` will be non-zero, even for an empty string response
135 from Claude.
136
137 Total input tokens in a request is the summation of `input_tokens`,
138 `cache_creation_input_tokens`, and `cache_read_input_tokens`.
139 """
140
141 def to_param(self) -> MessageParam:
142 """Convert this response Message to a MessageParam so it can be passed back
143 to the API as an input message, e.g. in the `messages` param.
144 """
145 return MessageParam(role=self.role, content=list(self.content))