Skip to content

TypeError: Cannot instantiate typing.Union when running with latest openai Python package #1417

@mar-yam12

Description

@mar-yam12

Description

When using the latest openai Python package with the openai-agents SDK, calling Runner.run_sync() fails with:

TypeError: Cannot instantiate typing.Union

This happens because the ChatCompletionMessageToolCallParam type in openai is now a typing.Union instead of a concrete class, and the openai-agents code still attempts to instantiate it directly.


Environment

  • Python: 3.12.11
  • openai: 1.99.3 (also tested with 1.99.4)
  • openai-agents: latest from PyPI
  • OS: Windows 10 / 11

Steps to Reproduce

  1. Install latest packages:
    pip install --upgrade openai openai-agents
    
    

Root Cause Analysis

In recent openai releases (>=1.99.2), ChatCompletionMessageToolCallParam is no longer a concrete Pydantic model — it’s now defined as a typing.Union of multiple tool call param types, e.g.:

ChatCompletionMessageFunctionToolCallParam | ChatCompletionMessageCustomToolCallParam

The openai-agents SDK still attempts to instantiate ChatCompletionMessageToolCallParam(...) directly in chatcmpl_converter.py.

In Python, typing.Union is not callable, so this raises:

TypeError: Cannot instantiate typing.Union


Workarounds

  1. Pin openai to 1.99.1:
    pip install "openai==1.99.1"
    
    
  2. Modify chatcmpl_converter.py to:
  • Build a plain dict, or
  • Instantiate a specific member of the union, e.g.:
    ChatCompletionMessageFunctionToolCallParam(...)

instead of calling the union type alias.

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions