Debian Codegraph @

src/anthropic/types/message_create_params.py

raw
1 from __future__ import annotations
2
3 from typing import Union, Iterable, Optional
4 from typing_extensions import Literal, Required, TypeAlias, TypedDict
5
6 from .._types import SequenceNotStr
7 from .model_param import ModelParam
8 from .message_param import MessageParam
9 from .metadata_param import MetadataParam
10 from .text_block_param import TextBlockParam
11 from .tool_union_param import ToolUnionParam
12 from .diagnostics_param import DiagnosticsParam
13 from .tool_choice_param import ToolChoiceParam
14 from .output_config_param import OutputConfigParam
15 from .thinking_config_param import ThinkingConfigParam
16 from .tool_choice_any_param import ToolChoiceAnyParam
17 from .tool_choice_auto_param import ToolChoiceAutoParam
18 from .tool_choice_tool_param import ToolChoiceToolParam
19 from .cache_control_ephemeral_param import CacheControlEphemeralParam
20 from .message_create_params_container_param import MessageCreateParamsContainerParam
21
22 __all__ = [
23 "MessageCreateParamsBase",
24 "Metadata",
25 "ToolChoice",
26 "ToolChoiceToolChoiceAuto",
27 "ToolChoiceToolChoiceAny",
28 "ToolChoiceToolChoiceTool",
29 "MessageCreateParamsNonStreaming",
30 "MessageCreateParamsStreaming",
31 ]
32
33
34 class MessageCreateParamsBase(TypedDict, total=False):
35 max_tokens: Required[int]
36 """The maximum number of tokens to generate before stopping.
37
38 Note that our models may stop _before_ reaching this maximum. This parameter
39 only specifies the absolute maximum number of tokens to generate.
40
41 Set to `0` to populate the
42 [prompt cache](https://platform.claude.com/docs/en/build-with-claude/prompt-caching#pre-warming-the-cache)
43 without generating a response.
44
45 Different models have different maximum values for this parameter. See
46 [models](https://platform.claude.com/docs/en/about-claude/models/overview) for
47 details.
48 """
49
50 messages: Required[Iterable[MessageParam]]
51 """Input messages.
52
53 Our models are trained to operate on alternating `user` and `assistant`
54 conversational turns. When creating a new `Message`, you specify the prior
55 conversational turns with the `messages` parameter, and the model then generates
56 the next `Message` in the conversation. Consecutive `user` or `assistant` turns
57 in your request will be combined into a single turn.
58
59 Each input message must be an object with a `role` and `content`. You can
60 specify a single `user`-role message, or you can include multiple `user` and
61 `assistant` messages.
62
63 If the final message uses the `assistant` role, the response content will
64 continue immediately from the content in that message. This can be used to
65 constrain part of the model's response.
66
67 Example with a single `user` message:
68
69 ```json
70 [{ "role": "user", "content": "Hello, Claude" }]
71 ```
72
73 Example with multiple conversational turns:
74
75 ```json
76 [
77 { "role": "user", "content": "Hello there." },
78 { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" },
79 { "role": "user", "content": "Can you explain LLMs in plain English?" }
80 ]
81 ```
82
83 Example with a partially-filled response from Claude:
84
85 ```json
86 [
87 {
88 "role": "user",
89 "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
90 },
91 { "role": "assistant", "content": "The best answer is (" }
92 ]
93 ```
94
95 Each input message `content` may be either a single `string` or an array of
96 content blocks, where each block has a specific `type`. Using a `string` for
97 `content` is shorthand for an array of one content block of type `"text"`. The
98 following input messages are equivalent:
99
100 ```json
101 { "role": "user", "content": "Hello, Claude" }
102 ```
103
104 ```json
105 { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] }
106 ```
107
108 See
109 [input examples](https://platform.claude.com/docs/en/build-with-claude/working-with-messages).
110
111 Note that if you want to include a
112 [system prompt](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices#give-claude-a-role),
113 you can use the top-level `system` parameter — there is no `"system"` role for
114 input messages in the Messages API.
115
116 There is a limit of 100,000 messages in a single request.
117 """
118
119 model: Required[ModelParam]
120 """The model that will complete your prompt.
121
122 See [models](https://docs.anthropic.com/en/docs/models-overview) for additional
123 details and options.
124 """
125
126 cache_control: Optional[CacheControlEphemeralParam]
127 """
128 Top-level cache control automatically applies a cache_control marker to the last
129 cacheable block in the request.
130 """
131
132 container: Optional[MessageCreateParamsContainerParam]
133 """Container identifier for reuse across requests."""
134
135 diagnostics: Optional[DiagnosticsParam]
136 """Request-level diagnostics.
137
138 Supply `previous_message_id` to have the response include
139 `diagnostics.cache_miss_reason` explaining any prompt-cache divergence from that
140 prior request.
141 """
142
143 inference_geo: Optional[str]
144 """Specifies the geographic region for inference processing.
145
146 If not specified, the workspace's `default_inference_geo` is used.
147 """
148
149 metadata: MetadataParam
150 """An object describing metadata about the request."""
151
152 output_config: OutputConfigParam
153 """Configuration options for the model's output, such as the output format."""
154
155 service_tier: Literal["auto", "standard_only"]
156 """
157 Determines whether to use priority capacity (if available) or standard capacity
158 for this request.
159
160 Anthropic offers different levels of service for your API requests. See
161 [service-tiers](https://platform.claude.com/docs/en/api/service-tiers) for
162 details.
163 """
164
165 stop_sequences: SequenceNotStr[str]
166 """Custom text sequences that will cause the model to stop generating.
167
168 Our models will normally stop when they have naturally completed their turn,
169 which will result in a response `stop_reason` of `"end_turn"`.
170
171 If you want the model to stop generating when it encounters custom strings of
172 text, you can use the `stop_sequences` parameter. If the model encounters one of
173 the custom sequences, the response `stop_reason` value will be `"stop_sequence"`
174 and the response `stop_sequence` value will contain the matched stop sequence.
175 """
176
177 system: Union[str, Iterable[TextBlockParam]]
178 """System prompt.
179
180 A system prompt is a way of providing context and instructions to Claude, such
181 as specifying a particular goal or role. See our
182 [guide to system prompts](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices#give-claude-a-role).
183 """
184
185 thinking: ThinkingConfigParam
186 """Configuration for enabling Claude's extended thinking.
187
188 When enabled, responses include `thinking` content blocks showing Claude's
189 thinking process before the final answer. Requires a minimum budget of 1,024
190 tokens and counts towards your `max_tokens` limit.
191
192 See
193 [extended thinking](https://platform.claude.com/docs/en/build-with-claude/extended-thinking)
194 for details.
195 """
196
197 tool_choice: ToolChoiceParam
198 """How the model should use the provided tools.
199
200 The model can use a specific tool, any available tool, decide by itself, or not
201 use tools at all.
202 """
203
204 tools: Iterable[ToolUnionParam]
205 """Definitions of tools that the model may use.
206
207 If you include `tools` in your API request, the model may return `tool_use`
208 content blocks that represent the model's use of those tools. You can then run
209 those tools using the tool input generated by the model and then optionally
210 return results back to the model using `tool_result` content blocks.
211
212 There are two types of tools: **client tools** and **server tools**. The
213 behavior described below applies to client tools. For
214 [server tools](https://platform.claude.com/docs/en/agents-and-tools/tool-use/server-tools),
215 see their individual documentation as each has its own behavior (e.g., the
216 [web search tool](https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-search-tool)).
217
218 Each tool definition includes:
219
220 - `name`: Name of the tool.
221 - `description`: Optional, but strongly-recommended description of the tool.
222 - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the
223 tool `input` shape that the model will produce in `tool_use` output content
224 blocks.
225
226 For example, if you defined `tools` as:
227
228 ```json
229 [
230 {
231 "name": "get_stock_price",
232 "description": "Get the current stock price for a given ticker symbol.",
233 "input_schema": {
234 "type": "object",
235 "properties": {
236 "ticker": {
237 "type": "string",
238 "description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
239 }
240 },
241 "required": ["ticker"]
242 }
243 }
244 ]
245 ```
246
247 And then asked the model "What's the S&P 500 at today?", the model might produce
248 `tool_use` content blocks in the response like this:
249
250 ```json
251 [
252 {
253 "type": "tool_use",
254 "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV",
255 "name": "get_stock_price",
256 "input": { "ticker": "^GSPC" }
257 }
258 ]
259 ```
260
261 You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an
262 input, and return the following back to the model in a subsequent `user`
263 message:
264
265 ```json
266 [
267 {
268 "type": "tool_result",
269 "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV",
270 "content": "259.75 USD"
271 }
272 ]
273 ```
274
275 Tools can be used for workflows that include running client-side tools and
276 functions, or more generally whenever you want the model to produce a particular
277 JSON structure of output.
278
279 See our
280 [guide](https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview)
281 for more details.
282 """
283
284 user_profile_id: str
285 """The user profile ID to attribute this request to.
286
287 Use when acting on behalf of a party other than your organization. Requires the
288 `user-profiles` beta header.
289 """
290
291 workspace_id: str
292 """Optional header to select the Workspace for this request.
293
294 The value is a Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`).
295
296 Only needed for credentials that can act on more than one Workspace. A
297 credential that belongs to a specific Workspace may omit it; if sent, it must
298 match that Workspace.
299 """
300
301
302 Metadata: TypeAlias = MetadataParam
303 """This is deprecated, `MetadataParam` should be used instead"""
304
305 ToolChoice: TypeAlias = ToolChoiceParam
306 """This is deprecated, `ToolChoiceParam` should be used instead"""
307
308 ToolChoiceToolChoiceAuto: TypeAlias = ToolChoiceAutoParam
309 """This is deprecated, `ToolChoiceAutoParam` should be used instead"""
310
311 ToolChoiceToolChoiceAny: TypeAlias = ToolChoiceAnyParam
312 """This is deprecated, `ToolChoiceAnyParam` should be used instead"""
313
314 ToolChoiceToolChoiceTool: TypeAlias = ToolChoiceToolParam
315 """This is deprecated, `ToolChoiceToolParam` should be used instead"""
316
317
318 class MessageCreateParamsNonStreaming(MessageCreateParamsBase, total=False):
319 stream: Literal[False]
320 """Whether to incrementally stream the response using server-sent events.
321
322 When `true`, SDKs return a raw event stream.
323
324 In the TypeScript, Python and Ruby SDKs, the recommended way to stream is
325 `messages.stream()`. It sets `stream` for you and accumulates the events into
326 the final message. See
327 [Streaming with SDKs](https://platform.claude.com/docs/en/build-with-claude/streaming#streaming-with-sdks)
328 for an example in each language.
329 """
330
331
332 class MessageCreateParamsStreaming(MessageCreateParamsBase):
333 stream: Required[Literal[True]]
334 """Whether to incrementally stream the response using server-sent events.
335
336 When `true`, SDKs return a raw event stream.
337
338 In the TypeScript, Python and Ruby SDKs, the recommended way to stream is
339 `messages.stream()`. It sets `stream` for you and accumulates the events into
340 the final message. See
341 [Streaming with SDKs](https://platform.claude.com/docs/en/build-with-claude/streaming#streaming-with-sdks)
342 for an example in each language.
343 """
344
345
346 MessageCreateParams = Union[MessageCreateParamsNonStreaming, MessageCreateParamsStreaming]