From f6f3afe2e26ba004a24f7c3bdd36c7b4c1ae4697 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Mon, 7 Oct 2024 20:58:09 +0100 Subject: [PATCH] refactor(types): improve tool type names (#679) --- api.md | 4 ++ .../resources/beta/prompt_caching/messages.py | 17 +++---- src/anthropic/resources/messages.py | 21 ++++---- src/anthropic/types/__init__.py | 4 ++ .../prompt_caching/message_create_params.py | 48 +++++-------------- src/anthropic/types/message_create_params.py | 48 +++++-------------- src/anthropic/types/tool_choice_any_param.py | 18 +++++++ src/anthropic/types/tool_choice_auto_param.py | 18 +++++++ src/anthropic/types/tool_choice_param.py | 14 ++++++ src/anthropic/types/tool_choice_tool_param.py | 21 ++++++++ 10 files changed, 125 insertions(+), 88 deletions(-) create mode 100644 src/anthropic/types/tool_choice_any_param.py create mode 100644 src/anthropic/types/tool_choice_auto_param.py create mode 100644 src/anthropic/types/tool_choice_param.py create mode 100644 src/anthropic/types/tool_choice_tool_param.py diff --git a/api.md b/api.md index feb03537..307bce68 100644 --- a/api.md +++ b/api.md @@ -29,6 +29,10 @@ from anthropic.types import ( TextBlockParam, TextDelta, Tool, + ToolChoice, + ToolChoiceAny, + ToolChoiceAuto, + ToolChoiceTool, ToolResultBlockParam, ToolUseBlock, ToolUseBlockParam, diff --git a/src/anthropic/resources/beta/prompt_caching/messages.py b/src/anthropic/resources/beta/prompt_caching/messages.py index 1ce65e74..2223bf20 100644 --- a/src/anthropic/resources/beta/prompt_caching/messages.py +++ b/src/anthropic/resources/beta/prompt_caching/messages.py @@ -24,6 +24,7 @@ from ...._base_client import make_request_options from ....lib.streaming import PromptCachingBetaMessageStreamManager, AsyncPromptCachingBetaMessageStreamManager from ....types.model_param import ModelParam +from ....types.tool_choice_param import ToolChoiceParam from ....types.beta.prompt_caching import message_create_params from ....types.beta.prompt_caching.prompt_caching_beta_message import PromptCachingBetaMessage from ....types.beta.prompt_caching.prompt_caching_beta_tool_param import PromptCachingBetaToolParam @@ -68,7 +69,7 @@ def create( stream: Literal[False] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[PromptCachingBetaTextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[PromptCachingBetaToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -331,7 +332,7 @@ def create( stop_sequences: List[str] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[PromptCachingBetaTextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[PromptCachingBetaToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -594,7 +595,7 @@ def create( stop_sequences: List[str] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[PromptCachingBetaTextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[PromptCachingBetaToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -857,7 +858,7 @@ def create( stream: Literal[False] | Literal[True] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[PromptCachingBetaTextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[PromptCachingBetaToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -990,7 +991,7 @@ async def create( stream: Literal[False] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[PromptCachingBetaTextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[PromptCachingBetaToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -1253,7 +1254,7 @@ async def create( stop_sequences: List[str] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[PromptCachingBetaTextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[PromptCachingBetaToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -1516,7 +1517,7 @@ async def create( stop_sequences: List[str] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[PromptCachingBetaTextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[PromptCachingBetaToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -1779,7 +1780,7 @@ async def create( stream: Literal[False] | Literal[True] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[PromptCachingBetaTextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[PromptCachingBetaToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, diff --git a/src/anthropic/resources/messages.py b/src/anthropic/resources/messages.py index 1215a543..0d4c03fa 100644 --- a/src/anthropic/resources/messages.py +++ b/src/anthropic/resources/messages.py @@ -30,6 +30,7 @@ from ..types.model_param import ModelParam from ..types.message_param import MessageParam from ..types.text_block_param import TextBlockParam +from ..types.tool_choice_param import ToolChoiceParam from ..types.raw_message_stream_event import RawMessageStreamEvent __all__ = ["Messages", "AsyncMessages"] @@ -76,7 +77,7 @@ def create( stream: Literal[False] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[TextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -339,7 +340,7 @@ def create( stop_sequences: List[str] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[TextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -602,7 +603,7 @@ def create( stop_sequences: List[str] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[TextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -865,7 +866,7 @@ def create( stream: Literal[False] | Literal[True] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[TextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -925,7 +926,7 @@ def stream( temperature: float | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. @@ -1008,7 +1009,7 @@ async def create( stream: Literal[False] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[TextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -1271,7 +1272,7 @@ async def create( stop_sequences: List[str] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[TextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -1534,7 +1535,7 @@ async def create( stop_sequences: List[str] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[TextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -1797,7 +1798,7 @@ async def create( stream: Literal[False] | Literal[True] | NotGiven = NOT_GIVEN, system: Union[str, Iterable[TextBlockParam]] | NotGiven = NOT_GIVEN, temperature: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, @@ -1857,7 +1858,7 @@ def stream( temperature: float | NotGiven = NOT_GIVEN, top_k: int | NotGiven = NOT_GIVEN, top_p: float | NotGiven = NOT_GIVEN, - tool_choice: message_create_params.ToolChoice | NotGiven = NOT_GIVEN, + tool_choice: ToolChoiceParam | NotGiven = NOT_GIVEN, tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. diff --git a/src/anthropic/types/__init__.py b/src/anthropic/types/__init__.py index 9839b5a2..41b1634d 100644 --- a/src/anthropic/types/__init__.py +++ b/src/anthropic/types/__init__.py @@ -16,6 +16,7 @@ from .input_json_delta import InputJSONDelta as InputJSONDelta from .text_block_param import TextBlockParam as TextBlockParam from .image_block_param import ImageBlockParam as ImageBlockParam +from .tool_choice_param import ToolChoiceParam as ToolChoiceParam from .message_stop_event import MessageStopEvent as MessageStopEvent from .message_delta_event import MessageDeltaEvent as MessageDeltaEvent from .message_delta_usage import MessageDeltaUsage as MessageDeltaUsage @@ -23,7 +24,10 @@ from .message_stream_event import MessageStreamEvent as MessageStreamEvent from .tool_use_block_param import ToolUseBlockParam as ToolUseBlockParam from .message_create_params import MessageCreateParams as MessageCreateParams +from .tool_choice_any_param import ToolChoiceAnyParam as ToolChoiceAnyParam from .raw_message_stop_event import RawMessageStopEvent as RawMessageStopEvent +from .tool_choice_auto_param import ToolChoiceAutoParam as ToolChoiceAutoParam +from .tool_choice_tool_param import ToolChoiceToolParam as ToolChoiceToolParam from .raw_message_delta_event import RawMessageDeltaEvent as RawMessageDeltaEvent from .raw_message_start_event import RawMessageStartEvent as RawMessageStartEvent from .tool_result_block_param import ToolResultBlockParam as ToolResultBlockParam diff --git a/src/anthropic/types/beta/prompt_caching/message_create_params.py b/src/anthropic/types/beta/prompt_caching/message_create_params.py index 99012076..c164b457 100644 --- a/src/anthropic/types/beta/prompt_caching/message_create_params.py +++ b/src/anthropic/types/beta/prompt_caching/message_create_params.py @@ -6,6 +6,10 @@ from typing_extensions import Literal, Required, TypeAlias, TypedDict from ...model_param import ModelParam +from ...tool_choice_param import ToolChoiceParam +from ...tool_choice_any_param import ToolChoiceAnyParam +from ...tool_choice_auto_param import ToolChoiceAutoParam +from ...tool_choice_tool_param import ToolChoiceToolParam from .prompt_caching_beta_tool_param import PromptCachingBetaToolParam from .prompt_caching_beta_message_param import PromptCachingBetaMessageParam from .prompt_caching_beta_text_block_param import PromptCachingBetaTextBlockParam @@ -162,7 +166,7 @@ class MessageCreateParamsBase(TypedDict, total=False): deterministic. """ - tool_choice: ToolChoice + tool_choice: ToolChoiceParam """How the model should use the provided tools. The model can use a specific tool, any available tool, or decide by itself. @@ -272,43 +276,17 @@ class Metadata(TypedDict, total=False): """ -class ToolChoiceToolChoiceAuto(TypedDict, total=False): - type: Required[Literal["auto"]] +ToolChoice: TypeAlias = ToolChoiceParam +"""This is deprecated, `ToolChoiceParam` should be used instead""" - disable_parallel_tool_use: bool - """Whether to disable parallel tool use. - - Defaults to `false`. If set to `true`, the model will output at most one tool - use. - """ - - -class ToolChoiceToolChoiceAny(TypedDict, total=False): - type: Required[Literal["any"]] - - disable_parallel_tool_use: bool - """Whether to disable parallel tool use. - - Defaults to `false`. If set to `true`, the model will output exactly one tool - use. - """ - - -class ToolChoiceToolChoiceTool(TypedDict, total=False): - name: Required[str] - """The name of the tool to use.""" - - type: Required[Literal["tool"]] - - disable_parallel_tool_use: bool - """Whether to disable parallel tool use. - - Defaults to `false`. If set to `true`, the model will output exactly one tool - use. - """ +ToolChoiceToolChoiceAuto: TypeAlias = ToolChoiceAutoParam +"""This is deprecated, `ToolChoiceAutoParam` should be used instead""" +ToolChoiceToolChoiceAny: TypeAlias = ToolChoiceAnyParam +"""This is deprecated, `ToolChoiceAnyParam` should be used instead""" -ToolChoice: TypeAlias = Union[ToolChoiceToolChoiceAuto, ToolChoiceToolChoiceAny, ToolChoiceToolChoiceTool] +ToolChoiceToolChoiceTool: TypeAlias = ToolChoiceToolParam +"""This is deprecated, `ToolChoiceToolParam` should be used instead""" class MessageCreateParamsNonStreaming(MessageCreateParamsBase, total=False): diff --git a/src/anthropic/types/message_create_params.py b/src/anthropic/types/message_create_params.py index 482f2cfb..298e9702 100644 --- a/src/anthropic/types/message_create_params.py +++ b/src/anthropic/types/message_create_params.py @@ -9,6 +9,10 @@ from .model_param import ModelParam from .message_param import MessageParam from .text_block_param import TextBlockParam +from .tool_choice_param import ToolChoiceParam +from .tool_choice_any_param import ToolChoiceAnyParam +from .tool_choice_auto_param import ToolChoiceAutoParam +from .tool_choice_tool_param import ToolChoiceToolParam __all__ = [ "MessageCreateParamsBase", @@ -162,7 +166,7 @@ class MessageCreateParamsBase(TypedDict, total=False): deterministic. """ - tool_choice: ToolChoice + tool_choice: ToolChoiceParam """How the model should use the provided tools. The model can use a specific tool, any available tool, or decide by itself. @@ -272,43 +276,17 @@ class Metadata(TypedDict, total=False): """ -class ToolChoiceToolChoiceAuto(TypedDict, total=False): - type: Required[Literal["auto"]] +ToolChoice: TypeAlias = ToolChoiceParam +"""This is deprecated, `ToolChoiceParam` should be used instead""" - disable_parallel_tool_use: bool - """Whether to disable parallel tool use. - - Defaults to `false`. If set to `true`, the model will output at most one tool - use. - """ - - -class ToolChoiceToolChoiceAny(TypedDict, total=False): - type: Required[Literal["any"]] - - disable_parallel_tool_use: bool - """Whether to disable parallel tool use. - - Defaults to `false`. If set to `true`, the model will output exactly one tool - use. - """ - - -class ToolChoiceToolChoiceTool(TypedDict, total=False): - name: Required[str] - """The name of the tool to use.""" - - type: Required[Literal["tool"]] - - disable_parallel_tool_use: bool - """Whether to disable parallel tool use. - - Defaults to `false`. If set to `true`, the model will output exactly one tool - use. - """ +ToolChoiceToolChoiceAuto: TypeAlias = ToolChoiceAutoParam +"""This is deprecated, `ToolChoiceAutoParam` should be used instead""" +ToolChoiceToolChoiceAny: TypeAlias = ToolChoiceAnyParam +"""This is deprecated, `ToolChoiceAnyParam` should be used instead""" -ToolChoice: TypeAlias = Union[ToolChoiceToolChoiceAuto, ToolChoiceToolChoiceAny, ToolChoiceToolChoiceTool] +ToolChoiceToolChoiceTool: TypeAlias = ToolChoiceToolParam +"""This is deprecated, `ToolChoiceToolParam` should be used instead""" class MessageCreateParamsNonStreaming(MessageCreateParamsBase, total=False): diff --git a/src/anthropic/types/tool_choice_any_param.py b/src/anthropic/types/tool_choice_any_param.py new file mode 100644 index 00000000..a0a566ea --- /dev/null +++ b/src/anthropic/types/tool_choice_any_param.py @@ -0,0 +1,18 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing_extensions import Literal, Required, TypedDict + +__all__ = ["ToolChoiceAnyParam"] + + +class ToolChoiceAnyParam(TypedDict, total=False): + type: Required[Literal["any"]] + + disable_parallel_tool_use: bool + """Whether to disable parallel tool use. + + Defaults to `false`. If set to `true`, the model will output exactly one tool + use. + """ diff --git a/src/anthropic/types/tool_choice_auto_param.py b/src/anthropic/types/tool_choice_auto_param.py new file mode 100644 index 00000000..456f675c --- /dev/null +++ b/src/anthropic/types/tool_choice_auto_param.py @@ -0,0 +1,18 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing_extensions import Literal, Required, TypedDict + +__all__ = ["ToolChoiceAutoParam"] + + +class ToolChoiceAutoParam(TypedDict, total=False): + type: Required[Literal["auto"]] + + disable_parallel_tool_use: bool + """Whether to disable parallel tool use. + + Defaults to `false`. If set to `true`, the model will output at most one tool + use. + """ diff --git a/src/anthropic/types/tool_choice_param.py b/src/anthropic/types/tool_choice_param.py new file mode 100644 index 00000000..1ffd2358 --- /dev/null +++ b/src/anthropic/types/tool_choice_param.py @@ -0,0 +1,14 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing import Union +from typing_extensions import TypeAlias + +from .tool_choice_any_param import ToolChoiceAnyParam +from .tool_choice_auto_param import ToolChoiceAutoParam +from .tool_choice_tool_param import ToolChoiceToolParam + +__all__ = ["ToolChoiceParam"] + +ToolChoiceParam: TypeAlias = Union[ToolChoiceAutoParam, ToolChoiceAnyParam, ToolChoiceToolParam] diff --git a/src/anthropic/types/tool_choice_tool_param.py b/src/anthropic/types/tool_choice_tool_param.py new file mode 100644 index 00000000..aeec9966 --- /dev/null +++ b/src/anthropic/types/tool_choice_tool_param.py @@ -0,0 +1,21 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing_extensions import Literal, Required, TypedDict + +__all__ = ["ToolChoiceToolParam"] + + +class ToolChoiceToolParam(TypedDict, total=False): + name: Required[str] + """The name of the tool to use.""" + + type: Required[Literal["tool"]] + + disable_parallel_tool_use: bool + """Whether to disable parallel tool use. + + Defaults to `false`. If set to `true`, the model will output exactly one tool + use. + """