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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ryx2
Copy link

@ryx2 ryx2 commented Aug 10, 2025

Summary

Fixes issue where ChatCompletionMessageToolCallParam could not be instantiated after version 1.99.2, causing TypeError: Cannot instantiate typing.Union.

This addresses:

Problem

In version 1.99.2, ChatCompletionMessageToolCallParam was changed from a concrete TypedDict class to a Union type alias to support custom tool calls. This broke backward compatibility for users who were directly instantiating the type.

Before (v1.99.1):

# This worked
tool_call = ChatCompletionMessageToolCallParam(
    id="test_id", 
    function={"name": "func", "arguments": "{}"}
)

After (v1.99.2+):

# This failed with "TypeError: Cannot instantiate typing.Union"
tool_call = ChatCompletionMessageToolCallParam(
    id="test_id", 
    function={"name": "func", "arguments": "{}"}
)

Solution

This change introduces a constructor class that can be instantiated while preserving the Union type behavior for type checking:

  • Instantiable: ChatCompletionMessageToolCallParam() now works again
  • Backward compatible: Pre-1.99.2 usage patterns continue to work
  • Type safe: Proper type hints with overloads for function and custom tools
  • Forward compatible: Supports both function and custom tool call types
  • Error handling: Clear error messages for invalid parameters

Changes Made

  1. Constructor class: _ChatCompletionMessageToolCallParamConstructor with proper overloads
  2. Type alias preservation: ChatCompletionMessageToolCallParamType for explicit typing
  3. Backward compatibility: Automatic type="function" inference for pre-1.99.2 patterns
  4. Export updates: Added new type alias to __init__.py

Test Plan

🤖 Generated with Claude Code

Fixes issue where ChatCompletionMessageToolCallParam could not be instantiated
after version 1.99.2, causing "TypeError: Cannot instantiate typing.Union".

The issue occurred when ChatCompletionMessageToolCallParam was changed from a
concrete TypedDict class to a Union type alias to support custom tool calls.

This change:
- Introduces a constructor class that can be instantiated
- Maintains backward compatibility with pre-1.99.2 usage patterns
- Supports both function and custom tool call types
- Provides proper type hints and error handling
- Adds ChatCompletionMessageToolCallParamType for explicit typing

Fixes openai#2541

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant