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