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