Skip to content

fix(types): make ChatCompletionMessageToolCallParam instantiable #2543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/openai/types/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
]