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 """
28 Information about the container used in the request (for the code execution
29 tool)
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 """
75 Request-level diagnostics: why the prompt cache could not fully reuse the prefix
76 of the request named by `diagnostics.previous_message_id`.
77 """
78
79 model: Model
80 """The model that will complete your prompt.
81
82 See [models](https://docs.anthropic.com/en/docs/models-overview) for additional
83 details and options.
84 """
85
86 role: Literal["assistant"]
87 """Conversational role of the generated message.
88
89 This will always be `"assistant"`.
90 """
91
92 stop_details: Optional[BetaRefusalStopDetails] = None
93 """Structured information about a refusal."""
94
95 stop_reason: Optional[BetaStopReason] = None
96 """The reason that we stopped.
97
98 This may be one the following values:
99
100 - `"end_turn"`: the model reached a natural stopping point
101 - `"max_tokens"`: we exceeded the requested `max_tokens` or the model's maximum
102 - `"stop_sequence"`: one of your provided custom `stop_sequences` was generated
103 - `"tool_use"`: the model invoked one or more tools
104 - `"pause_turn"`: we paused a long-running turn. You may provide the response
105 back as-is in a subsequent request to let the model continue.
106 - `"refusal"`: when streaming classifiers intervene to handle potential policy
107 violations
108 - `"model_context_window_exceeded"`: we exceeded the model's context window
109
110 In non-streaming mode this value is always non-null. In streaming mode, it is
111 null in the `message_start` event and non-null otherwise.
112 """
113
114 stop_sequence: Optional[str] = None
115 """Which custom stop sequence was generated, if any.
116
117 This value will be a non-null string if one of your custom stop sequences was
118 generated.
119 """
120
121 type: Literal["message"]
122 """Object type.
123
124 For Messages, this is always `"message"`.
125 """
126
127 usage: BetaUsage
128 """Billing and rate-limit usage.
129
130 Anthropic's API bills and rate-limits by token counts, as tokens represent the
131 underlying cost to our systems.
132
133 Under the hood, the API transforms requests into a format suitable for the
134 model. The model's output then goes through a parsing stage before becoming an
135 API response. As a result, the token counts in `usage` will not match one-to-one
136 with the exact visible content of an API request or response.
137
138 For example, `output_tokens` will be non-zero, even for an empty string response
139 from Claude.
140
141 Total input tokens in a request is the summation of `input_tokens`,
142 `cache_creation_input_tokens`, and `cache_read_input_tokens`.
143 """
144
145 input_transformations: Optional[List[BetaInputTransformation]] = None
146 """
147 Changes the API made to the request's input before showing it to the model, and
148 blocks that failed a binding check but were left unchanged: one entry per block,
149 in request order. Two entry types today. `thinking_dropped` — a `thinking`,
150 `redacted_thinking` or `connector_text` block from the request's `messages` that
151 was removed from the prompt instead of being shown to the model because it
152 failed a binding check. `thinking_mismatch_allowed` — a `thinking` or
153 `redacted_thinking` block that failed the conversation check (the conversation
154 before it differs from the one it was created in, or it carries no record of one
155 on a model that requires it) and was shown to the model all the same, because
156 that check is not enforced for this request. More entry types may be added over
157 time; ignore types you do not recognize.
158
159 Requires `anthropic-beta: thinking-binding-controls-2026-08-01`. Present on
160 every such response from a model that supports extended thinking, as `[]` when
161 there is no entry to report; without the beta, blocks are removed or left in
162 place all the same but nothing is reported. Removed blocks contribute nothing to
163 `usage.input_tokens`; blocks left in place count as sent. When streaming, the
164 array is final in `message_start`; the final `message_delta` event carries it
165 only when a server-side model fallback happened mid-stream, in which case it
166 holds the serving model's entries and replaces the one in `message_start`.
167 """
168
169 def to_param(self) -> BetaMessageParam:
170 """Convert this response BetaMessage to a BetaMessageParam so it can be passed back
171 to the API as an input message, e.g. in the `messages` param.
172 """
173 return BetaMessageParam(role=self.role, content=list(self.content))