anthropic 0.72.0__py3-none-any.whl → 0.73.0__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/__init__.py +2 -0
- anthropic/_compat.py +6 -0
- anthropic/_models.py +50 -16
- anthropic/_streaming.py +4 -6
- anthropic/_utils/_sync.py +3 -31
- anthropic/_utils/_transform.py +1 -1
- anthropic/_utils/_utils.py +1 -1
- anthropic/_version.py +1 -1
- anthropic/lib/_parse/_response.py +44 -0
- anthropic/lib/_parse/_transform.py +167 -0
- anthropic/lib/streaming/__init__.py +14 -4
- anthropic/lib/streaming/_beta_messages.py +82 -43
- anthropic/lib/streaming/_beta_types.py +21 -13
- anthropic/lib/tools/_beta_runner.py +102 -101
- anthropic/resources/beta/messages/batches.py +12 -12
- anthropic/resources/beta/messages/messages.py +365 -29
- anthropic/resources/messages/batches.py +12 -12
- anthropic/resources/messages/messages.py +14 -8
- anthropic/types/beta/__init__.py +1 -0
- anthropic/types/beta/beta_code_execution_tool_20250522_param.py +2 -0
- anthropic/types/beta/beta_code_execution_tool_20250825_param.py +2 -0
- anthropic/types/beta/beta_json_output_format_param.py +15 -0
- anthropic/types/beta/beta_memory_tool_20250818_param.py +2 -0
- anthropic/types/beta/beta_tool_bash_20241022_param.py +2 -0
- anthropic/types/beta/beta_tool_bash_20250124_param.py +2 -0
- anthropic/types/beta/beta_tool_computer_use_20241022_param.py +2 -0
- anthropic/types/beta/beta_tool_computer_use_20250124_param.py +2 -0
- anthropic/types/beta/beta_tool_param.py +2 -0
- anthropic/types/beta/beta_tool_text_editor_20241022_param.py +2 -0
- anthropic/types/beta/beta_tool_text_editor_20250124_param.py +2 -0
- anthropic/types/beta/beta_tool_text_editor_20250429_param.py +2 -0
- anthropic/types/beta/beta_tool_text_editor_20250728_param.py +2 -0
- anthropic/types/beta/beta_web_fetch_tool_20250910_param.py +2 -0
- anthropic/types/beta/beta_web_search_tool_20250305_param.py +2 -0
- anthropic/types/beta/message_count_tokens_params.py +4 -0
- anthropic/types/beta/message_create_params.py +24 -2
- anthropic/types/beta/messages/batch_create_params.py +8 -2
- anthropic/types/beta/parsed_beta_message.py +68 -0
- anthropic/types/messages/batch_create_params.py +0 -1
- {anthropic-0.72.0.dist-info → anthropic-0.73.0.dist-info}/METADATA +4 -5
- {anthropic-0.72.0.dist-info → anthropic-0.73.0.dist-info}/RECORD +43 -39
- {anthropic-0.72.0.dist-info → anthropic-0.73.0.dist-info}/WHEEL +0 -0
- {anthropic-0.72.0.dist-info → anthropic-0.73.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
import inspect
|
|
5
6
|
import warnings
|
|
6
|
-
from typing import TYPE_CHECKING, List, Union, Iterable, Optional, cast
|
|
7
|
+
from typing import TYPE_CHECKING, List, Type, Union, Iterable, Optional, cast
|
|
7
8
|
from functools import partial
|
|
8
9
|
from itertools import chain
|
|
9
10
|
from typing_extensions import Literal, overload
|
|
10
11
|
|
|
11
12
|
import httpx
|
|
13
|
+
import pydantic
|
|
12
14
|
|
|
13
15
|
from .... import _legacy_response
|
|
14
16
|
from .batches import (
|
|
@@ -22,6 +24,7 @@ from .batches import (
|
|
|
22
24
|
from ...._types import NOT_GIVEN, Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given
|
|
23
25
|
from ...._utils import is_given, required_args, maybe_transform, strip_not_given, async_maybe_transform
|
|
24
26
|
from ...._compat import cached_property
|
|
27
|
+
from ...._models import TypeAdapter
|
|
25
28
|
from ...._resource import SyncAPIResource, AsyncAPIResource
|
|
26
29
|
from ...._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
|
|
27
30
|
from ....lib.tools import (
|
|
@@ -41,16 +44,20 @@ from ...._base_client import make_request_options
|
|
|
41
44
|
from ....lib.streaming import BetaMessageStreamManager, BetaAsyncMessageStreamManager
|
|
42
45
|
from ...messages.messages import DEPRECATED_MODELS
|
|
43
46
|
from ....types.model_param import ModelParam
|
|
47
|
+
from ....lib._parse._response import ResponseFormatT, parse_response
|
|
48
|
+
from ....lib._parse._transform import transform_schema
|
|
44
49
|
from ....types.beta.beta_message import BetaMessage
|
|
45
50
|
from ....lib.tools._beta_functions import BetaRunnableTool, BetaAsyncRunnableTool
|
|
46
51
|
from ....types.anthropic_beta_param import AnthropicBetaParam
|
|
47
52
|
from ....types.beta.beta_message_param import BetaMessageParam
|
|
48
53
|
from ....types.beta.beta_metadata_param import BetaMetadataParam
|
|
54
|
+
from ....types.beta.parsed_beta_message import ParsedBetaMessage
|
|
49
55
|
from ....types.beta.beta_text_block_param import BetaTextBlockParam
|
|
50
56
|
from ....types.beta.beta_tool_union_param import BetaToolUnionParam
|
|
51
57
|
from ....types.beta.beta_tool_choice_param import BetaToolChoiceParam
|
|
52
58
|
from ....types.beta.beta_message_tokens_count import BetaMessageTokensCount
|
|
53
59
|
from ....types.beta.beta_thinking_config_param import BetaThinkingConfigParam
|
|
60
|
+
from ....types.beta.beta_json_output_format_param import BetaJSONOutputFormatParam
|
|
54
61
|
from ....types.beta.beta_raw_message_stream_event import BetaRawMessageStreamEvent
|
|
55
62
|
from ....types.beta.beta_context_management_config_param import BetaContextManagementConfigParam
|
|
56
63
|
from ....types.beta.beta_request_mcp_server_url_definition_param import BetaRequestMCPServerURLDefinitionParam
|
|
@@ -96,6 +103,7 @@ class Messages(SyncAPIResource):
|
|
|
96
103
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
97
104
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
98
105
|
metadata: BetaMetadataParam | Omit = omit,
|
|
106
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
99
107
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
100
108
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
101
109
|
stream: Literal[False] | Omit = omit,
|
|
@@ -121,7 +129,8 @@ class Messages(SyncAPIResource):
|
|
|
121
129
|
The Messages API can be used for either single queries or stateless multi-turn
|
|
122
130
|
conversations.
|
|
123
131
|
|
|
124
|
-
Learn more about the Messages API in our
|
|
132
|
+
Learn more about the Messages API in our
|
|
133
|
+
[user guide](https://docs.claude.com/en/docs/initial-setup)
|
|
125
134
|
|
|
126
135
|
Args:
|
|
127
136
|
max_tokens: The maximum number of tokens to generate before stopping.
|
|
@@ -213,6 +222,8 @@ class Messages(SyncAPIResource):
|
|
|
213
222
|
|
|
214
223
|
metadata: An object describing metadata about the request.
|
|
215
224
|
|
|
225
|
+
output_format: A schema to specify Claude's output format in responses.
|
|
226
|
+
|
|
216
227
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
217
228
|
for this request.
|
|
218
229
|
|
|
@@ -379,6 +390,7 @@ class Messages(SyncAPIResource):
|
|
|
379
390
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
380
391
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
381
392
|
metadata: BetaMetadataParam | Omit = omit,
|
|
393
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
382
394
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
383
395
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
384
396
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
@@ -403,7 +415,8 @@ class Messages(SyncAPIResource):
|
|
|
403
415
|
The Messages API can be used for either single queries or stateless multi-turn
|
|
404
416
|
conversations.
|
|
405
417
|
|
|
406
|
-
Learn more about the Messages API in our
|
|
418
|
+
Learn more about the Messages API in our
|
|
419
|
+
[user guide](https://docs.claude.com/en/docs/initial-setup)
|
|
407
420
|
|
|
408
421
|
Args:
|
|
409
422
|
max_tokens: The maximum number of tokens to generate before stopping.
|
|
@@ -499,6 +512,8 @@ class Messages(SyncAPIResource):
|
|
|
499
512
|
|
|
500
513
|
metadata: An object describing metadata about the request.
|
|
501
514
|
|
|
515
|
+
output_format: A schema to specify Claude's output format in responses.
|
|
516
|
+
|
|
502
517
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
503
518
|
for this request.
|
|
504
519
|
|
|
@@ -661,6 +676,7 @@ class Messages(SyncAPIResource):
|
|
|
661
676
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
662
677
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
663
678
|
metadata: BetaMetadataParam | Omit = omit,
|
|
679
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
664
680
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
665
681
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
666
682
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
@@ -685,7 +701,8 @@ class Messages(SyncAPIResource):
|
|
|
685
701
|
The Messages API can be used for either single queries or stateless multi-turn
|
|
686
702
|
conversations.
|
|
687
703
|
|
|
688
|
-
Learn more about the Messages API in our
|
|
704
|
+
Learn more about the Messages API in our
|
|
705
|
+
[user guide](https://docs.claude.com/en/docs/initial-setup)
|
|
689
706
|
|
|
690
707
|
Args:
|
|
691
708
|
max_tokens: The maximum number of tokens to generate before stopping.
|
|
@@ -781,6 +798,8 @@ class Messages(SyncAPIResource):
|
|
|
781
798
|
|
|
782
799
|
metadata: An object describing metadata about the request.
|
|
783
800
|
|
|
801
|
+
output_format: A schema to specify Claude's output format in responses.
|
|
802
|
+
|
|
784
803
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
785
804
|
for this request.
|
|
786
805
|
|
|
@@ -942,6 +961,7 @@ class Messages(SyncAPIResource):
|
|
|
942
961
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
943
962
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
944
963
|
metadata: BetaMetadataParam | Omit = omit,
|
|
964
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
945
965
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
946
966
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
947
967
|
stream: Literal[False] | Literal[True] | Omit = omit,
|
|
@@ -960,6 +980,8 @@ class Messages(SyncAPIResource):
|
|
|
960
980
|
extra_body: Body | None = None,
|
|
961
981
|
timeout: float | httpx.Timeout | None | NotGiven = not_given,
|
|
962
982
|
) -> BetaMessage | Stream[BetaRawMessageStreamEvent]:
|
|
983
|
+
validate_output_format(output_format)
|
|
984
|
+
|
|
963
985
|
if not stream and not is_given(timeout) and self._client.timeout == DEFAULT_TIMEOUT:
|
|
964
986
|
timeout = self._client._calculate_nonstreaming_timeout(
|
|
965
987
|
max_tokens, MODEL_NONSTREAMING_TOKENS.get(model, None)
|
|
@@ -987,6 +1009,7 @@ class Messages(SyncAPIResource):
|
|
|
987
1009
|
"context_management": context_management,
|
|
988
1010
|
"mcp_servers": mcp_servers,
|
|
989
1011
|
"metadata": metadata,
|
|
1012
|
+
"output_format": output_format,
|
|
990
1013
|
"service_tier": service_tier,
|
|
991
1014
|
"stop_sequences": stop_sequences,
|
|
992
1015
|
"stream": stream,
|
|
@@ -1010,6 +1033,122 @@ class Messages(SyncAPIResource):
|
|
|
1010
1033
|
stream_cls=Stream[BetaRawMessageStreamEvent],
|
|
1011
1034
|
)
|
|
1012
1035
|
|
|
1036
|
+
def parse(
|
|
1037
|
+
self,
|
|
1038
|
+
*,
|
|
1039
|
+
max_tokens: int,
|
|
1040
|
+
messages: Iterable[BetaMessageParam],
|
|
1041
|
+
model: ModelParam,
|
|
1042
|
+
container: Optional[message_create_params.Container] | Omit = omit,
|
|
1043
|
+
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
1044
|
+
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
1045
|
+
metadata: BetaMetadataParam | Omit = omit,
|
|
1046
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
1047
|
+
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
1048
|
+
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
1049
|
+
stream: Literal[False] | Literal[True] | Omit = omit,
|
|
1050
|
+
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
1051
|
+
temperature: float | Omit = omit,
|
|
1052
|
+
thinking: BetaThinkingConfigParam | Omit = omit,
|
|
1053
|
+
tool_choice: BetaToolChoiceParam | Omit = omit,
|
|
1054
|
+
tools: Iterable[BetaToolUnionParam] | Omit = omit,
|
|
1055
|
+
top_k: int | Omit = omit,
|
|
1056
|
+
top_p: float | Omit = omit,
|
|
1057
|
+
betas: List[AnthropicBetaParam] | Omit = omit,
|
|
1058
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
1059
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
1060
|
+
extra_headers: Headers | None = None,
|
|
1061
|
+
extra_query: Query | None = None,
|
|
1062
|
+
extra_body: Body | None = None,
|
|
1063
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
1064
|
+
) -> ParsedBetaMessage[ResponseFormatT]:
|
|
1065
|
+
if not stream and not is_given(timeout) and self._client.timeout == DEFAULT_TIMEOUT:
|
|
1066
|
+
timeout = self._client._calculate_nonstreaming_timeout(
|
|
1067
|
+
max_tokens, MODEL_NONSTREAMING_TOKENS.get(model, None)
|
|
1068
|
+
)
|
|
1069
|
+
|
|
1070
|
+
if model in DEPRECATED_MODELS:
|
|
1071
|
+
warnings.warn(
|
|
1072
|
+
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.",
|
|
1073
|
+
DeprecationWarning,
|
|
1074
|
+
stacklevel=3,
|
|
1075
|
+
)
|
|
1076
|
+
|
|
1077
|
+
betas = [beta for beta in betas] if is_given(betas) else []
|
|
1078
|
+
|
|
1079
|
+
if "structured-outputs-2025-09-17" not in betas:
|
|
1080
|
+
# Ensure structured outputs beta is included for parse method
|
|
1081
|
+
betas.append("structured-outputs-2025-09-17")
|
|
1082
|
+
|
|
1083
|
+
extra_headers = {
|
|
1084
|
+
"X-Stainless-Helper": "beta.messages.parse",
|
|
1085
|
+
**strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else NOT_GIVEN}),
|
|
1086
|
+
**(extra_headers or {}),
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
transformed_output_format: Optional[message_create_params.OutputFormat] | NotGiven = NOT_GIVEN
|
|
1090
|
+
|
|
1091
|
+
if is_given(output_format) and output_format is not None:
|
|
1092
|
+
adapted_type: TypeAdapter[ResponseFormatT] = TypeAdapter(output_format)
|
|
1093
|
+
|
|
1094
|
+
try:
|
|
1095
|
+
schema = adapted_type.json_schema()
|
|
1096
|
+
transformed_output_format = message_create_params.OutputFormat(
|
|
1097
|
+
schema=transform_schema(schema), type="json_schema"
|
|
1098
|
+
)
|
|
1099
|
+
except pydantic.errors.PydanticSchemaGenerationError as e:
|
|
1100
|
+
raise TypeError(
|
|
1101
|
+
(
|
|
1102
|
+
"Could not generate JSON schema for the given `output_format` type. "
|
|
1103
|
+
"Use a type that works with `pydanitc.TypeAdapter`"
|
|
1104
|
+
)
|
|
1105
|
+
) from e
|
|
1106
|
+
|
|
1107
|
+
def parser(response: BetaMessage) -> ParsedBetaMessage[ResponseFormatT]:
|
|
1108
|
+
return parse_response(
|
|
1109
|
+
response=response,
|
|
1110
|
+
output_format=cast(
|
|
1111
|
+
ResponseFormatT,
|
|
1112
|
+
output_format if is_given(output_format) and output_format is not None else NOT_GIVEN,
|
|
1113
|
+
),
|
|
1114
|
+
)
|
|
1115
|
+
|
|
1116
|
+
return self._post(
|
|
1117
|
+
"/v1/messages?beta=true",
|
|
1118
|
+
body=maybe_transform(
|
|
1119
|
+
{
|
|
1120
|
+
"max_tokens": max_tokens,
|
|
1121
|
+
"messages": messages,
|
|
1122
|
+
"model": model,
|
|
1123
|
+
"container": container,
|
|
1124
|
+
"context_management": context_management,
|
|
1125
|
+
"mcp_servers": mcp_servers,
|
|
1126
|
+
"metadata": metadata,
|
|
1127
|
+
"output_format": transformed_output_format,
|
|
1128
|
+
"service_tier": service_tier,
|
|
1129
|
+
"stop_sequences": stop_sequences,
|
|
1130
|
+
"stream": stream,
|
|
1131
|
+
"system": system,
|
|
1132
|
+
"temperature": temperature,
|
|
1133
|
+
"thinking": thinking,
|
|
1134
|
+
"tool_choice": tool_choice,
|
|
1135
|
+
"tools": tools,
|
|
1136
|
+
"top_k": top_k,
|
|
1137
|
+
"top_p": top_p,
|
|
1138
|
+
},
|
|
1139
|
+
message_create_params.MessageCreateParamsNonStreaming,
|
|
1140
|
+
),
|
|
1141
|
+
options=make_request_options(
|
|
1142
|
+
extra_headers=extra_headers,
|
|
1143
|
+
extra_query=extra_query,
|
|
1144
|
+
extra_body=extra_body,
|
|
1145
|
+
timeout=timeout,
|
|
1146
|
+
post_parser=parser,
|
|
1147
|
+
),
|
|
1148
|
+
cast_to=cast(Type[ParsedBetaMessage[ResponseFormatT]], BetaMessage),
|
|
1149
|
+
stream=False,
|
|
1150
|
+
)
|
|
1151
|
+
|
|
1013
1152
|
@overload
|
|
1014
1153
|
def tool_runner(
|
|
1015
1154
|
self,
|
|
@@ -1018,11 +1157,12 @@ class Messages(SyncAPIResource):
|
|
|
1018
1157
|
messages: Iterable[BetaMessageParam],
|
|
1019
1158
|
model: ModelParam,
|
|
1020
1159
|
tools: Iterable[BetaRunnableTool],
|
|
1021
|
-
max_iterations: int | Omit = omit,
|
|
1022
1160
|
container: Optional[message_create_params.Container] | Omit = omit,
|
|
1023
1161
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
1162
|
+
max_iterations: int | Omit = omit,
|
|
1024
1163
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
1025
1164
|
metadata: BetaMetadataParam | Omit = omit,
|
|
1165
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
1026
1166
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
1027
1167
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
1028
1168
|
stream: Literal[False] | Omit = omit,
|
|
@@ -1039,7 +1179,7 @@ class Messages(SyncAPIResource):
|
|
|
1039
1179
|
extra_query: Query | None = None,
|
|
1040
1180
|
extra_body: Body | None = None,
|
|
1041
1181
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
1042
|
-
) -> BetaToolRunner: ...
|
|
1182
|
+
) -> BetaToolRunner[ResponseFormatT]: ...
|
|
1043
1183
|
|
|
1044
1184
|
@overload
|
|
1045
1185
|
def tool_runner(
|
|
@@ -1055,6 +1195,7 @@ class Messages(SyncAPIResource):
|
|
|
1055
1195
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
1056
1196
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
1057
1197
|
metadata: BetaMetadataParam | Omit = omit,
|
|
1198
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
1058
1199
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
1059
1200
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
1060
1201
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
@@ -1070,7 +1211,7 @@ class Messages(SyncAPIResource):
|
|
|
1070
1211
|
extra_query: Query | None = None,
|
|
1071
1212
|
extra_body: Body | None = None,
|
|
1072
1213
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
1073
|
-
) -> BetaStreamingToolRunner: ...
|
|
1214
|
+
) -> BetaStreamingToolRunner[ResponseFormatT]: ...
|
|
1074
1215
|
|
|
1075
1216
|
@overload
|
|
1076
1217
|
def tool_runner(
|
|
@@ -1086,6 +1227,7 @@ class Messages(SyncAPIResource):
|
|
|
1086
1227
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
1087
1228
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
1088
1229
|
metadata: BetaMetadataParam | Omit = omit,
|
|
1230
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
1089
1231
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
1090
1232
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
1091
1233
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
@@ -1101,7 +1243,7 @@ class Messages(SyncAPIResource):
|
|
|
1101
1243
|
extra_query: Query | None = None,
|
|
1102
1244
|
extra_body: Body | None = None,
|
|
1103
1245
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
1104
|
-
) -> BetaStreamingToolRunner | BetaToolRunner: ...
|
|
1246
|
+
) -> BetaStreamingToolRunner[ResponseFormatT] | BetaToolRunner[ResponseFormatT]: ...
|
|
1105
1247
|
|
|
1106
1248
|
def tool_runner(
|
|
1107
1249
|
self,
|
|
@@ -1115,6 +1257,7 @@ class Messages(SyncAPIResource):
|
|
|
1115
1257
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
1116
1258
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
1117
1259
|
metadata: BetaMetadataParam | Omit = omit,
|
|
1260
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
1118
1261
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
1119
1262
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
1120
1263
|
stream: bool | Omit = omit,
|
|
@@ -1131,7 +1274,7 @@ class Messages(SyncAPIResource):
|
|
|
1131
1274
|
extra_query: Query | None = None,
|
|
1132
1275
|
extra_body: Body | None = None,
|
|
1133
1276
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
1134
|
-
) ->
|
|
1277
|
+
) -> BetaStreamingToolRunner[ResponseFormatT] | BetaToolRunner[ResponseFormatT]:
|
|
1135
1278
|
"""Create a Message stream"""
|
|
1136
1279
|
if model in DEPRECATED_MODELS:
|
|
1137
1280
|
warnings.warn(
|
|
@@ -1147,7 +1290,7 @@ class Messages(SyncAPIResource):
|
|
|
1147
1290
|
}
|
|
1148
1291
|
|
|
1149
1292
|
params = cast(
|
|
1150
|
-
message_create_params.
|
|
1293
|
+
message_create_params.ParseMessageCreateParamsBase[ResponseFormatT],
|
|
1151
1294
|
{
|
|
1152
1295
|
"max_tokens": max_tokens,
|
|
1153
1296
|
"messages": messages,
|
|
@@ -1156,6 +1299,7 @@ class Messages(SyncAPIResource):
|
|
|
1156
1299
|
"context_management": context_management,
|
|
1157
1300
|
"mcp_servers": mcp_servers,
|
|
1158
1301
|
"metadata": metadata,
|
|
1302
|
+
"output_format": output_format,
|
|
1159
1303
|
"service_tier": service_tier,
|
|
1160
1304
|
"stop_sequences": stop_sequences,
|
|
1161
1305
|
"system": system,
|
|
@@ -1169,7 +1313,7 @@ class Messages(SyncAPIResource):
|
|
|
1169
1313
|
)
|
|
1170
1314
|
|
|
1171
1315
|
if stream:
|
|
1172
|
-
return BetaStreamingToolRunner(
|
|
1316
|
+
return BetaStreamingToolRunner[ResponseFormatT](
|
|
1173
1317
|
tools=tools,
|
|
1174
1318
|
params=params,
|
|
1175
1319
|
options={
|
|
@@ -1181,7 +1325,7 @@ class Messages(SyncAPIResource):
|
|
|
1181
1325
|
client=cast("Anthropic", self._client),
|
|
1182
1326
|
max_iterations=max_iterations if is_given(max_iterations) else None,
|
|
1183
1327
|
)
|
|
1184
|
-
return BetaToolRunner(
|
|
1328
|
+
return BetaToolRunner[ResponseFormatT](
|
|
1185
1329
|
tools=tools,
|
|
1186
1330
|
params=params,
|
|
1187
1331
|
options={
|
|
@@ -1204,6 +1348,7 @@ class Messages(SyncAPIResource):
|
|
|
1204
1348
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
1205
1349
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
1206
1350
|
metadata: BetaMetadataParam | Omit = omit,
|
|
1351
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
1207
1352
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
1208
1353
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
1209
1354
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
@@ -1220,7 +1365,7 @@ class Messages(SyncAPIResource):
|
|
|
1220
1365
|
extra_query: Query | None = None,
|
|
1221
1366
|
extra_body: Body | None = None,
|
|
1222
1367
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
1223
|
-
) -> BetaMessageStreamManager:
|
|
1368
|
+
) -> BetaMessageStreamManager[ResponseFormatT]:
|
|
1224
1369
|
if model in DEPRECATED_MODELS:
|
|
1225
1370
|
warnings.warn(
|
|
1226
1371
|
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.",
|
|
@@ -1234,6 +1379,25 @@ class Messages(SyncAPIResource):
|
|
|
1234
1379
|
**strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else NOT_GIVEN}),
|
|
1235
1380
|
**(extra_headers or {}),
|
|
1236
1381
|
}
|
|
1382
|
+
|
|
1383
|
+
transformed_output_format: Optional[message_create_params.OutputFormat] | NotGiven = NOT_GIVEN
|
|
1384
|
+
|
|
1385
|
+
if is_given(output_format) and output_format is not None:
|
|
1386
|
+
adapted_type: TypeAdapter[ResponseFormatT] = TypeAdapter(output_format)
|
|
1387
|
+
|
|
1388
|
+
try:
|
|
1389
|
+
schema = adapted_type.json_schema()
|
|
1390
|
+
transformed_output_format = message_create_params.OutputFormat(
|
|
1391
|
+
schema=transform_schema(schema), type="json_schema"
|
|
1392
|
+
)
|
|
1393
|
+
except pydantic.errors.PydanticSchemaGenerationError as e:
|
|
1394
|
+
raise TypeError(
|
|
1395
|
+
(
|
|
1396
|
+
"Could not generate JSON schema for the given `output_format` type. "
|
|
1397
|
+
"Use a type that works with `pydanitc.TypeAdapter`"
|
|
1398
|
+
)
|
|
1399
|
+
) from e
|
|
1400
|
+
|
|
1237
1401
|
make_request = partial(
|
|
1238
1402
|
self._post,
|
|
1239
1403
|
"/v1/messages?beta=true",
|
|
@@ -1243,6 +1407,7 @@ class Messages(SyncAPIResource):
|
|
|
1243
1407
|
"messages": messages,
|
|
1244
1408
|
"model": model,
|
|
1245
1409
|
"metadata": metadata,
|
|
1410
|
+
"output_format": transformed_output_format,
|
|
1246
1411
|
"container": container,
|
|
1247
1412
|
"context_management": context_management,
|
|
1248
1413
|
"mcp_servers": mcp_servers,
|
|
@@ -1266,7 +1431,7 @@ class Messages(SyncAPIResource):
|
|
|
1266
1431
|
stream=True,
|
|
1267
1432
|
stream_cls=Stream[BetaRawMessageStreamEvent],
|
|
1268
1433
|
)
|
|
1269
|
-
return BetaMessageStreamManager(make_request)
|
|
1434
|
+
return BetaMessageStreamManager(make_request, output_format=cast(ResponseFormatT, output_format))
|
|
1270
1435
|
|
|
1271
1436
|
def count_tokens(
|
|
1272
1437
|
self,
|
|
@@ -1275,6 +1440,7 @@ class Messages(SyncAPIResource):
|
|
|
1275
1440
|
model: ModelParam,
|
|
1276
1441
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
1277
1442
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
1443
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
1278
1444
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
1279
1445
|
thinking: BetaThinkingConfigParam | Omit = omit,
|
|
1280
1446
|
tool_choice: BetaToolChoiceParam | Omit = omit,
|
|
@@ -1294,7 +1460,7 @@ class Messages(SyncAPIResource):
|
|
|
1294
1460
|
including tools, images, and documents, without creating it.
|
|
1295
1461
|
|
|
1296
1462
|
Learn more about token counting in our
|
|
1297
|
-
[user guide](/en/docs/build-with-claude/token-counting)
|
|
1463
|
+
[user guide](https://docs.claude.com/en/docs/build-with-claude/token-counting)
|
|
1298
1464
|
|
|
1299
1465
|
Args:
|
|
1300
1466
|
messages: Input messages.
|
|
@@ -1374,6 +1540,8 @@ class Messages(SyncAPIResource):
|
|
|
1374
1540
|
|
|
1375
1541
|
mcp_servers: MCP servers to be utilized in this request
|
|
1376
1542
|
|
|
1543
|
+
output_format: A schema to specify Claude's output format in responses.
|
|
1544
|
+
|
|
1377
1545
|
system: System prompt.
|
|
1378
1546
|
|
|
1379
1547
|
A system prompt is a way of providing context and instructions to Claude, such
|
|
@@ -1498,6 +1666,7 @@ class Messages(SyncAPIResource):
|
|
|
1498
1666
|
"model": model,
|
|
1499
1667
|
"context_management": context_management,
|
|
1500
1668
|
"mcp_servers": mcp_servers,
|
|
1669
|
+
"output_format": output_format,
|
|
1501
1670
|
"system": system,
|
|
1502
1671
|
"thinking": thinking,
|
|
1503
1672
|
"tool_choice": tool_choice,
|
|
@@ -1547,6 +1716,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
1547
1716
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
1548
1717
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
1549
1718
|
metadata: BetaMetadataParam | Omit = omit,
|
|
1719
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
1550
1720
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
1551
1721
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
1552
1722
|
stream: Literal[False] | Omit = omit,
|
|
@@ -1572,7 +1742,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
1572
1742
|
The Messages API can be used for either single queries or stateless multi-turn
|
|
1573
1743
|
conversations.
|
|
1574
1744
|
|
|
1575
|
-
Learn more about the Messages API in our
|
|
1745
|
+
Learn more about the Messages API in our
|
|
1746
|
+
[user guide](https://docs.claude.com/en/docs/initial-setup)
|
|
1576
1747
|
|
|
1577
1748
|
Args:
|
|
1578
1749
|
max_tokens: The maximum number of tokens to generate before stopping.
|
|
@@ -1664,6 +1835,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
1664
1835
|
|
|
1665
1836
|
metadata: An object describing metadata about the request.
|
|
1666
1837
|
|
|
1838
|
+
output_format: A schema to specify Claude's output format in responses.
|
|
1839
|
+
|
|
1667
1840
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
1668
1841
|
for this request.
|
|
1669
1842
|
|
|
@@ -1830,6 +2003,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
1830
2003
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
1831
2004
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
1832
2005
|
metadata: BetaMetadataParam | Omit = omit,
|
|
2006
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
1833
2007
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
1834
2008
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
1835
2009
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
@@ -1854,7 +2028,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
1854
2028
|
The Messages API can be used for either single queries or stateless multi-turn
|
|
1855
2029
|
conversations.
|
|
1856
2030
|
|
|
1857
|
-
Learn more about the Messages API in our
|
|
2031
|
+
Learn more about the Messages API in our
|
|
2032
|
+
[user guide](https://docs.claude.com/en/docs/initial-setup)
|
|
1858
2033
|
|
|
1859
2034
|
Args:
|
|
1860
2035
|
max_tokens: The maximum number of tokens to generate before stopping.
|
|
@@ -1950,6 +2125,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
1950
2125
|
|
|
1951
2126
|
metadata: An object describing metadata about the request.
|
|
1952
2127
|
|
|
2128
|
+
output_format: A schema to specify Claude's output format in responses.
|
|
2129
|
+
|
|
1953
2130
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
1954
2131
|
for this request.
|
|
1955
2132
|
|
|
@@ -2112,6 +2289,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2112
2289
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
2113
2290
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
2114
2291
|
metadata: BetaMetadataParam | Omit = omit,
|
|
2292
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
2115
2293
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
2116
2294
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
2117
2295
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
@@ -2136,7 +2314,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2136
2314
|
The Messages API can be used for either single queries or stateless multi-turn
|
|
2137
2315
|
conversations.
|
|
2138
2316
|
|
|
2139
|
-
Learn more about the Messages API in our
|
|
2317
|
+
Learn more about the Messages API in our
|
|
2318
|
+
[user guide](https://docs.claude.com/en/docs/initial-setup)
|
|
2140
2319
|
|
|
2141
2320
|
Args:
|
|
2142
2321
|
max_tokens: The maximum number of tokens to generate before stopping.
|
|
@@ -2232,6 +2411,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2232
2411
|
|
|
2233
2412
|
metadata: An object describing metadata about the request.
|
|
2234
2413
|
|
|
2414
|
+
output_format: A schema to specify Claude's output format in responses.
|
|
2415
|
+
|
|
2235
2416
|
service_tier: Determines whether to use priority capacity (if available) or standard capacity
|
|
2236
2417
|
for this request.
|
|
2237
2418
|
|
|
@@ -2393,6 +2574,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2393
2574
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
2394
2575
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
2395
2576
|
metadata: BetaMetadataParam | Omit = omit,
|
|
2577
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
2396
2578
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
2397
2579
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
2398
2580
|
stream: Literal[False] | Literal[True] | Omit = omit,
|
|
@@ -2411,6 +2593,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2411
2593
|
extra_body: Body | None = None,
|
|
2412
2594
|
timeout: float | httpx.Timeout | None | NotGiven = not_given,
|
|
2413
2595
|
) -> BetaMessage | AsyncStream[BetaRawMessageStreamEvent]:
|
|
2596
|
+
validate_output_format(output_format)
|
|
2597
|
+
|
|
2414
2598
|
if not stream and not is_given(timeout) and self._client.timeout == DEFAULT_TIMEOUT:
|
|
2415
2599
|
timeout = self._client._calculate_nonstreaming_timeout(
|
|
2416
2600
|
max_tokens, MODEL_NONSTREAMING_TOKENS.get(model, None)
|
|
@@ -2438,6 +2622,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2438
2622
|
"context_management": context_management,
|
|
2439
2623
|
"mcp_servers": mcp_servers,
|
|
2440
2624
|
"metadata": metadata,
|
|
2625
|
+
"output_format": output_format,
|
|
2441
2626
|
"service_tier": service_tier,
|
|
2442
2627
|
"stop_sequences": stop_sequences,
|
|
2443
2628
|
"stream": stream,
|
|
@@ -2461,6 +2646,121 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2461
2646
|
stream_cls=AsyncStream[BetaRawMessageStreamEvent],
|
|
2462
2647
|
)
|
|
2463
2648
|
|
|
2649
|
+
async def parse(
|
|
2650
|
+
self,
|
|
2651
|
+
*,
|
|
2652
|
+
max_tokens: int,
|
|
2653
|
+
messages: Iterable[BetaMessageParam],
|
|
2654
|
+
model: ModelParam,
|
|
2655
|
+
container: Optional[message_create_params.Container] | Omit = omit,
|
|
2656
|
+
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
2657
|
+
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
2658
|
+
metadata: BetaMetadataParam | Omit = omit,
|
|
2659
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
2660
|
+
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
2661
|
+
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
2662
|
+
stream: Literal[False] | Literal[True] | Omit = omit,
|
|
2663
|
+
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
2664
|
+
temperature: float | Omit = omit,
|
|
2665
|
+
thinking: BetaThinkingConfigParam | Omit = omit,
|
|
2666
|
+
tool_choice: BetaToolChoiceParam | Omit = omit,
|
|
2667
|
+
tools: Iterable[BetaToolUnionParam] | Omit = omit,
|
|
2668
|
+
top_k: int | Omit = omit,
|
|
2669
|
+
top_p: float | Omit = omit,
|
|
2670
|
+
betas: List[AnthropicBetaParam] | Omit = omit,
|
|
2671
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
2672
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
2673
|
+
extra_headers: Headers | None = None,
|
|
2674
|
+
extra_query: Query | None = None,
|
|
2675
|
+
extra_body: Body | None = None,
|
|
2676
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
2677
|
+
) -> ParsedBetaMessage[ResponseFormatT]:
|
|
2678
|
+
if not stream and not is_given(timeout) and self._client.timeout == DEFAULT_TIMEOUT:
|
|
2679
|
+
timeout = self._client._calculate_nonstreaming_timeout(
|
|
2680
|
+
max_tokens, MODEL_NONSTREAMING_TOKENS.get(model, None)
|
|
2681
|
+
)
|
|
2682
|
+
|
|
2683
|
+
if model in DEPRECATED_MODELS:
|
|
2684
|
+
warnings.warn(
|
|
2685
|
+
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.",
|
|
2686
|
+
DeprecationWarning,
|
|
2687
|
+
stacklevel=3,
|
|
2688
|
+
)
|
|
2689
|
+
betas = [beta for beta in betas] if is_given(betas) else []
|
|
2690
|
+
|
|
2691
|
+
if "structured-outputs-2025-09-17" not in betas:
|
|
2692
|
+
# Ensure structured outputs beta is included for parse method
|
|
2693
|
+
betas.append("structured-outputs-2025-09-17")
|
|
2694
|
+
|
|
2695
|
+
extra_headers = {
|
|
2696
|
+
"X-Stainless-Helper": "beta.messages.parse",
|
|
2697
|
+
**strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else NOT_GIVEN}),
|
|
2698
|
+
**(extra_headers or {}),
|
|
2699
|
+
}
|
|
2700
|
+
|
|
2701
|
+
transformed_output_format: Optional[message_create_params.OutputFormat] | NotGiven = NOT_GIVEN
|
|
2702
|
+
|
|
2703
|
+
if is_given(output_format) and output_format is not None:
|
|
2704
|
+
adapted_type: TypeAdapter[ResponseFormatT] = TypeAdapter(output_format)
|
|
2705
|
+
|
|
2706
|
+
try:
|
|
2707
|
+
schema = adapted_type.json_schema()
|
|
2708
|
+
transformed_output_format = message_create_params.OutputFormat(
|
|
2709
|
+
schema=transform_schema(schema), type="json_schema"
|
|
2710
|
+
)
|
|
2711
|
+
except pydantic.errors.PydanticSchemaGenerationError as e:
|
|
2712
|
+
raise TypeError(
|
|
2713
|
+
(
|
|
2714
|
+
"Could not generate JSON schema for the given `output_format` type. "
|
|
2715
|
+
"Use a type that works with `pydanitc.TypeAdapter`"
|
|
2716
|
+
)
|
|
2717
|
+
) from e
|
|
2718
|
+
|
|
2719
|
+
def parser(response: BetaMessage) -> ParsedBetaMessage[ResponseFormatT]:
|
|
2720
|
+
return parse_response(
|
|
2721
|
+
response=response,
|
|
2722
|
+
output_format=cast(
|
|
2723
|
+
ResponseFormatT,
|
|
2724
|
+
output_format if is_given(output_format) and output_format is not None else NOT_GIVEN,
|
|
2725
|
+
),
|
|
2726
|
+
)
|
|
2727
|
+
|
|
2728
|
+
return await self._post(
|
|
2729
|
+
"/v1/messages?beta=true",
|
|
2730
|
+
body=maybe_transform(
|
|
2731
|
+
{
|
|
2732
|
+
"max_tokens": max_tokens,
|
|
2733
|
+
"messages": messages,
|
|
2734
|
+
"model": model,
|
|
2735
|
+
"container": container,
|
|
2736
|
+
"context_management": context_management,
|
|
2737
|
+
"mcp_servers": mcp_servers,
|
|
2738
|
+
"metadata": metadata,
|
|
2739
|
+
"output_format": transformed_output_format,
|
|
2740
|
+
"service_tier": service_tier,
|
|
2741
|
+
"stop_sequences": stop_sequences,
|
|
2742
|
+
"stream": stream,
|
|
2743
|
+
"system": system,
|
|
2744
|
+
"temperature": temperature,
|
|
2745
|
+
"thinking": thinking,
|
|
2746
|
+
"tool_choice": tool_choice,
|
|
2747
|
+
"tools": tools,
|
|
2748
|
+
"top_k": top_k,
|
|
2749
|
+
"top_p": top_p,
|
|
2750
|
+
},
|
|
2751
|
+
message_create_params.MessageCreateParamsNonStreaming,
|
|
2752
|
+
),
|
|
2753
|
+
options=make_request_options(
|
|
2754
|
+
extra_headers=extra_headers,
|
|
2755
|
+
extra_query=extra_query,
|
|
2756
|
+
extra_body=extra_body,
|
|
2757
|
+
timeout=timeout,
|
|
2758
|
+
post_parser=parser,
|
|
2759
|
+
),
|
|
2760
|
+
cast_to=cast(Type[ParsedBetaMessage[ResponseFormatT]], BetaMessage),
|
|
2761
|
+
stream=False,
|
|
2762
|
+
)
|
|
2763
|
+
|
|
2464
2764
|
@overload
|
|
2465
2765
|
def tool_runner(
|
|
2466
2766
|
self,
|
|
@@ -2474,6 +2774,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2474
2774
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
2475
2775
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
2476
2776
|
metadata: BetaMetadataParam | Omit = omit,
|
|
2777
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
2477
2778
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
2478
2779
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
2479
2780
|
stream: Literal[False] | Omit = omit,
|
|
@@ -2490,7 +2791,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2490
2791
|
extra_query: Query | None = None,
|
|
2491
2792
|
extra_body: Body | None = None,
|
|
2492
2793
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
2493
|
-
) -> BetaAsyncToolRunner: ...
|
|
2794
|
+
) -> BetaAsyncToolRunner[ResponseFormatT]: ...
|
|
2494
2795
|
|
|
2495
2796
|
@overload
|
|
2496
2797
|
def tool_runner(
|
|
@@ -2506,6 +2807,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2506
2807
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
2507
2808
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
2508
2809
|
metadata: BetaMetadataParam | Omit = omit,
|
|
2810
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
2509
2811
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
2510
2812
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
2511
2813
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
@@ -2521,7 +2823,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2521
2823
|
extra_query: Query | None = None,
|
|
2522
2824
|
extra_body: Body | None = None,
|
|
2523
2825
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
2524
|
-
) -> BetaAsyncStreamingToolRunner: ...
|
|
2826
|
+
) -> BetaAsyncStreamingToolRunner[ResponseFormatT]: ...
|
|
2525
2827
|
|
|
2526
2828
|
@overload
|
|
2527
2829
|
def tool_runner(
|
|
@@ -2537,6 +2839,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2537
2839
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
2538
2840
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
2539
2841
|
metadata: BetaMetadataParam | Omit = omit,
|
|
2842
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
2540
2843
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
2541
2844
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
2542
2845
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
@@ -2552,7 +2855,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2552
2855
|
extra_query: Query | None = None,
|
|
2553
2856
|
extra_body: Body | None = None,
|
|
2554
2857
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
2555
|
-
) -> BetaAsyncStreamingToolRunner | BetaAsyncToolRunner: ...
|
|
2858
|
+
) -> BetaAsyncStreamingToolRunner[ResponseFormatT] | BetaAsyncToolRunner[ResponseFormatT]: ...
|
|
2556
2859
|
|
|
2557
2860
|
def tool_runner(
|
|
2558
2861
|
self,
|
|
@@ -2566,9 +2869,10 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2566
2869
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
2567
2870
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
2568
2871
|
metadata: BetaMetadataParam | Omit = omit,
|
|
2872
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
2569
2873
|
service_tier: Literal["auto", "standard_only"] | Omit = omit,
|
|
2570
2874
|
stop_sequences: SequenceNotStr[str] | Omit = omit,
|
|
2571
|
-
stream: Literal[True] | Literal[False] | Omit =
|
|
2875
|
+
stream: Literal[True] | Literal[False] | Omit = omit,
|
|
2572
2876
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
2573
2877
|
temperature: float | Omit = omit,
|
|
2574
2878
|
top_k: int | Omit = omit,
|
|
@@ -2582,7 +2886,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2582
2886
|
extra_query: Query | None = None,
|
|
2583
2887
|
extra_body: Body | None = None,
|
|
2584
2888
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
2585
|
-
) -> BetaAsyncToolRunner | BetaAsyncStreamingToolRunner:
|
|
2889
|
+
) -> BetaAsyncToolRunner[ResponseFormatT] | BetaAsyncStreamingToolRunner[ResponseFormatT]:
|
|
2586
2890
|
"""Create a Message stream"""
|
|
2587
2891
|
if model in DEPRECATED_MODELS:
|
|
2588
2892
|
warnings.warn(
|
|
@@ -2598,7 +2902,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2598
2902
|
}
|
|
2599
2903
|
|
|
2600
2904
|
params = cast(
|
|
2601
|
-
message_create_params.
|
|
2905
|
+
message_create_params.ParseMessageCreateParamsBase[ResponseFormatT],
|
|
2602
2906
|
{
|
|
2603
2907
|
"max_tokens": max_tokens,
|
|
2604
2908
|
"messages": messages,
|
|
@@ -2607,6 +2911,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2607
2911
|
"context_management": context_management,
|
|
2608
2912
|
"mcp_servers": mcp_servers,
|
|
2609
2913
|
"metadata": metadata,
|
|
2914
|
+
"output_format": output_format,
|
|
2610
2915
|
"service_tier": service_tier,
|
|
2611
2916
|
"stop_sequences": stop_sequences,
|
|
2612
2917
|
"system": system,
|
|
@@ -2620,7 +2925,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2620
2925
|
)
|
|
2621
2926
|
|
|
2622
2927
|
if stream:
|
|
2623
|
-
return BetaAsyncStreamingToolRunner(
|
|
2928
|
+
return BetaAsyncStreamingToolRunner[ResponseFormatT](
|
|
2624
2929
|
tools=tools,
|
|
2625
2930
|
params=params,
|
|
2626
2931
|
options={
|
|
@@ -2632,7 +2937,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2632
2937
|
client=cast("AsyncAnthropic", self._client),
|
|
2633
2938
|
max_iterations=max_iterations if is_given(max_iterations) else None,
|
|
2634
2939
|
)
|
|
2635
|
-
return BetaAsyncToolRunner(
|
|
2940
|
+
return BetaAsyncToolRunner[ResponseFormatT](
|
|
2636
2941
|
tools=tools,
|
|
2637
2942
|
params=params,
|
|
2638
2943
|
options={
|
|
@@ -2652,6 +2957,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2652
2957
|
messages: Iterable[BetaMessageParam],
|
|
2653
2958
|
model: ModelParam,
|
|
2654
2959
|
metadata: BetaMetadataParam | Omit = omit,
|
|
2960
|
+
output_format: Optional[type[ResponseFormatT]] | Omit = omit,
|
|
2655
2961
|
container: Optional[message_create_params.Container] | Omit = omit,
|
|
2656
2962
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
2657
2963
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
@@ -2671,7 +2977,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2671
2977
|
extra_query: Query | None = None,
|
|
2672
2978
|
extra_body: Body | None = None,
|
|
2673
2979
|
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
2674
|
-
) -> BetaAsyncMessageStreamManager:
|
|
2980
|
+
) -> BetaAsyncMessageStreamManager[ResponseFormatT]:
|
|
2675
2981
|
if model in DEPRECATED_MODELS:
|
|
2676
2982
|
warnings.warn(
|
|
2677
2983
|
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.",
|
|
@@ -2684,6 +2990,24 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2684
2990
|
**strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else NOT_GIVEN}),
|
|
2685
2991
|
**(extra_headers or {}),
|
|
2686
2992
|
}
|
|
2993
|
+
|
|
2994
|
+
transformed_output_format: Optional[message_create_params.OutputFormat] | NotGiven = NOT_GIVEN
|
|
2995
|
+
|
|
2996
|
+
if is_given(output_format) and output_format is not None:
|
|
2997
|
+
adapted_type: TypeAdapter[ResponseFormatT] = TypeAdapter(output_format)
|
|
2998
|
+
|
|
2999
|
+
try:
|
|
3000
|
+
schema = adapted_type.json_schema()
|
|
3001
|
+
transformed_output_format = message_create_params.OutputFormat(
|
|
3002
|
+
schema=transform_schema(schema), type="json_schema"
|
|
3003
|
+
)
|
|
3004
|
+
except pydantic.errors.PydanticSchemaGenerationError as e:
|
|
3005
|
+
raise TypeError(
|
|
3006
|
+
(
|
|
3007
|
+
"Could not generate JSON schema for the given `output_format` type. "
|
|
3008
|
+
"Use a type that works with `pydanitc.TypeAdapter`"
|
|
3009
|
+
)
|
|
3010
|
+
) from e
|
|
2687
3011
|
request = self._post(
|
|
2688
3012
|
"/v1/messages",
|
|
2689
3013
|
body=maybe_transform(
|
|
@@ -2692,6 +3016,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2692
3016
|
"messages": messages,
|
|
2693
3017
|
"model": model,
|
|
2694
3018
|
"metadata": metadata,
|
|
3019
|
+
"output_format": transformed_output_format,
|
|
2695
3020
|
"container": container,
|
|
2696
3021
|
"context_management": context_management,
|
|
2697
3022
|
"mcp_servers": mcp_servers,
|
|
@@ -2715,7 +3040,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2715
3040
|
stream=True,
|
|
2716
3041
|
stream_cls=AsyncStream[BetaRawMessageStreamEvent],
|
|
2717
3042
|
)
|
|
2718
|
-
return BetaAsyncMessageStreamManager(request)
|
|
3043
|
+
return BetaAsyncMessageStreamManager(request, output_format=cast(ResponseFormatT, output_format))
|
|
2719
3044
|
|
|
2720
3045
|
async def count_tokens(
|
|
2721
3046
|
self,
|
|
@@ -2724,6 +3049,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2724
3049
|
model: ModelParam,
|
|
2725
3050
|
context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
|
|
2726
3051
|
mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
|
|
3052
|
+
output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
|
|
2727
3053
|
system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
|
|
2728
3054
|
thinking: BetaThinkingConfigParam | Omit = omit,
|
|
2729
3055
|
tool_choice: BetaToolChoiceParam | Omit = omit,
|
|
@@ -2743,7 +3069,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2743
3069
|
including tools, images, and documents, without creating it.
|
|
2744
3070
|
|
|
2745
3071
|
Learn more about token counting in our
|
|
2746
|
-
[user guide](/en/docs/build-with-claude/token-counting)
|
|
3072
|
+
[user guide](https://docs.claude.com/en/docs/build-with-claude/token-counting)
|
|
2747
3073
|
|
|
2748
3074
|
Args:
|
|
2749
3075
|
messages: Input messages.
|
|
@@ -2823,6 +3149,8 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2823
3149
|
|
|
2824
3150
|
mcp_servers: MCP servers to be utilized in this request
|
|
2825
3151
|
|
|
3152
|
+
output_format: A schema to specify Claude's output format in responses.
|
|
3153
|
+
|
|
2826
3154
|
system: System prompt.
|
|
2827
3155
|
|
|
2828
3156
|
A system prompt is a way of providing context and instructions to Claude, such
|
|
@@ -2947,6 +3275,7 @@ class AsyncMessages(AsyncAPIResource):
|
|
|
2947
3275
|
"model": model,
|
|
2948
3276
|
"context_management": context_management,
|
|
2949
3277
|
"mcp_servers": mcp_servers,
|
|
3278
|
+
"output_format": output_format,
|
|
2950
3279
|
"system": system,
|
|
2951
3280
|
"thinking": thinking,
|
|
2952
3281
|
"tool_choice": tool_choice,
|
|
@@ -3023,3 +3352,10 @@ class AsyncMessagesWithStreamingResponse:
|
|
|
3023
3352
|
@cached_property
|
|
3024
3353
|
def batches(self) -> AsyncBatchesWithStreamingResponse:
|
|
3025
3354
|
return AsyncBatchesWithStreamingResponse(self._messages.batches)
|
|
3355
|
+
|
|
3356
|
+
|
|
3357
|
+
def validate_output_format(output_format: object) -> None:
|
|
3358
|
+
if inspect.isclass(output_format) and issubclass(output_format, pydantic.BaseModel):
|
|
3359
|
+
raise TypeError(
|
|
3360
|
+
"You tried to pass a `BaseModel` class to `beta.messages.create()`; You must use `beta.messages.parse()` instead"
|
|
3361
|
+
)
|