Debian Codegraph @

src/anthropic/types/beta/beta_message.py

raw
1 from typing import List, Optional
2 from typing_extensions import Literal
3
4 from ..model import Model
5 from ..._models import BaseModel
6 from .beta_usage import BetaUsage
7 from .beta_container import BetaContainer
8 from .beta_diagnostics import BetaDiagnostics
9 from .beta_stop_reason import BetaStopReason
10 from .beta_content_block import BetaContentBlock, BetaContentBlock as BetaContentBlock
11 from .beta_message_param import BetaMessageParam
12 from .beta_input_transformation import BetaInputTransformation
13 from .beta_refusal_stop_details import BetaRefusalStopDetails
14 from .beta_context_management_response import BetaContextManagementResponse
15
16 __all__ = ["BetaMessage"]
17
18
19 class BetaMessage(BaseModel):
20 id: str
21 """Unique object identifier.
22
23 The format and length of IDs may change over time.
24 """
25
26 container: Optional[BetaContainer] = None
27 """Information about the container used in this request.
28
29 This will be non-null if a container tool (e.g. code execution) was used.
30 """
31
32 content: List[BetaContentBlock]
33 """Content generated by the model.
34
35 This is an array of content blocks, each of which has a `type` that determines
36 its shape.
37
38 Example:
39
40 ```json
41 [{ "type": "text", "text": "Hi, I'm Claude." }]
42 ```
43
44 If the request input `messages` ended with an `assistant` turn, then the
45 response `content` will continue directly from that last turn. You can use this
46 to constrain the model's output.
47
48 For example, if the input `messages` were:
49
50 ```json
51 [
52 {
53 "role": "user",
54 "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
55 },
56 { "role": "assistant", "content": "The best answer is (" }
57 ]
58 ```
59
60 Then the response `content` might be:
61
62 ```json
63 [{ "type": "text", "text": "B)" }]
64 ```
65 """
66
67 context_management: Optional[BetaContextManagementResponse] = None
68 """Context management response.
69
70 Information about context management strategies applied during the request.
71 """
72
73 diagnostics: Optional[BetaDiagnostics] = None
74 """Request-level diagnostics.
75
76 `null` when the request did not supply `diagnostics`, or when it did and no
77 prompt-cache divergence was detected.
78 """
79
80 model: Model
81 """The model that will complete your prompt.
82
83 See [models](https://docs.anthropic.com/en/docs/models-overview) for additional
84 details and options.
85 """
86
87 role: Literal["assistant"]
88 """Conversational role of the generated message.
89
90 This will always be `"assistant"`.
91 """
92
93 stop_details: Optional[BetaRefusalStopDetails] = None
94 """Structured information about why model output stopped.
95
96 This is `null` when the `stop_reason` has no additional detail to report.
97 """
98
99 stop_reason: Optional[BetaStopReason] = None
100 """The reason that we stopped.
101
102 This may be one the following values:
103
104 - `"end_turn"`: the model reached a natural stopping point
105 - `"max_tokens"`: we exceeded the requested `max_tokens` or the model's maximum
106 - `"stop_sequence"`: one of your provided custom `stop_sequences` was generated
107 - `"tool_use"`: the model invoked one or more tools
108 - `"pause_turn"`: we paused a long-running turn. You may provide the response
109 back as-is in a subsequent request to let the model continue.
110 - `"refusal"`: when streaming classifiers intervene to handle potential policy
111 violations
112 - `"model_context_window_exceeded"`: we exceeded the model's context window
113
114 In non-streaming mode this value is always non-null. In streaming mode, it is
115 null in the `message_start` event and non-null otherwise.
116 """
117
118 stop_sequence: Optional[str] = None
119 """Which custom stop sequence was generated, if any.
120
121 This value will be a non-null string if one of your custom stop sequences was
122 generated.
123 """
124
125 type: Literal["message"]
126 """Object type.
127
128 For Messages, this is always `"message"`.
129 """
130
131 usage: BetaUsage
132 """Billing and rate-limit usage.
133
134 Anthropic's API bills and rate-limits by token counts, as tokens represent the
135 underlying cost to our systems.
136
137 Under the hood, the API transforms requests into a format suitable for the
138 model. The model's output then goes through a parsing stage before becoming an
139 API response. As a result, the token counts in `usage` will not match one-to-one
140 with the exact visible content of an API request or response.
141
142 For example, `output_tokens` will be non-zero, even for an empty string response
143 from Claude.
144
145 Total input tokens in a request is the summation of `input_tokens`,
146 `cache_creation_input_tokens`, and `cache_read_input_tokens`.
147 """
148
149 input_transformations: Optional[List[BetaInputTransformation]] = None
150 """
151 Changes the API made to the request's input before showing it to the model, and
152 blocks that failed a binding check but were left unchanged: one entry per block,
153 in request order. Two entry types today. `thinking_dropped` — a `thinking`,
154 `redacted_thinking` or `connector_text` block from the request's `messages` that
155 was removed from the prompt instead of being shown to the model because it
156 failed a binding check. `thinking_mismatch_allowed` — a `thinking` or
157 `redacted_thinking` block that failed the conversation check (the conversation
158 before it differs from the one it was created in, or it carries no record of one
159 on a model that requires it) and was shown to the model all the same, because
160 that check is not enforced for this request. More entry types may be added over
161 time; ignore types you do not recognize.
162
163 Requires `anthropic-beta: thinking-binding-controls-2026-08-01`. Present on
164 every such response from a model that supports extended thinking, as `[]` when
165 there is no entry to report; without the beta, blocks are removed or left in
166 place all the same but nothing is reported. Removed blocks contribute nothing to
167 `usage.input_tokens`; blocks left in place count as sent. When streaming, the
168 array is final in `message_start`; the final `message_delta` event carries it
169 only when a server-side model fallback happened mid-stream, in which case it
170 holds the serving model's entries and replaces the one in `message_start`.
171 """
172
173 def to_param(self) -> BetaMessageParam:
174 """Convert this response BetaMessage to a BetaMessageParam so it can be passed back
175 to the API as an input message, e.g. in the `messages` param.
176 """
177 return BetaMessageParam(role=self.role, content=list(self.content))