anthropic 0.76.0__py3-none-any.whl → 0.77.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- anthropic/_base_client.py +5 -2
- anthropic/_compat.py +3 -3
- anthropic/_utils/_json.py +35 -0
- anthropic/_version.py +1 -1
- anthropic/lib/_parse/_response.py +29 -1
- anthropic/lib/streaming/__init__.py +3 -0
- anthropic/lib/streaming/_messages.py +74 -40
- anthropic/lib/streaming/_types.py +42 -2
- anthropic/resources/beta/messages/messages.py +170 -59
- anthropic/resources/messages/messages.py +407 -5
- anthropic/types/__init__.py +7 -0
- anthropic/types/beta/beta_code_execution_tool_20250522_param.py +1 -0
- anthropic/types/beta/beta_code_execution_tool_20250825_param.py +1 -0
- anthropic/types/beta/beta_memory_tool_20250818_param.py +1 -0
- anthropic/types/beta/beta_output_config_param.py +15 -1
- anthropic/types/beta/beta_server_tool_use_block.py +4 -4
- anthropic/types/beta/beta_tool_bash_20241022_param.py +1 -0
- anthropic/types/beta/beta_tool_bash_20250124_param.py +1 -0
- anthropic/types/beta/beta_tool_computer_use_20241022_param.py +1 -0
- anthropic/types/beta/beta_tool_computer_use_20250124_param.py +1 -0
- anthropic/types/beta/beta_tool_computer_use_20251124_param.py +1 -0
- anthropic/types/beta/beta_tool_param.py +1 -0
- anthropic/types/beta/beta_tool_search_tool_bm25_20251119_param.py +1 -0
- anthropic/types/beta/beta_tool_search_tool_regex_20251119_param.py +1 -0
- anthropic/types/beta/beta_tool_text_editor_20241022_param.py +1 -0
- anthropic/types/beta/beta_tool_text_editor_20250124_param.py +1 -0
- anthropic/types/beta/beta_tool_text_editor_20250429_param.py +1 -0
- anthropic/types/beta/beta_tool_text_editor_20250728_param.py +1 -0
- anthropic/types/beta/beta_web_fetch_tool_20250910_param.py +1 -0
- anthropic/types/beta/beta_web_search_tool_20250305_param.py +1 -0
- anthropic/types/beta/beta_web_search_tool_result_error_code.py +1 -1
- anthropic/types/beta/message_count_tokens_params.py +9 -5
- anthropic/types/beta/message_create_params.py +9 -5
- anthropic/types/beta/messages/batch_create_params.py +2 -9
- anthropic/types/json_output_format_param.py +15 -0
- anthropic/types/message_count_tokens_params.py +4 -0
- anthropic/types/message_create_params.py +4 -0
- anthropic/types/output_config_param.py +19 -0
- anthropic/types/parsed_message.py +56 -0
- anthropic/types/tool_bash_20250124_param.py +3 -0
- anthropic/types/tool_param.py +3 -0
- anthropic/types/tool_text_editor_20250124_param.py +3 -0
- anthropic/types/tool_text_editor_20250429_param.py +3 -0
- anthropic/types/tool_text_editor_20250728_param.py +3 -0
- anthropic/types/web_search_tool_20250305_param.py +3 -0
- anthropic/types/web_search_tool_request_error_param.py +8 -1
- anthropic/types/web_search_tool_result_error.py +8 -1
- {anthropic-0.76.0.dist-info → anthropic-0.77.1.dist-info}/METADATA +1 -1
- {anthropic-0.76.0.dist-info → anthropic-0.77.1.dist-info}/RECORD +51 -47
- {anthropic-0.76.0.dist-info → anthropic-0.77.1.dist-info}/WHEEL +0 -0
- {anthropic-0.76.0.dist-info → anthropic-0.77.1.dist-info}/licenses/LICENSE +0 -0
|
@@ -40,12 +40,13 @@ from ....types.beta import (
|
|
|
40
40
|
message_create_params,
|
|
41
41
|
message_count_tokens_params,
|
|
42
42
|
)
|
|
43
|
+
from ...._exceptions import AnthropicError
|
|
43
44
|
from ...._base_client import make_request_options
|
|
44
45
|
from ...._utils._utils import is_dict
|
|
45
46
|
from ....lib.streaming import BetaMessageStreamManager, BetaAsyncMessageStreamManager
|
|
46
47
|
from ...messages.messages import DEPRECATED_MODELS
|
|
47
48
|
from ....types.model_param import ModelParam
|
|
48
|
-
from ....lib._parse._response import ResponseFormatT,
|
|
49
|
+
from ....lib._parse._response import ResponseFormatT, parse_beta_response
|
|
49
50
|
from ....lib._parse._transform import transform_schema
|
|
50
51
|
from ....types.beta.beta_message import BetaMessage
|
|
51
52
|
from ....lib.tools._beta_functions import (
|
|
@@ -233,10 +234,13 @@ class Messages(SyncAPIResource):
|
|
|
233
234
|
|
|
234
235
|
metadata: An object describing metadata about the request.
|
|
235
236
|
|
|
236
|
-
output_config: Configuration options for the model's output
|
|
237
|
-
effort the model puts into its response.
|
|
237
|
+
output_config: Configuration options for the model's output, such as the output format.
|
|
238
238
|
|
|
239
|
-
output_format:
|
|
239
|
+
output_format: Deprecated: Use `output_config.format` instead. See
|
|
240
|
+
[structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
|
|
241
|
+
|
|
242
|
+
A schema to specify Claude's output format in responses. This parameter will be
|
|
243
|
+
removed in a future release.
|
|
240
244
|
|
|
241
245
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
242
246
|
for this request.
|
|
@@ -527,10 +531,13 @@ class Messages(SyncAPIResource):
|
|
|
527
531
|
|
|
528
532
|
metadata: An object describing metadata about the request.
|
|
529
533
|
|
|
530
|
-
output_config: Configuration options for the model's output
|
|
531
|
-
|
|
534
|
+
output_config: Configuration options for the model's output, such as the output format.
|
|
535
|
+
|
|
536
|
+
output_format: Deprecated: Use `output_config.format` instead. See
|
|
537
|
+
[structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
|
|
532
538
|
|
|
533
|
-
|
|
539
|
+
A schema to specify Claude's output format in responses. This parameter will be
|
|
540
|
+
removed in a future release.
|
|
534
541
|
|
|
535
542
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
536
543
|
for this request.
|
|
@@ -817,10 +824,13 @@ class Messages(SyncAPIResource):
|
|
|
817
824
|
|
|
818
825
|
metadata: An object describing metadata about the request.
|
|
819
826
|
|
|
820
|
-
output_config: Configuration options for the model's output
|
|
821
|
-
|
|
827
|
+
output_config: Configuration options for the model's output, such as the output format.
|
|
828
|
+
|
|
829
|
+
output_format: Deprecated: Use `output_config.format` instead. See
|
|
830
|
+
[structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
|
|
822
831
|
|
|
823
|
-
|
|
832
|
+
A schema to specify Claude's output format in responses. This parameter will be
|
|
833
|
+
removed in a future release.
|
|
824
834
|
|
|
825
835
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
826
836
|
for this request.
|
|
@@ -1004,6 +1014,8 @@ class Messages(SyncAPIResource):
|
|
|
1004
1014
|
timeout: float | httpx.Timeout | None | NotGiven = not_given,
|
|
1005
1015
|
) -> BetaMessage | Stream[BetaRawMessageStreamEvent]:
|
|
1006
1016
|
validate_output_format(output_format)
|
|
1017
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
1018
|
+
_warn_output_format_deprecated(output_format)
|
|
1007
1019
|
|
|
1008
1020
|
if not stream and not is_given(timeout) and self._client.timeout == DEFAULT_TIMEOUT:
|
|
1009
1021
|
timeout = self._client._calculate_nonstreaming_timeout(
|
|
@@ -1017,6 +1029,8 @@ class Messages(SyncAPIResource):
|
|
|
1017
1029
|
stacklevel=3,
|
|
1018
1030
|
)
|
|
1019
1031
|
|
|
1032
|
+
merged_output_config = _merge_output_configs(output_config, output_format)
|
|
1033
|
+
|
|
1020
1034
|
extra_headers = {
|
|
1021
1035
|
**strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given}),
|
|
1022
1036
|
**(extra_headers or {}),
|
|
@@ -1032,8 +1046,8 @@ class Messages(SyncAPIResource):
|
|
|
1032
1046
|
"context_management": context_management,
|
|
1033
1047
|
"mcp_servers": mcp_servers,
|
|
1034
1048
|
"metadata": metadata,
|
|
1035
|
-
"output_config":
|
|
1036
|
-
"output_format":
|
|
1049
|
+
"output_config": merged_output_config,
|
|
1050
|
+
"output_format": omit,
|
|
1037
1051
|
"service_tier": service_tier,
|
|
1038
1052
|
"stop_sequences": stop_sequences,
|
|
1039
1053
|
"stream": stream,
|
|
@@ -1087,6 +1101,9 @@ class Messages(SyncAPIResource):
|
|
|
1087
1101
|
extra_body: Body | None = None,
|
|
1088
1102
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
1089
1103
|
) -> ParsedBetaMessage[ResponseFormatT]:
|
|
1104
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
1105
|
+
_warn_output_format_deprecated(output_format)
|
|
1106
|
+
|
|
1090
1107
|
if not stream and not is_given(timeout) and self._client.timeout == DEFAULT_TIMEOUT:
|
|
1091
1108
|
timeout = self._client._calculate_nonstreaming_timeout(
|
|
1092
1109
|
max_tokens, MODEL_NONSTREAMING_TOKENS.get(model, None)
|
|
@@ -1101,9 +1118,9 @@ class Messages(SyncAPIResource):
|
|
|
1101
1118
|
|
|
1102
1119
|
betas = [beta for beta in betas] if is_given(betas) else []
|
|
1103
1120
|
|
|
1104
|
-
if "structured-outputs-2025-
|
|
1121
|
+
if "structured-outputs-2025-12-15" not in betas:
|
|
1105
1122
|
# Ensure structured outputs beta is included for parse method
|
|
1106
|
-
betas.append("structured-outputs-2025-
|
|
1123
|
+
betas.append("structured-outputs-2025-12-15")
|
|
1107
1124
|
|
|
1108
1125
|
extra_headers = {
|
|
1109
1126
|
"X-Stainless-Helper": "beta.messages.parse",
|
|
@@ -1111,26 +1128,28 @@ class Messages(SyncAPIResource):
|
|
|
1111
1128
|
**(extra_headers or {}),
|
|
1112
1129
|
}
|
|
1113
1130
|
|
|
1114
|
-
transformed_output_format: Optional[message_create_params.OutputFormat] | NotGiven = NOT_GIVEN
|
|
1115
|
-
|
|
1116
1131
|
if is_given(output_format) and output_format is not None:
|
|
1117
1132
|
adapted_type: TypeAdapter[ResponseFormatT] = TypeAdapter(output_format)
|
|
1118
1133
|
|
|
1119
1134
|
try:
|
|
1120
1135
|
schema = adapted_type.json_schema()
|
|
1121
|
-
transformed_output_format =
|
|
1136
|
+
transformed_output_format = BetaJSONOutputFormatParam(
|
|
1122
1137
|
schema=transform_schema(schema), type="json_schema"
|
|
1123
1138
|
)
|
|
1124
1139
|
except pydantic.errors.PydanticSchemaGenerationError as e:
|
|
1125
1140
|
raise TypeError(
|
|
1126
1141
|
(
|
|
1127
1142
|
"Could not generate JSON schema for the given `output_format` type. "
|
|
1128
|
-
"Use a type that works with `
|
|
1143
|
+
"Use a type that works with `pydantic.TypeAdapter`"
|
|
1129
1144
|
)
|
|
1130
1145
|
) from e
|
|
1131
1146
|
|
|
1147
|
+
merged_output_config = _merge_output_configs(output_config, transformed_output_format)
|
|
1148
|
+
else:
|
|
1149
|
+
merged_output_config = output_config
|
|
1150
|
+
|
|
1132
1151
|
def parser(response: BetaMessage) -> ParsedBetaMessage[ResponseFormatT]:
|
|
1133
|
-
return
|
|
1152
|
+
return parse_beta_response(
|
|
1134
1153
|
response=response,
|
|
1135
1154
|
output_format=cast(
|
|
1136
1155
|
ResponseFormatT,
|
|
@@ -1149,8 +1168,8 @@ class Messages(SyncAPIResource):
|
|
|
1149
1168
|
"context_management": context_management,
|
|
1150
1169
|
"mcp_servers": mcp_servers,
|
|
1151
1170
|
"metadata": metadata,
|
|
1152
|
-
"output_config":
|
|
1153
|
-
"output_format":
|
|
1171
|
+
"output_config": merged_output_config,
|
|
1172
|
+
"output_format": omit,
|
|
1154
1173
|
"service_tier": service_tier,
|
|
1155
1174
|
"stop_sequences": stop_sequences,
|
|
1156
1175
|
"stream": stream,
|
|
@@ -1310,6 +1329,9 @@ class Messages(SyncAPIResource):
|
|
|
1310
1329
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
1311
1330
|
) -> BetaStreamingToolRunner[ResponseFormatT] | BetaToolRunner[ResponseFormatT]:
|
|
1312
1331
|
"""Create a Message stream"""
|
|
1332
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
1333
|
+
_warn_output_format_deprecated(output_format)
|
|
1334
|
+
|
|
1313
1335
|
if model in DEPRECATED_MODELS:
|
|
1314
1336
|
warnings.warn(
|
|
1315
1337
|
f"The model '{model}' is deprecated and will reach end-of-life on {DEPRECATED_MODELS[model]}.\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.",
|
|
@@ -1413,6 +1435,9 @@ class Messages(SyncAPIResource):
|
|
|
1413
1435
|
extra_body: Body | None = None,
|
|
1414
1436
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
1415
1437
|
) -> BetaMessageStreamManager[ResponseFormatT]:
|
|
1438
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
1439
|
+
_warn_output_format_deprecated(output_format)
|
|
1440
|
+
|
|
1416
1441
|
if model in DEPRECATED_MODELS:
|
|
1417
1442
|
warnings.warn(
|
|
1418
1443
|
f"The model '{model}' is deprecated and will reach end-of-life on {DEPRECATED_MODELS[model]}.\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.",
|
|
@@ -1428,7 +1453,7 @@ class Messages(SyncAPIResource):
|
|
|
1428
1453
|
**(extra_headers or {}),
|
|
1429
1454
|
}
|
|
1430
1455
|
|
|
1431
|
-
transformed_output_format:
|
|
1456
|
+
transformed_output_format: BetaJSONOutputFormatParam | Omit = omit
|
|
1432
1457
|
|
|
1433
1458
|
if is_dict(output_format):
|
|
1434
1459
|
transformed_output_format = cast(BetaJSONOutputFormatParam, output_format)
|
|
@@ -1444,9 +1469,12 @@ class Messages(SyncAPIResource):
|
|
|
1444
1469
|
raise TypeError(
|
|
1445
1470
|
(
|
|
1446
1471
|
"Could not generate JSON schema for the given `output_format` type. "
|
|
1447
|
-
"Use a type that works with `
|
|
1472
|
+
"Use a type that works with `pydantic.TypeAdapter`"
|
|
1448
1473
|
)
|
|
1449
1474
|
) from e
|
|
1475
|
+
|
|
1476
|
+
merged_output_config = _merge_output_configs(output_config, transformed_output_format)
|
|
1477
|
+
|
|
1450
1478
|
make_request = partial(
|
|
1451
1479
|
self._post,
|
|
1452
1480
|
"/v1/messages?beta=true",
|
|
@@ -1456,8 +1484,8 @@ class Messages(SyncAPIResource):
|
|
|
1456
1484
|
"messages": messages,
|
|
1457
1485
|
"model": model,
|
|
1458
1486
|
"metadata": metadata,
|
|
1459
|
-
"output_config":
|
|
1460
|
-
"output_format":
|
|
1487
|
+
"output_config": merged_output_config,
|
|
1488
|
+
"output_format": omit,
|
|
1461
1489
|
"container": container,
|
|
1462
1490
|
"context_management": context_management,
|
|
1463
1491
|
"mcp_servers": mcp_servers,
|
|
@@ -1594,10 +1622,13 @@ class Messages(SyncAPIResource):
|
|
|
1594
1622
|
|
|
1595
1623
|
mcp_servers: MCP servers to be utilized in this request
|
|
1596
1624
|
|
|
1597
|
-
output_config: Configuration options for the model's output
|
|
1598
|
-
|
|
1625
|
+
output_config: Configuration options for the model's output, such as the output format.
|
|
1626
|
+
|
|
1627
|
+
output_format: Deprecated: Use `output_config.format` instead. See
|
|
1628
|
+
[structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
|
|
1599
1629
|
|
|
1600
|
-
|
|
1630
|
+
A schema to specify Claude's output format in responses. This parameter will be
|
|
1631
|
+
removed in a future release.
|
|
1601
1632
|
|
|
1602
1633
|
system: System prompt.
|
|
1603
1634
|
|
|
@@ -1704,6 +1735,11 @@ class Messages(SyncAPIResource):
|
|
|
1704
1735
|
|
|
1705
1736
|
timeout: Override the client-level default timeout for this request, in seconds
|
|
1706
1737
|
"""
|
|
1738
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
1739
|
+
_warn_output_format_deprecated(output_format)
|
|
1740
|
+
|
|
1741
|
+
merged_output_config = _merge_output_configs(output_config, output_format)
|
|
1742
|
+
|
|
1707
1743
|
extra_headers = {
|
|
1708
1744
|
**strip_not_given(
|
|
1709
1745
|
{
|
|
@@ -1723,8 +1759,8 @@ class Messages(SyncAPIResource):
|
|
|
1723
1759
|
"model": model,
|
|
1724
1760
|
"context_management": context_management,
|
|
1725
1761
|
"mcp_servers": mcp_servers,
|
|
1726
|
-
"output_config":
|
|
1727
|
-
"output_format":
|
|
1762
|
+
"output_config": merged_output_config,
|
|
1763
|
+
"output_format": omit,
|
|
1728
1764
|
"system": system,
|
|
1729
1765
|
"thinking": thinking,
|
|
1730
1766
|
"tool_choice": tool_choice,
|
|
@@ -1894,10 +1930,13 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
1894
1930
|
|
|
1895
1931
|
metadata: An object describing metadata about the request.
|
|
1896
1932
|
|
|
1897
|
-
output_config: Configuration options for the model's output
|
|
1898
|
-
effort the model puts into its response.
|
|
1933
|
+
output_config: Configuration options for the model's output, such as the output format.
|
|
1899
1934
|
|
|
1900
|
-
output_format:
|
|
1935
|
+
output_format: Deprecated: Use `output_config.format` instead. See
|
|
1936
|
+
[structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
|
|
1937
|
+
|
|
1938
|
+
A schema to specify Claude's output format in responses. This parameter will be
|
|
1939
|
+
removed in a future release.
|
|
1901
1940
|
|
|
1902
1941
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
1903
1942
|
for this request.
|
|
@@ -2188,10 +2227,13 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2188
2227
|
|
|
2189
2228
|
metadata: An object describing metadata about the request.
|
|
2190
2229
|
|
|
2191
|
-
output_config: Configuration options for the model's output
|
|
2192
|
-
|
|
2230
|
+
output_config: Configuration options for the model's output, such as the output format.
|
|
2231
|
+
|
|
2232
|
+
output_format: Deprecated: Use `output_config.format` instead. See
|
|
2233
|
+
[structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
|
|
2193
2234
|
|
|
2194
|
-
|
|
2235
|
+
A schema to specify Claude's output format in responses. This parameter will be
|
|
2236
|
+
removed in a future release.
|
|
2195
2237
|
|
|
2196
2238
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
2197
2239
|
for this request.
|
|
@@ -2478,10 +2520,13 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2478
2520
|
|
|
2479
2521
|
metadata: An object describing metadata about the request.
|
|
2480
2522
|
|
|
2481
|
-
output_config: Configuration options for the model's output
|
|
2482
|
-
effort the model puts into its response.
|
|
2523
|
+
output_config: Configuration options for the model's output, such as the output format.
|
|
2483
2524
|
|
|
2484
|
-
output_format:
|
|
2525
|
+
output_format: Deprecated: Use `output_config.format` instead. See
|
|
2526
|
+
[structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
|
|
2527
|
+
|
|
2528
|
+
A schema to specify Claude's output format in responses. This parameter will be
|
|
2529
|
+
removed in a future release.
|
|
2485
2530
|
|
|
2486
2531
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
2487
2532
|
for this request.
|
|
@@ -2665,6 +2710,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2665
2710
|
timeout: float | httpx.Timeout | None | NotGiven = not_given,
|
|
2666
2711
|
) -> BetaMessage | AsyncStream[BetaRawMessageStreamEvent]:
|
|
2667
2712
|
validate_output_format(output_format)
|
|
2713
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
2714
|
+
_warn_output_format_deprecated(output_format)
|
|
2668
2715
|
|
|
2669
2716
|
if not stream and not is_given(timeout) and self._client.timeout == DEFAULT_TIMEOUT:
|
|
2670
2717
|
timeout = self._client._calculate_nonstreaming_timeout(
|
|
@@ -2678,6 +2725,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2678
2725
|
stacklevel=3,
|
|
2679
2726
|
)
|
|
2680
2727
|
|
|
2728
|
+
merged_output_config = _merge_output_configs(output_config, output_format)
|
|
2729
|
+
|
|
2681
2730
|
extra_headers = {
|
|
2682
2731
|
**strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given}),
|
|
2683
2732
|
**(extra_headers or {}),
|
|
@@ -2693,8 +2742,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2693
2742
|
"context_management": context_management,
|
|
2694
2743
|
"mcp_servers": mcp_servers,
|
|
2695
2744
|
"metadata": metadata,
|
|
2696
|
-
"output_config":
|
|
2697
|
-
"output_format":
|
|
2745
|
+
"output_config": merged_output_config,
|
|
2746
|
+
"output_format": omit,
|
|
2698
2747
|
"service_tier": service_tier,
|
|
2699
2748
|
"stop_sequences": stop_sequences,
|
|
2700
2749
|
"stream": stream,
|
|
@@ -2748,6 +2797,9 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2748
2797
|
extra_body: Body | None = None,
|
|
2749
2798
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
2750
2799
|
) -> ParsedBetaMessage[ResponseFormatT]:
|
|
2800
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
2801
|
+
_warn_output_format_deprecated(output_format)
|
|
2802
|
+
|
|
2751
2803
|
if not stream and not is_given(timeout) and self._client.timeout == DEFAULT_TIMEOUT:
|
|
2752
2804
|
timeout = self._client._calculate_nonstreaming_timeout(
|
|
2753
2805
|
max_tokens, MODEL_NONSTREAMING_TOKENS.get(model, None)
|
|
@@ -2761,9 +2813,9 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2761
2813
|
)
|
|
2762
2814
|
betas = [beta for beta in betas] if is_given(betas) else []
|
|
2763
2815
|
|
|
2764
|
-
if "structured-outputs-2025-
|
|
2816
|
+
if "structured-outputs-2025-12-15" not in betas:
|
|
2765
2817
|
# Ensure structured outputs beta is included for parse method
|
|
2766
|
-
betas.append("structured-outputs-2025-
|
|
2818
|
+
betas.append("structured-outputs-2025-12-15")
|
|
2767
2819
|
|
|
2768
2820
|
extra_headers = {
|
|
2769
2821
|
"X-Stainless-Helper": "beta.messages.parse",
|
|
@@ -2771,26 +2823,28 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2771
2823
|
**(extra_headers or {}),
|
|
2772
2824
|
}
|
|
2773
2825
|
|
|
2774
|
-
transformed_output_format: Optional[message_create_params.OutputFormat] | NotGiven = NOT_GIVEN
|
|
2775
|
-
|
|
2776
2826
|
if is_given(output_format) and output_format is not None:
|
|
2777
2827
|
adapted_type: TypeAdapter[ResponseFormatT] = TypeAdapter(output_format)
|
|
2778
2828
|
|
|
2779
2829
|
try:
|
|
2780
2830
|
schema = adapted_type.json_schema()
|
|
2781
|
-
transformed_output_format =
|
|
2831
|
+
transformed_output_format = BetaJSONOutputFormatParam(
|
|
2782
2832
|
schema=transform_schema(schema), type="json_schema"
|
|
2783
2833
|
)
|
|
2784
2834
|
except pydantic.errors.PydanticSchemaGenerationError as e:
|
|
2785
2835
|
raise TypeError(
|
|
2786
2836
|
(
|
|
2787
2837
|
"Could not generate JSON schema for the given `output_format` type. "
|
|
2788
|
-
"Use a type that works with `
|
|
2838
|
+
"Use a type that works with `pydantic.TypeAdapter`"
|
|
2789
2839
|
)
|
|
2790
2840
|
) from e
|
|
2791
2841
|
|
|
2842
|
+
merged_output_config = _merge_output_configs(output_config, transformed_output_format)
|
|
2843
|
+
else:
|
|
2844
|
+
merged_output_config = output_config
|
|
2845
|
+
|
|
2792
2846
|
def parser(response: BetaMessage) -> ParsedBetaMessage[ResponseFormatT]:
|
|
2793
|
-
return
|
|
2847
|
+
return parse_beta_response(
|
|
2794
2848
|
response=response,
|
|
2795
2849
|
output_format=cast(
|
|
2796
2850
|
ResponseFormatT,
|
|
@@ -2808,9 +2862,9 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2808
2862
|
"container": container,
|
|
2809
2863
|
"context_management": context_management,
|
|
2810
2864
|
"mcp_servers": mcp_servers,
|
|
2811
|
-
"output_config":
|
|
2865
|
+
"output_config": merged_output_config,
|
|
2812
2866
|
"metadata": metadata,
|
|
2813
|
-
"output_format":
|
|
2867
|
+
"output_format": omit,
|
|
2814
2868
|
"service_tier": service_tier,
|
|
2815
2869
|
"stop_sequences": stop_sequences,
|
|
2816
2870
|
"stream": stream,
|
|
@@ -2970,6 +3024,9 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2970
3024
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
2971
3025
|
) -> BetaAsyncToolRunner[ResponseFormatT] | BetaAsyncStreamingToolRunner[ResponseFormatT]:
|
|
2972
3026
|
"""Create a Message stream"""
|
|
3027
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
3028
|
+
_warn_output_format_deprecated(output_format)
|
|
3029
|
+
|
|
2973
3030
|
if model in DEPRECATED_MODELS:
|
|
2974
3031
|
warnings.warn(
|
|
2975
3032
|
f"The model '{model}' is deprecated and will reach end-of-life on {DEPRECATED_MODELS[model]}.\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.",
|
|
@@ -3073,6 +3130,9 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
3073
3130
|
extra_body: Body | None = None,
|
|
3074
3131
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
3075
3132
|
) -> BetaAsyncMessageStreamManager[ResponseFormatT]:
|
|
3133
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
3134
|
+
_warn_output_format_deprecated(output_format)
|
|
3135
|
+
|
|
3076
3136
|
if model in DEPRECATED_MODELS:
|
|
3077
3137
|
warnings.warn(
|
|
3078
3138
|
f"The model '{model}' is deprecated and will reach end-of-life on {DEPRECATED_MODELS[model]}.\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.",
|
|
@@ -3087,7 +3147,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
3087
3147
|
**(extra_headers or {}),
|
|
3088
3148
|
}
|
|
3089
3149
|
|
|
3090
|
-
transformed_output_format:
|
|
3150
|
+
transformed_output_format: BetaJSONOutputFormatParam | Omit = omit
|
|
3091
3151
|
|
|
3092
3152
|
if is_dict(output_format):
|
|
3093
3153
|
transformed_output_format = cast(BetaJSONOutputFormatParam, output_format)
|
|
@@ -3103,9 +3163,12 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
3103
3163
|
raise TypeError(
|
|
3104
3164
|
(
|
|
3105
3165
|
"Could not generate JSON schema for the given `output_format` type. "
|
|
3106
|
-
"Use a type that works with `
|
|
3166
|
+
"Use a type that works with `pydantic.TypeAdapter`"
|
|
3107
3167
|
)
|
|
3108
3168
|
) from e
|
|
3169
|
+
|
|
3170
|
+
merged_output_config = _merge_output_configs(output_config, transformed_output_format)
|
|
3171
|
+
|
|
3109
3172
|
request = self._post(
|
|
3110
3173
|
"/v1/messages?beta=true",
|
|
3111
3174
|
body=maybe_transform(
|
|
@@ -3114,8 +3177,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
3114
3177
|
"messages": messages,
|
|
3115
3178
|
"model": model,
|
|
3116
3179
|
"metadata": metadata,
|
|
3117
|
-
"output_config":
|
|
3118
|
-
"output_format":
|
|
3180
|
+
"output_config": merged_output_config,
|
|
3181
|
+
"output_format": omit,
|
|
3119
3182
|
"container": container,
|
|
3120
3183
|
"context_management": context_management,
|
|
3121
3184
|
"mcp_servers": mcp_servers,
|
|
@@ -3252,10 +3315,13 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
3252
3315
|
|
|
3253
3316
|
mcp_servers: MCP servers to be utilized in this request
|
|
3254
3317
|
|
|
3255
|
-
output_config: Configuration options for the model's output
|
|
3256
|
-
effort the model puts into its response.
|
|
3318
|
+
output_config: Configuration options for the model's output, such as the output format.
|
|
3257
3319
|
|
|
3258
|
-
output_format:
|
|
3320
|
+
output_format: Deprecated: Use `output_config.format` instead. See
|
|
3321
|
+
[structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
|
|
3322
|
+
|
|
3323
|
+
A schema to specify Claude's output format in responses. This parameter will be
|
|
3324
|
+
removed in a future release.
|
|
3259
3325
|
|
|
3260
3326
|
system: System prompt.
|
|
3261
3327
|
|
|
@@ -3362,6 +3428,11 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
3362
3428
|
|
|
3363
3429
|
timeout: Override the client-level default timeout for this request, in seconds
|
|
3364
3430
|
"""
|
|
3431
|
+
_validate_output_config_conflict(output_config, output_format)
|
|
3432
|
+
_warn_output_format_deprecated(output_format)
|
|
3433
|
+
|
|
3434
|
+
merged_output_config = _merge_output_configs(output_config, output_format)
|
|
3435
|
+
|
|
3365
3436
|
extra_headers = {
|
|
3366
3437
|
**strip_not_given(
|
|
3367
3438
|
{
|
|
@@ -3381,8 +3452,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
3381
3452
|
"model": model,
|
|
3382
3453
|
"context_management": context_management,
|
|
3383
3454
|
"mcp_servers": mcp_servers,
|
|
3384
|
-
"output_config":
|
|
3385
|
-
"output_format":
|
|
3455
|
+
"output_config": merged_output_config,
|
|
3456
|
+
"output_format": omit,
|
|
3386
3457
|
"system": system,
|
|
3387
3458
|
"thinking": thinking,
|
|
3388
3459
|
"tool_choice": tool_choice,
|
|
@@ -3404,6 +3475,9 @@ class MessagesWithRawResponse:
|
|
|
3404
3475
|
self.create = _legacy_response.to_raw_response_wrapper(
|
|
3405
3476
|
messages.create,
|
|
3406
3477
|
)
|
|
3478
|
+
self.parse = _legacy_response.to_raw_response_wrapper(
|
|
3479
|
+
messages.parse,
|
|
3480
|
+
)
|
|
3407
3481
|
self.count_tokens = _legacy_response.to_raw_response_wrapper(
|
|
3408
3482
|
messages.count_tokens,
|
|
3409
3483
|
)
|
|
@@ -3420,6 +3494,9 @@ class AsyncMessagesWithRawResponse:
|
|
|
3420
3494
|
self.create = _legacy_response.async_to_raw_response_wrapper(
|
|
3421
3495
|
messages.create,
|
|
3422
3496
|
)
|
|
3497
|
+
self.parse = _legacy_response.async_to_raw_response_wrapper(
|
|
3498
|
+
messages.parse,
|
|
3499
|
+
)
|
|
3423
3500
|
self.count_tokens = _legacy_response.async_to_raw_response_wrapper(
|
|
3424
3501
|
messages.count_tokens,
|
|
3425
3502
|
)
|
|
@@ -3466,3 +3543,37 @@ def validate_output_format(output_format: object) -> None:
|
|
|
3466
3543
|
raise TypeError(
|
|
3467
3544
|
"You tried to pass a `BaseModel` class to `beta.messages.create()`; You must use `beta.messages.parse()` instead"
|
|
3468
3545
|
)
|
|
3546
|
+
|
|
3547
|
+
|
|
3548
|
+
def _validate_output_config_conflict(
|
|
3549
|
+
output_config: BetaOutputConfigParam | Omit,
|
|
3550
|
+
output_format: object,
|
|
3551
|
+
) -> None:
|
|
3552
|
+
if is_given(output_format) and output_format is not None and is_given(output_config):
|
|
3553
|
+
if "format" in output_config and output_config["format"] is not None:
|
|
3554
|
+
raise AnthropicError(
|
|
3555
|
+
"Both output_format and output_config.format were provided. "
|
|
3556
|
+
"Please use only output_config.format (output_format is deprecated).",
|
|
3557
|
+
)
|
|
3558
|
+
|
|
3559
|
+
|
|
3560
|
+
def _merge_output_configs(
|
|
3561
|
+
output_config: BetaOutputConfigParam | Omit,
|
|
3562
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit,
|
|
3563
|
+
) -> BetaOutputConfigParam | Omit:
|
|
3564
|
+
if is_given(output_format):
|
|
3565
|
+
if is_given(output_config):
|
|
3566
|
+
return {**output_config, "format": output_format}
|
|
3567
|
+
else:
|
|
3568
|
+
return {"format": output_format}
|
|
3569
|
+
return output_config
|
|
3570
|
+
|
|
3571
|
+
|
|
3572
|
+
def _warn_output_format_deprecated(output_format: object) -> None:
|
|
3573
|
+
"""Emit deprecation warning if output_format is provided."""
|
|
3574
|
+
if is_given(output_format) and output_format is not None:
|
|
3575
|
+
warnings.warn(
|
|
3576
|
+
"The 'output_format' parameter is deprecated. Please use 'output_config.format' instead.",
|
|
3577
|
+
DeprecationWarning,
|
|
3578
|
+
stacklevel=4,
|
|
3579
|
+
)
|