diff --git a/src/openai/types/chat/__init__.py b/src/openai/types/chat/__init__.py index c9e77ff41c..a47e19f288 100644 --- a/src/openai/types/chat/__init__.py +++ b/src/openai/types/chat/__init__.py @@ -54,6 +54,7 @@ ) from .chat_completion_message_tool_call_param import ( ChatCompletionMessageToolCallParam as ChatCompletionMessageToolCallParam, + ChatCompletionMessageToolCallParamType as ChatCompletionMessageToolCallParamType, ) from .chat_completion_named_tool_choice_param import ( ChatCompletionNamedToolChoiceParam as ChatCompletionNamedToolChoiceParam, diff --git a/src/openai/types/chat/chat_completion_message_tool_call_param.py b/src/openai/types/chat/chat_completion_message_tool_call_param.py index 96ba6521f0..b4311915f3 100644 --- a/src/openai/types/chat/chat_completion_message_tool_call_param.py +++ b/src/openai/types/chat/chat_completion_message_tool_call_param.py @@ -2,14 +2,63 @@ from __future__ import annotations -from typing import Union -from typing_extensions import TypeAlias +from typing import Any, Union, overload +from typing_extensions import Literal, TypeAlias -from .chat_completion_message_custom_tool_call_param import ChatCompletionMessageCustomToolCallParam -from .chat_completion_message_function_tool_call_param import ChatCompletionMessageFunctionToolCallParam +from .chat_completion_message_custom_tool_call_param import ChatCompletionMessageCustomToolCallParam, Custom +from .chat_completion_message_function_tool_call_param import ChatCompletionMessageFunctionToolCallParam, Function -__all__ = ["ChatCompletionMessageToolCallParam"] +__all__ = ["ChatCompletionMessageToolCallParam", "ChatCompletionMessageToolCallParamType"] -ChatCompletionMessageToolCallParam: TypeAlias = Union[ + +class _ChatCompletionMessageToolCallParamConstructor: + """ + Constructor class that provides backward compatibility for ChatCompletionMessageToolCallParam. + This allows instantiation while maintaining the Union type behavior. + """ + + @overload + def __call__( + self, + *, + id: str, + type: Literal["function"], + function: Function, + **kwargs: Any, + ) -> ChatCompletionMessageFunctionToolCallParam: ... + + @overload + def __call__( + self, + *, + id: str, + type: Literal["custom"], + custom: Custom, + **kwargs: Any, + ) -> ChatCompletionMessageCustomToolCallParam: ... + + def __call__(self, **kwargs: Any) -> Union[ChatCompletionMessageFunctionToolCallParam, ChatCompletionMessageCustomToolCallParam]: + from typing import cast + + tool_type = kwargs.get("type") + + if tool_type == "function": + return cast(ChatCompletionMessageFunctionToolCallParam, kwargs) + elif tool_type == "custom": + return cast(ChatCompletionMessageCustomToolCallParam, kwargs) + else: + # Default to function for backward compatibility (pre-1.99.2 behavior) + if "function" in kwargs and "type" not in kwargs: + kwargs["type"] = "function" + return cast(ChatCompletionMessageFunctionToolCallParam, kwargs) + + raise ValueError(f"Invalid tool call type: {tool_type}. Expected 'function' or 'custom'.") + + +# Create an instance that can be called like a constructor +ChatCompletionMessageToolCallParam = _ChatCompletionMessageToolCallParamConstructor() + +# Also create the type alias for static type checking +ChatCompletionMessageToolCallParamType: TypeAlias = Union[ ChatCompletionMessageFunctionToolCallParam, ChatCompletionMessageCustomToolCallParam ]