| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
from typing import Any, cast |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from anthropic import Anthropic, AsyncAnthropic |
| 9 |
from tests.utils import assert_matches_type |
| 10 |
from anthropic.types.beta import ( |
| 11 |
BetaMessage, |
| 12 |
BetaMessageTokensCount, |
| 13 |
) |
| 14 |
|
| 15 |
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 16 |
|
| 17 |
|
| 18 |
class TestMessages: |
| 19 |
parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 20 |
|
| 21 |
@parametrize |
| 22 |
def test_method_create_overload_1(self, client: Anthropic) -> None: |
| 23 |
message = client.beta.messages.create( |
| 24 |
max_tokens=1024, |
| 25 |
messages=[ |
| 26 |
{ |
| 27 |
"content": "Hello, world", |
| 28 |
"role": "user", |
| 29 |
} |
| 30 |
], |
| 31 |
model="claude-opus-5", |
| 32 |
) |
| 33 |
assert_matches_type(BetaMessage, message, path=["response"]) |
| 34 |
|
| 35 |
@parametrize |
| 36 |
def test_method_create_with_all_params_overload_1(self, client: Anthropic) -> None: |
| 37 |
message = client.beta.messages.create( |
| 38 |
max_tokens=1024, |
| 39 |
messages=[ |
| 40 |
{ |
| 41 |
"content": "Hello, world", |
| 42 |
"role": "user", |
| 43 |
"clear_at": "next_user_message", |
| 44 |
"output_config": {"effort": "low"}, |
| 45 |
} |
| 46 |
], |
| 47 |
model="claude-opus-5", |
| 48 |
cache_control={ |
| 49 |
"type": "ephemeral", |
| 50 |
"ttl": "5m", |
| 51 |
}, |
| 52 |
compaction={ |
| 53 |
"type": "summarize", |
| 54 |
"instructions": "instructions", |
| 55 |
}, |
| 56 |
container={ |
| 57 |
"id": "id", |
| 58 |
"skills": [ |
| 59 |
{ |
| 60 |
"skill_id": "pdf", |
| 61 |
"type": "anthropic", |
| 62 |
"version": "latest", |
| 63 |
} |
| 64 |
], |
| 65 |
}, |
| 66 |
context_management={ |
| 67 |
"edits": [ |
| 68 |
{ |
| 69 |
"type": "clear_tool_uses_20250919", |
| 70 |
"clear_at_least": { |
| 71 |
"type": "input_tokens", |
| 72 |
"value": 0, |
| 73 |
}, |
| 74 |
"clear_tool_inputs": True, |
| 75 |
"exclude_tools": ["string"], |
| 76 |
"keep": { |
| 77 |
"type": "tool_uses", |
| 78 |
"value": 0, |
| 79 |
}, |
| 80 |
"trigger": { |
| 81 |
"type": "input_tokens", |
| 82 |
"value": 1, |
| 83 |
}, |
| 84 |
} |
| 85 |
] |
| 86 |
}, |
| 87 |
diagnostics={"previous_message_id": "previous_message_id"}, |
| 88 |
fallback_credit_token="x", |
| 89 |
fallbacks="default", |
| 90 |
inference_geo="inference_geo", |
| 91 |
mcp_servers=[ |
| 92 |
{ |
| 93 |
"name": "name", |
| 94 |
"type": "url", |
| 95 |
"url": "url", |
| 96 |
"authorization_token": "authorization_token", |
| 97 |
"tool_configuration": { |
| 98 |
"allowed_tools": ["string"], |
| 99 |
"enabled": True, |
| 100 |
}, |
| 101 |
} |
| 102 |
], |
| 103 |
metadata={"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"}, |
| 104 |
output_config={ |
| 105 |
"effort": "low", |
| 106 |
"format": { |
| 107 |
"schema": {"foo": "bar"}, |
| 108 |
"type": "json_schema", |
| 109 |
}, |
| 110 |
"task_budget": { |
| 111 |
"total": 1024, |
| 112 |
"type": "tokens", |
| 113 |
"remaining": 0, |
| 114 |
}, |
| 115 |
}, |
| 116 |
service_tier="auto", |
| 117 |
speed="standard", |
| 118 |
stop_sequences=["string"], |
| 119 |
stream=False, |
| 120 |
system=[ |
| 121 |
{ |
| 122 |
"text": "Today's date is 2024-06-01.", |
| 123 |
"type": "text", |
| 124 |
"cache_control": { |
| 125 |
"type": "ephemeral", |
| 126 |
"ttl": "5m", |
| 127 |
}, |
| 128 |
"citations": [ |
| 129 |
{ |
| 130 |
"cited_text": "The grass is green. The sky is blue.", |
| 131 |
"document_index": 0, |
| 132 |
"document_title": "x", |
| 133 |
"end_char_index": 0, |
| 134 |
"start_char_index": 0, |
| 135 |
"type": "char_location", |
| 136 |
} |
| 137 |
], |
| 138 |
} |
| 139 |
], |
| 140 |
thinking={ |
| 141 |
"type": "adaptive", |
| 142 |
"block_binding": {"prefix_mismatch_behavior": "error"}, |
| 143 |
"display": "summarized", |
| 144 |
}, |
| 145 |
tool_choice={ |
| 146 |
"type": "auto", |
| 147 |
"disable_parallel_tool_use": True, |
| 148 |
}, |
| 149 |
tools=[ |
| 150 |
{ |
| 151 |
"input_schema": { |
| 152 |
"type": "object", |
| 153 |
"properties": { |
| 154 |
"location": "bar", |
| 155 |
"unit": "bar", |
| 156 |
}, |
| 157 |
"required": ["location"], |
| 158 |
}, |
| 159 |
"name": "name", |
| 160 |
"allowed_callers": ["direct"], |
| 161 |
"cache_control": { |
| 162 |
"type": "ephemeral", |
| 163 |
"ttl": "5m", |
| 164 |
}, |
| 165 |
"defer_loading": True, |
| 166 |
"description": "Get the current weather in a given location", |
| 167 |
"eager_input_streaming": True, |
| 168 |
"input_examples": [{"foo": "bar"}], |
| 169 |
"strict": True, |
| 170 |
"type": "custom", |
| 171 |
} |
| 172 |
], |
| 173 |
betas=["message-batches-2024-09-24"], |
| 174 |
user_profile_id="anthropic-user-profile-id", |
| 175 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 176 |
) |
| 177 |
assert_matches_type(BetaMessage, message, path=["response"]) |
| 178 |
|
| 179 |
@parametrize |
| 180 |
def test_raw_response_create_overload_1(self, client: Anthropic) -> None: |
| 181 |
response = client.beta.messages.with_raw_response.create( |
| 182 |
max_tokens=1024, |
| 183 |
messages=[ |
| 184 |
{ |
| 185 |
"content": "Hello, world", |
| 186 |
"role": "user", |
| 187 |
} |
| 188 |
], |
| 189 |
model="claude-opus-5", |
| 190 |
) |
| 191 |
|
| 192 |
assert response.is_closed is True |
| 193 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 194 |
message = response.parse() |
| 195 |
assert_matches_type(BetaMessage, message, path=["response"]) |
| 196 |
|
| 197 |
@parametrize |
| 198 |
def test_streaming_response_create_overload_1(self, client: Anthropic) -> None: |
| 199 |
with client.beta.messages.with_streaming_response.create( |
| 200 |
max_tokens=1024, |
| 201 |
messages=[ |
| 202 |
{ |
| 203 |
"content": "Hello, world", |
| 204 |
"role": "user", |
| 205 |
} |
| 206 |
], |
| 207 |
model="claude-opus-5", |
| 208 |
) as response: |
| 209 |
assert not response.is_closed |
| 210 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 211 |
|
| 212 |
message = response.parse() |
| 213 |
assert_matches_type(BetaMessage, message, path=["response"]) |
| 214 |
|
| 215 |
assert cast(Any, response.is_closed) is True |
| 216 |
|
| 217 |
@parametrize |
| 218 |
def test_method_create_overload_2(self, client: Anthropic) -> None: |
| 219 |
message_stream = client.beta.messages.create( |
| 220 |
max_tokens=1024, |
| 221 |
messages=[ |
| 222 |
{ |
| 223 |
"content": "Hello, world", |
| 224 |
"role": "user", |
| 225 |
} |
| 226 |
], |
| 227 |
model="claude-opus-5", |
| 228 |
stream=True, |
| 229 |
) |
| 230 |
message_stream.response.close() |
| 231 |
|
| 232 |
@parametrize |
| 233 |
def test_method_create_with_all_params_overload_2(self, client: Anthropic) -> None: |
| 234 |
message_stream = client.beta.messages.create( |
| 235 |
max_tokens=1024, |
| 236 |
messages=[ |
| 237 |
{ |
| 238 |
"content": "Hello, world", |
| 239 |
"role": "user", |
| 240 |
"clear_at": "next_user_message", |
| 241 |
"output_config": {"effort": "low"}, |
| 242 |
} |
| 243 |
], |
| 244 |
model="claude-opus-5", |
| 245 |
stream=True, |
| 246 |
cache_control={ |
| 247 |
"type": "ephemeral", |
| 248 |
"ttl": "5m", |
| 249 |
}, |
| 250 |
compaction={ |
| 251 |
"type": "summarize", |
| 252 |
"instructions": "instructions", |
| 253 |
}, |
| 254 |
container={ |
| 255 |
"id": "id", |
| 256 |
"skills": [ |
| 257 |
{ |
| 258 |
"skill_id": "pdf", |
| 259 |
"type": "anthropic", |
| 260 |
"version": "latest", |
| 261 |
} |
| 262 |
], |
| 263 |
}, |
| 264 |
context_management={ |
| 265 |
"edits": [ |
| 266 |
{ |
| 267 |
"type": "clear_tool_uses_20250919", |
| 268 |
"clear_at_least": { |
| 269 |
"type": "input_tokens", |
| 270 |
"value": 0, |
| 271 |
}, |
| 272 |
"clear_tool_inputs": True, |
| 273 |
"exclude_tools": ["string"], |
| 274 |
"keep": { |
| 275 |
"type": "tool_uses", |
| 276 |
"value": 0, |
| 277 |
}, |
| 278 |
"trigger": { |
| 279 |
"type": "input_tokens", |
| 280 |
"value": 1, |
| 281 |
}, |
| 282 |
} |
| 283 |
] |
| 284 |
}, |
| 285 |
diagnostics={"previous_message_id": "previous_message_id"}, |
| 286 |
fallback_credit_token="x", |
| 287 |
fallbacks="default", |
| 288 |
inference_geo="inference_geo", |
| 289 |
mcp_servers=[ |
| 290 |
{ |
| 291 |
"name": "name", |
| 292 |
"type": "url", |
| 293 |
"url": "url", |
| 294 |
"authorization_token": "authorization_token", |
| 295 |
"tool_configuration": { |
| 296 |
"allowed_tools": ["string"], |
| 297 |
"enabled": True, |
| 298 |
}, |
| 299 |
} |
| 300 |
], |
| 301 |
metadata={"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"}, |
| 302 |
output_config={ |
| 303 |
"effort": "low", |
| 304 |
"format": { |
| 305 |
"schema": {"foo": "bar"}, |
| 306 |
"type": "json_schema", |
| 307 |
}, |
| 308 |
"task_budget": { |
| 309 |
"total": 1024, |
| 310 |
"type": "tokens", |
| 311 |
"remaining": 0, |
| 312 |
}, |
| 313 |
}, |
| 314 |
service_tier="auto", |
| 315 |
speed="standard", |
| 316 |
stop_sequences=["string"], |
| 317 |
system=[ |
| 318 |
{ |
| 319 |
"text": "Today's date is 2024-06-01.", |
| 320 |
"type": "text", |
| 321 |
"cache_control": { |
| 322 |
"type": "ephemeral", |
| 323 |
"ttl": "5m", |
| 324 |
}, |
| 325 |
"citations": [ |
| 326 |
{ |
| 327 |
"cited_text": "The grass is green. The sky is blue.", |
| 328 |
"document_index": 0, |
| 329 |
"document_title": "x", |
| 330 |
"end_char_index": 0, |
| 331 |
"start_char_index": 0, |
| 332 |
"type": "char_location", |
| 333 |
} |
| 334 |
], |
| 335 |
} |
| 336 |
], |
| 337 |
thinking={ |
| 338 |
"type": "adaptive", |
| 339 |
"block_binding": {"prefix_mismatch_behavior": "error"}, |
| 340 |
"display": "summarized", |
| 341 |
}, |
| 342 |
tool_choice={ |
| 343 |
"type": "auto", |
| 344 |
"disable_parallel_tool_use": True, |
| 345 |
}, |
| 346 |
tools=[ |
| 347 |
{ |
| 348 |
"input_schema": { |
| 349 |
"type": "object", |
| 350 |
"properties": { |
| 351 |
"location": "bar", |
| 352 |
"unit": "bar", |
| 353 |
}, |
| 354 |
"required": ["location"], |
| 355 |
}, |
| 356 |
"name": "name", |
| 357 |
"allowed_callers": ["direct"], |
| 358 |
"cache_control": { |
| 359 |
"type": "ephemeral", |
| 360 |
"ttl": "5m", |
| 361 |
}, |
| 362 |
"defer_loading": True, |
| 363 |
"description": "Get the current weather in a given location", |
| 364 |
"eager_input_streaming": True, |
| 365 |
"input_examples": [{"foo": "bar"}], |
| 366 |
"strict": True, |
| 367 |
"type": "custom", |
| 368 |
} |
| 369 |
], |
| 370 |
betas=["message-batches-2024-09-24"], |
| 371 |
user_profile_id="anthropic-user-profile-id", |
| 372 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 373 |
) |
| 374 |
message_stream.response.close() |
| 375 |
|
| 376 |
@parametrize |
| 377 |
def test_raw_response_create_overload_2(self, client: Anthropic) -> None: |
| 378 |
response = client.beta.messages.with_raw_response.create( |
| 379 |
max_tokens=1024, |
| 380 |
messages=[ |
| 381 |
{ |
| 382 |
"content": "Hello, world", |
| 383 |
"role": "user", |
| 384 |
} |
| 385 |
], |
| 386 |
model="claude-opus-5", |
| 387 |
stream=True, |
| 388 |
) |
| 389 |
|
| 390 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 391 |
stream = response.parse() |
| 392 |
stream.close() |
| 393 |
|
| 394 |
@parametrize |
| 395 |
def test_streaming_response_create_overload_2(self, client: Anthropic) -> None: |
| 396 |
with client.beta.messages.with_streaming_response.create( |
| 397 |
max_tokens=1024, |
| 398 |
messages=[ |
| 399 |
{ |
| 400 |
"content": "Hello, world", |
| 401 |
"role": "user", |
| 402 |
} |
| 403 |
], |
| 404 |
model="claude-opus-5", |
| 405 |
stream=True, |
| 406 |
) as response: |
| 407 |
assert not response.is_closed |
| 408 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 409 |
|
| 410 |
stream = response.parse() |
| 411 |
stream.close() |
| 412 |
|
| 413 |
assert cast(Any, response.is_closed) is True |
| 414 |
|
| 415 |
@parametrize |
| 416 |
def test_method_count_tokens(self, client: Anthropic) -> None: |
| 417 |
message = client.beta.messages.count_tokens( |
| 418 |
messages=[ |
| 419 |
{ |
| 420 |
"content": "Hello, world", |
| 421 |
"role": "user", |
| 422 |
} |
| 423 |
], |
| 424 |
model="claude-opus-5", |
| 425 |
) |
| 426 |
assert_matches_type(BetaMessageTokensCount, message, path=["response"]) |
| 427 |
|
| 428 |
@parametrize |
| 429 |
def test_method_count_tokens_with_all_params(self, client: Anthropic) -> None: |
| 430 |
message = client.beta.messages.count_tokens( |
| 431 |
messages=[ |
| 432 |
{ |
| 433 |
"content": "Hello, world", |
| 434 |
"role": "user", |
| 435 |
"clear_at": "next_user_message", |
| 436 |
"output_config": {"effort": "low"}, |
| 437 |
} |
| 438 |
], |
| 439 |
model="claude-opus-5", |
| 440 |
cache_control={ |
| 441 |
"type": "ephemeral", |
| 442 |
"ttl": "5m", |
| 443 |
}, |
| 444 |
compaction={ |
| 445 |
"type": "summarize", |
| 446 |
"instructions": "instructions", |
| 447 |
}, |
| 448 |
context_management={ |
| 449 |
"edits": [ |
| 450 |
{ |
| 451 |
"type": "clear_tool_uses_20250919", |
| 452 |
"clear_at_least": { |
| 453 |
"type": "input_tokens", |
| 454 |
"value": 0, |
| 455 |
}, |
| 456 |
"clear_tool_inputs": True, |
| 457 |
"exclude_tools": ["string"], |
| 458 |
"keep": { |
| 459 |
"type": "tool_uses", |
| 460 |
"value": 0, |
| 461 |
}, |
| 462 |
"trigger": { |
| 463 |
"type": "input_tokens", |
| 464 |
"value": 1, |
| 465 |
}, |
| 466 |
} |
| 467 |
] |
| 468 |
}, |
| 469 |
mcp_servers=[ |
| 470 |
{ |
| 471 |
"name": "name", |
| 472 |
"type": "url", |
| 473 |
"url": "url", |
| 474 |
"authorization_token": "authorization_token", |
| 475 |
"tool_configuration": { |
| 476 |
"allowed_tools": ["string"], |
| 477 |
"enabled": True, |
| 478 |
}, |
| 479 |
} |
| 480 |
], |
| 481 |
output_config={ |
| 482 |
"effort": "low", |
| 483 |
"format": { |
| 484 |
"schema": {"foo": "bar"}, |
| 485 |
"type": "json_schema", |
| 486 |
}, |
| 487 |
"task_budget": { |
| 488 |
"total": 1024, |
| 489 |
"type": "tokens", |
| 490 |
"remaining": 0, |
| 491 |
}, |
| 492 |
}, |
| 493 |
speed="fast", |
| 494 |
system=[ |
| 495 |
{ |
| 496 |
"text": "Today's date is 2024-06-01.", |
| 497 |
"type": "text", |
| 498 |
"cache_control": { |
| 499 |
"type": "ephemeral", |
| 500 |
"ttl": "5m", |
| 501 |
}, |
| 502 |
"citations": [ |
| 503 |
{ |
| 504 |
"cited_text": "The grass is green. The sky is blue.", |
| 505 |
"document_index": 0, |
| 506 |
"document_title": "x", |
| 507 |
"end_char_index": 0, |
| 508 |
"start_char_index": 0, |
| 509 |
"type": "char_location", |
| 510 |
} |
| 511 |
], |
| 512 |
} |
| 513 |
], |
| 514 |
thinking={ |
| 515 |
"type": "adaptive", |
| 516 |
"block_binding": {"prefix_mismatch_behavior": "error"}, |
| 517 |
"display": "summarized", |
| 518 |
}, |
| 519 |
tool_choice={ |
| 520 |
"type": "auto", |
| 521 |
"disable_parallel_tool_use": True, |
| 522 |
}, |
| 523 |
tools=[ |
| 524 |
{ |
| 525 |
"input_schema": { |
| 526 |
"type": "object", |
| 527 |
"properties": { |
| 528 |
"location": "bar", |
| 529 |
"unit": "bar", |
| 530 |
}, |
| 531 |
"required": ["location"], |
| 532 |
}, |
| 533 |
"name": "name", |
| 534 |
"allowed_callers": ["direct"], |
| 535 |
"cache_control": { |
| 536 |
"type": "ephemeral", |
| 537 |
"ttl": "5m", |
| 538 |
}, |
| 539 |
"defer_loading": True, |
| 540 |
"description": "Get the current weather in a given location", |
| 541 |
"eager_input_streaming": True, |
| 542 |
"input_examples": [{"foo": "bar"}], |
| 543 |
"strict": True, |
| 544 |
"type": "custom", |
| 545 |
} |
| 546 |
], |
| 547 |
betas=["string"], |
| 548 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 549 |
) |
| 550 |
assert_matches_type(BetaMessageTokensCount, message, path=["response"]) |
| 551 |
|
| 552 |
@parametrize |
| 553 |
def test_raw_response_count_tokens(self, client: Anthropic) -> None: |
| 554 |
response = client.beta.messages.with_raw_response.count_tokens( |
| 555 |
messages=[ |
| 556 |
{ |
| 557 |
"content": "Hello, world", |
| 558 |
"role": "user", |
| 559 |
} |
| 560 |
], |
| 561 |
model="claude-opus-5", |
| 562 |
) |
| 563 |
|
| 564 |
assert response.is_closed is True |
| 565 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 566 |
message = response.parse() |
| 567 |
assert_matches_type(BetaMessageTokensCount, message, path=["response"]) |
| 568 |
|
| 569 |
@parametrize |
| 570 |
def test_streaming_response_count_tokens(self, client: Anthropic) -> None: |
| 571 |
with client.beta.messages.with_streaming_response.count_tokens( |
| 572 |
messages=[ |
| 573 |
{ |
| 574 |
"content": "Hello, world", |
| 575 |
"role": "user", |
| 576 |
} |
| 577 |
], |
| 578 |
model="claude-opus-5", |
| 579 |
) as response: |
| 580 |
assert not response.is_closed |
| 581 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 582 |
|
| 583 |
message = response.parse() |
| 584 |
assert_matches_type(BetaMessageTokensCount, message, path=["response"]) |
| 585 |
|
| 586 |
assert cast(Any, response.is_closed) is True |
| 587 |
|
| 588 |
|
| 589 |
class TestAsyncMessages: |
| 590 |
parametrize = pytest.mark.parametrize( |
| 591 |
"async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"] |
| 592 |
) |
| 593 |
|
| 594 |
@parametrize |
| 595 |
async def test_method_create_overload_1(self, async_client: AsyncAnthropic) -> None: |
| 596 |
message = await async_client.beta.messages.create( |
| 597 |
max_tokens=1024, |
| 598 |
messages=[ |
| 599 |
{ |
| 600 |
"content": "Hello, world", |
| 601 |
"role": "user", |
| 602 |
} |
| 603 |
], |
| 604 |
model="claude-opus-5", |
| 605 |
) |
| 606 |
assert_matches_type(BetaMessage, message, path=["response"]) |
| 607 |
|
| 608 |
@parametrize |
| 609 |
async def test_method_create_with_all_params_overload_1(self, async_client: AsyncAnthropic) -> None: |
| 610 |
message = await async_client.beta.messages.create( |
| 611 |
max_tokens=1024, |
| 612 |
messages=[ |
| 613 |
{ |
| 614 |
"content": "Hello, world", |
| 615 |
"role": "user", |
| 616 |
"clear_at": "next_user_message", |
| 617 |
"output_config": {"effort": "low"}, |
| 618 |
} |
| 619 |
], |
| 620 |
model="claude-opus-5", |
| 621 |
cache_control={ |
| 622 |
"type": "ephemeral", |
| 623 |
"ttl": "5m", |
| 624 |
}, |
| 625 |
compaction={ |
| 626 |
"type": "summarize", |
| 627 |
"instructions": "instructions", |
| 628 |
}, |
| 629 |
container={ |
| 630 |
"id": "id", |
| 631 |
"skills": [ |
| 632 |
{ |
| 633 |
"skill_id": "pdf", |
| 634 |
"type": "anthropic", |
| 635 |
"version": "latest", |
| 636 |
} |
| 637 |
], |
| 638 |
}, |
| 639 |
context_management={ |
| 640 |
"edits": [ |
| 641 |
{ |
| 642 |
"type": "clear_tool_uses_20250919", |
| 643 |
"clear_at_least": { |
| 644 |
"type": "input_tokens", |
| 645 |
"value": 0, |
| 646 |
}, |
| 647 |
"clear_tool_inputs": True, |
| 648 |
"exclude_tools": ["string"], |
| 649 |
"keep": { |
| 650 |
"type": "tool_uses", |
| 651 |
"value": 0, |
| 652 |
}, |
| 653 |
"trigger": { |
| 654 |
"type": "input_tokens", |
| 655 |
"value": 1, |
| 656 |
}, |
| 657 |
} |
| 658 |
] |
| 659 |
}, |
| 660 |
diagnostics={"previous_message_id": "previous_message_id"}, |
| 661 |
fallback_credit_token="x", |
| 662 |
fallbacks="default", |
| 663 |
inference_geo="inference_geo", |
| 664 |
mcp_servers=[ |
| 665 |
{ |
| 666 |
"name": "name", |
| 667 |
"type": "url", |
| 668 |
"url": "url", |
| 669 |
"authorization_token": "authorization_token", |
| 670 |
"tool_configuration": { |
| 671 |
"allowed_tools": ["string"], |
| 672 |
"enabled": True, |
| 673 |
}, |
| 674 |
} |
| 675 |
], |
| 676 |
metadata={"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"}, |
| 677 |
output_config={ |
| 678 |
"effort": "low", |
| 679 |
"format": { |
| 680 |
"schema": {"foo": "bar"}, |
| 681 |
"type": "json_schema", |
| 682 |
}, |
| 683 |
"task_budget": { |
| 684 |
"total": 1024, |
| 685 |
"type": "tokens", |
| 686 |
"remaining": 0, |
| 687 |
}, |
| 688 |
}, |
| 689 |
service_tier="auto", |
| 690 |
speed="standard", |
| 691 |
stop_sequences=["string"], |
| 692 |
stream=False, |
| 693 |
system=[ |
| 694 |
{ |
| 695 |
"text": "Today's date is 2024-06-01.", |
| 696 |
"type": "text", |
| 697 |
"cache_control": { |
| 698 |
"type": "ephemeral", |
| 699 |
"ttl": "5m", |
| 700 |
}, |
| 701 |
"citations": [ |
| 702 |
{ |
| 703 |
"cited_text": "The grass is green. The sky is blue.", |
| 704 |
"document_index": 0, |
| 705 |
"document_title": "x", |
| 706 |
"end_char_index": 0, |
| 707 |
"start_char_index": 0, |
| 708 |
"type": "char_location", |
| 709 |
} |
| 710 |
], |
| 711 |
} |
| 712 |
], |
| 713 |
thinking={ |
| 714 |
"type": "adaptive", |
| 715 |
"block_binding": {"prefix_mismatch_behavior": "error"}, |
| 716 |
"display": "summarized", |
| 717 |
}, |
| 718 |
tool_choice={ |
| 719 |
"type": "auto", |
| 720 |
"disable_parallel_tool_use": True, |
| 721 |
}, |
| 722 |
tools=[ |
| 723 |
{ |
| 724 |
"input_schema": { |
| 725 |
"type": "object", |
| 726 |
"properties": { |
| 727 |
"location": "bar", |
| 728 |
"unit": "bar", |
| 729 |
}, |
| 730 |
"required": ["location"], |
| 731 |
}, |
| 732 |
"name": "name", |
| 733 |
"allowed_callers": ["direct"], |
| 734 |
"cache_control": { |
| 735 |
"type": "ephemeral", |
| 736 |
"ttl": "5m", |
| 737 |
}, |
| 738 |
"defer_loading": True, |
| 739 |
"description": "Get the current weather in a given location", |
| 740 |
"eager_input_streaming": True, |
| 741 |
"input_examples": [{"foo": "bar"}], |
| 742 |
"strict": True, |
| 743 |
"type": "custom", |
| 744 |
} |
| 745 |
], |
| 746 |
betas=["message-batches-2024-09-24"], |
| 747 |
user_profile_id="anthropic-user-profile-id", |
| 748 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 749 |
) |
| 750 |
assert_matches_type(BetaMessage, message, path=["response"]) |
| 751 |
|
| 752 |
@parametrize |
| 753 |
async def test_raw_response_create_overload_1(self, async_client: AsyncAnthropic) -> None: |
| 754 |
response = await async_client.beta.messages.with_raw_response.create( |
| 755 |
max_tokens=1024, |
| 756 |
messages=[ |
| 757 |
{ |
| 758 |
"content": "Hello, world", |
| 759 |
"role": "user", |
| 760 |
} |
| 761 |
], |
| 762 |
model="claude-opus-5", |
| 763 |
) |
| 764 |
|
| 765 |
assert response.is_closed is True |
| 766 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 767 |
message = await response.parse() |
| 768 |
assert_matches_type(BetaMessage, message, path=["response"]) |
| 769 |
|
| 770 |
@parametrize |
| 771 |
async def test_streaming_response_create_overload_1(self, async_client: AsyncAnthropic) -> None: |
| 772 |
async with async_client.beta.messages.with_streaming_response.create( |
| 773 |
max_tokens=1024, |
| 774 |
messages=[ |
| 775 |
{ |
| 776 |
"content": "Hello, world", |
| 777 |
"role": "user", |
| 778 |
} |
| 779 |
], |
| 780 |
model="claude-opus-5", |
| 781 |
) as response: |
| 782 |
assert not response.is_closed |
| 783 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 784 |
|
| 785 |
message = await response.parse() |
| 786 |
assert_matches_type(BetaMessage, message, path=["response"]) |
| 787 |
|
| 788 |
assert cast(Any, response.is_closed) is True |
| 789 |
|
| 790 |
@parametrize |
| 791 |
async def test_method_create_overload_2(self, async_client: AsyncAnthropic) -> None: |
| 792 |
message_stream = await async_client.beta.messages.create( |
| 793 |
max_tokens=1024, |
| 794 |
messages=[ |
| 795 |
{ |
| 796 |
"content": "Hello, world", |
| 797 |
"role": "user", |
| 798 |
} |
| 799 |
], |
| 800 |
model="claude-opus-5", |
| 801 |
stream=True, |
| 802 |
) |
| 803 |
await message_stream.response.aclose() |
| 804 |
|
| 805 |
@parametrize |
| 806 |
async def test_method_create_with_all_params_overload_2(self, async_client: AsyncAnthropic) -> None: |
| 807 |
message_stream = await async_client.beta.messages.create( |
| 808 |
max_tokens=1024, |
| 809 |
messages=[ |
| 810 |
{ |
| 811 |
"content": "Hello, world", |
| 812 |
"role": "user", |
| 813 |
"clear_at": "next_user_message", |
| 814 |
"output_config": {"effort": "low"}, |
| 815 |
} |
| 816 |
], |
| 817 |
model="claude-opus-5", |
| 818 |
stream=True, |
| 819 |
cache_control={ |
| 820 |
"type": "ephemeral", |
| 821 |
"ttl": "5m", |
| 822 |
}, |
| 823 |
compaction={ |
| 824 |
"type": "summarize", |
| 825 |
"instructions": "instructions", |
| 826 |
}, |
| 827 |
container={ |
| 828 |
"id": "id", |
| 829 |
"skills": [ |
| 830 |
{ |
| 831 |
"skill_id": "pdf", |
| 832 |
"type": "anthropic", |
| 833 |
"version": "latest", |
| 834 |
} |
| 835 |
], |
| 836 |
}, |
| 837 |
context_management={ |
| 838 |
"edits": [ |
| 839 |
{ |
| 840 |
"type": "clear_tool_uses_20250919", |
| 841 |
"clear_at_least": { |
| 842 |
"type": "input_tokens", |
| 843 |
"value": 0, |
| 844 |
}, |
| 845 |
"clear_tool_inputs": True, |
| 846 |
"exclude_tools": ["string"], |
| 847 |
"keep": { |
| 848 |
"type": "tool_uses", |
| 849 |
"value": 0, |
| 850 |
}, |
| 851 |
"trigger": { |
| 852 |
"type": "input_tokens", |
| 853 |
"value": 1, |
| 854 |
}, |
| 855 |
} |
| 856 |
] |
| 857 |
}, |
| 858 |
diagnostics={"previous_message_id": "previous_message_id"}, |
| 859 |
fallback_credit_token="x", |
| 860 |
fallbacks="default", |
| 861 |
inference_geo="inference_geo", |
| 862 |
mcp_servers=[ |
| 863 |
{ |
| 864 |
"name": "name", |
| 865 |
"type": "url", |
| 866 |
"url": "url", |
| 867 |
"authorization_token": "authorization_token", |
| 868 |
"tool_configuration": { |
| 869 |
"allowed_tools": ["string"], |
| 870 |
"enabled": True, |
| 871 |
}, |
| 872 |
} |
| 873 |
], |
| 874 |
metadata={"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"}, |
| 875 |
output_config={ |
| 876 |
"effort": "low", |
| 877 |
"format": { |
| 878 |
"schema": {"foo": "bar"}, |
| 879 |
"type": "json_schema", |
| 880 |
}, |
| 881 |
"task_budget": { |
| 882 |
"total": 1024, |
| 883 |
"type": "tokens", |
| 884 |
"remaining": 0, |
| 885 |
}, |
| 886 |
}, |
| 887 |
service_tier="auto", |
| 888 |
speed="standard", |
| 889 |
stop_sequences=["string"], |
| 890 |
system=[ |
| 891 |
{ |
| 892 |
"text": "Today's date is 2024-06-01.", |
| 893 |
"type": "text", |
| 894 |
"cache_control": { |
| 895 |
"type": "ephemeral", |
| 896 |
"ttl": "5m", |
| 897 |
}, |
| 898 |
"citations": [ |
| 899 |
{ |
| 900 |
"cited_text": "The grass is green. The sky is blue.", |
| 901 |
"document_index": 0, |
| 902 |
"document_title": "x", |
| 903 |
"end_char_index": 0, |
| 904 |
"start_char_index": 0, |
| 905 |
"type": "char_location", |
| 906 |
} |
| 907 |
], |
| 908 |
} |
| 909 |
], |
| 910 |
thinking={ |
| 911 |
"type": "adaptive", |
| 912 |
"block_binding": {"prefix_mismatch_behavior": "error"}, |
| 913 |
"display": "summarized", |
| 914 |
}, |
| 915 |
tool_choice={ |
| 916 |
"type": "auto", |
| 917 |
"disable_parallel_tool_use": True, |
| 918 |
}, |
| 919 |
tools=[ |
| 920 |
{ |
| 921 |
"input_schema": { |
| 922 |
"type": "object", |
| 923 |
"properties": { |
| 924 |
"location": "bar", |
| 925 |
"unit": "bar", |
| 926 |
}, |
| 927 |
"required": ["location"], |
| 928 |
}, |
| 929 |
"name": "name", |
| 930 |
"allowed_callers": ["direct"], |
| 931 |
"cache_control": { |
| 932 |
"type": "ephemeral", |
| 933 |
"ttl": "5m", |
| 934 |
}, |
| 935 |
"defer_loading": True, |
| 936 |
"description": "Get the current weather in a given location", |
| 937 |
"eager_input_streaming": True, |
| 938 |
"input_examples": [{"foo": "bar"}], |
| 939 |
"strict": True, |
| 940 |
"type": "custom", |
| 941 |
} |
| 942 |
], |
| 943 |
betas=["message-batches-2024-09-24"], |
| 944 |
user_profile_id="anthropic-user-profile-id", |
| 945 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 946 |
) |
| 947 |
await message_stream.response.aclose() |
| 948 |
|
| 949 |
@parametrize |
| 950 |
async def test_raw_response_create_overload_2(self, async_client: AsyncAnthropic) -> None: |
| 951 |
response = await async_client.beta.messages.with_raw_response.create( |
| 952 |
max_tokens=1024, |
| 953 |
messages=[ |
| 954 |
{ |
| 955 |
"content": "Hello, world", |
| 956 |
"role": "user", |
| 957 |
} |
| 958 |
], |
| 959 |
model="claude-opus-5", |
| 960 |
stream=True, |
| 961 |
) |
| 962 |
|
| 963 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 964 |
stream = await response.parse() |
| 965 |
await stream.close() |
| 966 |
|
| 967 |
@parametrize |
| 968 |
async def test_streaming_response_create_overload_2(self, async_client: AsyncAnthropic) -> None: |
| 969 |
async with async_client.beta.messages.with_streaming_response.create( |
| 970 |
max_tokens=1024, |
| 971 |
messages=[ |
| 972 |
{ |
| 973 |
"content": "Hello, world", |
| 974 |
"role": "user", |
| 975 |
} |
| 976 |
], |
| 977 |
model="claude-opus-5", |
| 978 |
stream=True, |
| 979 |
) as response: |
| 980 |
assert not response.is_closed |
| 981 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 982 |
|
| 983 |
stream = await response.parse() |
| 984 |
await stream.close() |
| 985 |
|
| 986 |
assert cast(Any, response.is_closed) is True |
| 987 |
|
| 988 |
@parametrize |
| 989 |
async def test_method_count_tokens(self, async_client: AsyncAnthropic) -> None: |
| 990 |
message = await async_client.beta.messages.count_tokens( |
| 991 |
messages=[ |
| 992 |
{ |
| 993 |
"content": "Hello, world", |
| 994 |
"role": "user", |
| 995 |
} |
| 996 |
], |
| 997 |
model="claude-opus-5", |
| 998 |
) |
| 999 |
assert_matches_type(BetaMessageTokensCount, message, path=["response"]) |
| 1000 |
|
| 1001 |
@parametrize |
| 1002 |
async def test_method_count_tokens_with_all_params(self, async_client: AsyncAnthropic) -> None: |
| 1003 |
message = await async_client.beta.messages.count_tokens( |
| 1004 |
messages=[ |
| 1005 |
{ |
| 1006 |
"content": "Hello, world", |
| 1007 |
"role": "user", |
| 1008 |
"clear_at": "next_user_message", |
| 1009 |
"output_config": {"effort": "low"}, |
| 1010 |
} |
| 1011 |
], |
| 1012 |
model="claude-opus-5", |
| 1013 |
cache_control={ |
| 1014 |
"type": "ephemeral", |
| 1015 |
"ttl": "5m", |
| 1016 |
}, |
| 1017 |
compaction={ |
| 1018 |
"type": "summarize", |
| 1019 |
"instructions": "instructions", |
| 1020 |
}, |
| 1021 |
context_management={ |
| 1022 |
"edits": [ |
| 1023 |
{ |
| 1024 |
"type": "clear_tool_uses_20250919", |
| 1025 |
"clear_at_least": { |
| 1026 |
"type": "input_tokens", |
| 1027 |
"value": 0, |
| 1028 |
}, |
| 1029 |
"clear_tool_inputs": True, |
| 1030 |
"exclude_tools": ["string"], |
| 1031 |
"keep": { |
| 1032 |
"type": "tool_uses", |
| 1033 |
"value": 0, |
| 1034 |
}, |
| 1035 |
"trigger": { |
| 1036 |
"type": "input_tokens", |
| 1037 |
"value": 1, |
| 1038 |
}, |
| 1039 |
} |
| 1040 |
] |
| 1041 |
}, |
| 1042 |
mcp_servers=[ |
| 1043 |
{ |
| 1044 |
"name": "name", |
| 1045 |
"type": "url", |
| 1046 |
"url": "url", |
| 1047 |
"authorization_token": "authorization_token", |
| 1048 |
"tool_configuration": { |
| 1049 |
"allowed_tools": ["string"], |
| 1050 |
"enabled": True, |
| 1051 |
}, |
| 1052 |
} |
| 1053 |
], |
| 1054 |
output_config={ |
| 1055 |
"effort": "low", |
| 1056 |
"format": { |
| 1057 |
"schema": {"foo": "bar"}, |
| 1058 |
"type": "json_schema", |
| 1059 |
}, |
| 1060 |
"task_budget": { |
| 1061 |
"total": 1024, |
| 1062 |
"type": "tokens", |
| 1063 |
"remaining": 0, |
| 1064 |
}, |
| 1065 |
}, |
| 1066 |
speed="fast", |
| 1067 |
system=[ |
| 1068 |
{ |
| 1069 |
"text": "Today's date is 2024-06-01.", |
| 1070 |
"type": "text", |
| 1071 |
"cache_control": { |
| 1072 |
"type": "ephemeral", |
| 1073 |
"ttl": "5m", |
| 1074 |
}, |
| 1075 |
"citations": [ |
| 1076 |
{ |
| 1077 |
"cited_text": "The grass is green. The sky is blue.", |
| 1078 |
"document_index": 0, |
| 1079 |
"document_title": "x", |
| 1080 |
"end_char_index": 0, |
| 1081 |
"start_char_index": 0, |
| 1082 |
"type": "char_location", |
| 1083 |
} |
| 1084 |
], |
| 1085 |
} |
| 1086 |
], |
| 1087 |
thinking={ |
| 1088 |
"type": "adaptive", |
| 1089 |
"block_binding": {"prefix_mismatch_behavior": "error"}, |
| 1090 |
"display": "summarized", |
| 1091 |
}, |
| 1092 |
tool_choice={ |
| 1093 |
"type": "auto", |
| 1094 |
"disable_parallel_tool_use": True, |
| 1095 |
}, |
| 1096 |
tools=[ |
| 1097 |
{ |
| 1098 |
"input_schema": { |
| 1099 |
"type": "object", |
| 1100 |
"properties": { |
| 1101 |
"location": "bar", |
| 1102 |
"unit": "bar", |
| 1103 |
}, |
| 1104 |
"required": ["location"], |
| 1105 |
}, |
| 1106 |
"name": "name", |
| 1107 |
"allowed_callers": ["direct"], |
| 1108 |
"cache_control": { |
| 1109 |
"type": "ephemeral", |
| 1110 |
"ttl": "5m", |
| 1111 |
}, |
| 1112 |
"defer_loading": True, |
| 1113 |
"description": "Get the current weather in a given location", |
| 1114 |
"eager_input_streaming": True, |
| 1115 |
"input_examples": [{"foo": "bar"}], |
| 1116 |
"strict": True, |
| 1117 |
"type": "custom", |
| 1118 |
} |
| 1119 |
], |
| 1120 |
betas=["string"], |
| 1121 |
user_profile_id="anthropic-user-profile-id", |
| 1122 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 1123 |
) |
| 1124 |
assert_matches_type(BetaMessageTokensCount, message, path=["response"]) |
| 1125 |
|
| 1126 |
@parametrize |
| 1127 |
async def test_raw_response_count_tokens(self, async_client: AsyncAnthropic) -> None: |
| 1128 |
response = await async_client.beta.messages.with_raw_response.count_tokens( |
| 1129 |
messages=[ |
| 1130 |
{ |
| 1131 |
"content": "Hello, world", |
| 1132 |
"role": "user", |
| 1133 |
} |
| 1134 |
], |
| 1135 |
model="claude-opus-5", |
| 1136 |
) |
| 1137 |
|
| 1138 |
assert response.is_closed is True |
| 1139 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 1140 |
message = await response.parse() |
| 1141 |
assert_matches_type(BetaMessageTokensCount, message, path=["response"]) |
| 1142 |
|
| 1143 |
@parametrize |
| 1144 |
async def test_streaming_response_count_tokens(self, async_client: AsyncAnthropic) -> None: |
| 1145 |
async with async_client.beta.messages.with_streaming_response.count_tokens( |
| 1146 |
messages=[ |
| 1147 |
{ |
| 1148 |
"content": "Hello, world", |
| 1149 |
"role": "user", |
| 1150 |
} |
| 1151 |
], |
| 1152 |
model="claude-opus-5", |
| 1153 |
) as response: |
| 1154 |
assert not response.is_closed |
| 1155 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 1156 |
|
| 1157 |
message = await response.parse() |
| 1158 |
assert_matches_type(BetaMessageTokensCount, message, path=["response"]) |
| 1159 |
|
| 1160 |
assert cast(Any, response.is_closed) is True |