Skip to content

Fix: resolve union type handling for function call tool parameters in chat completions #1421

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
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
28 changes: 20 additions & 8 deletions src/agents/models/chatcmpl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
ChatCompletionToolChoiceOptionParam,
ChatCompletionToolMessageParam,
ChatCompletionUserMessageParam,
ChatCompletionMessageFunctionToolCallParam,
ChatCompletionMessageCustomToolCallParam,
)
from openai.types.chat.chat_completion_content_part_param import File, FileFile
from openai.types.chat.chat_completion_tool_param import ChatCompletionToolParam
Expand Down Expand Up @@ -440,14 +442,24 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
asst = ensure_assistant_message()
tool_calls = list(asst.get("tool_calls", []))
arguments = func_call["arguments"] if func_call["arguments"] else "{}"
new_tool_call = ChatCompletionMessageToolCallParam(
id=func_call["call_id"],
type="function",
function={
"name": func_call["name"],
"arguments": arguments,
},
)
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
# 5) function call output => tool message
Expand Down