-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Description:
When running an agent with a tool call, the agents SDK crashes with:
TypeError: Cannot instantiate typing.Union
Python: 3.13
openai (version : 1.99.6)
openai-agents (version: 0.0.16 )
OS ( Windows:11)
Full Traceback (shortened):
File "...\agents\models\chatcmpl_converter.py", line 443, in items_to_messages
new_tool_call = ChatCompletionMessageToolCallParam(...)
TypeError: Cannot instantiate typing.Union
Root cause:
ChatCompletionMessageToolCallParam is defined in openai.types.chat as:
ChatCompletionMessageToolCallParam: TypeAlias = Union[
ChatCompletionMessageFunctionToolCallParam,
ChatCompletionMessageCustomToolCallParam
]
This is a TypeAlias for a Union, not a class.
The converter calls it as if it were a constructor, which is invalid in Python:
new_tool_call = ChatCompletionMessageToolCallParam(...) # ❌
This triggers the error because typing.Union is not instantiable.
Expected behavior:
The converter should inspect the data, determine whether it matches
ChatCompletionMessageFunctionToolCallParam or ChatCompletionMessageCustomToolCallParam,
and then instantiate the correct concrete type:
Solution:
elif func_call := cls.maybe_function_tool_call(item):
asst = ensure_assistant_message()
tool_calls = list(asst.get("tool_calls", []))
arguments = func_call["arguments"] if func_call["arguments"] else "{}"
if func_call.get("type") == "function_call":
new_tool_call = ChatCompletionMessageFunctionToolCallParam(
id=func_call["call_id"],
type="function",
function={
"name": func_call["name"],
"arguments": arguments,
},
)
else:
new_tool_call = ChatCompletionMessageCustomToolCallParam(
id=func_call["call_id"],
type="function",
custom={
"name": func_call["name"],
"arguments": arguments,
},
)
tool_calls.append(new_tool_call)
asst["tool_calls"] = tool_calls

Steps to reproduce:
Create an agent with Runner.run()
Add a tool call to the conversation.
Run and observe the crash when the model returns a tool call.
Additional note:
I’m happy to open a PR with a fix for this in the repo. Please let me know if that would help — I can contribute the change and relevant test cases.I have attached a photo which shows my code worked after doing the solution i provide.