Skip to content

Commit 008e327

Browse files
Revert tool_choice/parallel_tool_calls changes - must be removed when no tools
OpenAI API requires tool_choice and parallel_tool_calls to only be present when tools are specified. Restored the logic that removes these options when there are no tools. - Restored check in _chat_client.py to remove tool_choice and parallel_tool_calls when no tools present - Restored same logic in _responses_client.py - Reverted test to expect the correct behavior
1 parent 6d1f1ac commit 008e327

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

python/packages/core/agent_framework/openai/_chat_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ def _prepare_options(self, messages: Sequence[ChatMessage], options: Mapping[str
282282
tools = options.get("tools")
283283
if tools is not None:
284284
run_options.update(self._prepare_tools_for_openai(tools))
285-
if tool_choice := run_options.pop("tool_choice", None):
285+
# Only include tool_choice and parallel_tool_calls if tools are present
286+
if not run_options.get("tools"):
287+
run_options.pop("parallel_tool_calls", None)
288+
run_options.pop("tool_choice", None)
289+
elif tool_choice := run_options.pop("tool_choice", None):
286290
tool_mode = validate_tool_mode(tool_choice)
287291
if (mode := tool_mode.get("mode")) == "required" and (
288292
func_name := tool_mode.get("required_function_name")

python/packages/core/agent_framework/openai/_responses_client.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -577,19 +577,21 @@ async def _prepare_options(
577577
# tools
578578
if tools := self._prepare_tools_for_openai(options.get("tools")):
579579
run_options["tools"] = tools
580-
581-
# tool_choice: convert ToolMode to appropriate format (keep even if no tools)
582-
if tool_choice := options.get("tool_choice"):
583-
tool_mode = validate_tool_mode(tool_choice)
584-
if (mode := tool_mode.get("mode")) == "required" and (
585-
func_name := tool_mode.get("required_function_name")
586-
) is not None:
587-
run_options["tool_choice"] = {
588-
"type": "function",
589-
"name": func_name,
590-
}
591-
else:
592-
run_options["tool_choice"] = mode
580+
# tool_choice: convert ToolMode to appropriate format
581+
if tool_choice := options.get("tool_choice"):
582+
tool_mode = validate_tool_mode(tool_choice)
583+
if (mode := tool_mode.get("mode")) == "required" and (
584+
func_name := tool_mode.get("required_function_name")
585+
) is not None:
586+
run_options["tool_choice"] = {
587+
"type": "function",
588+
"name": func_name,
589+
}
590+
else:
591+
run_options["tool_choice"] = mode
592+
else:
593+
run_options.pop("parallel_tool_calls", None)
594+
run_options.pop("tool_choice", None)
593595

594596
# response format and text config
595597
response_format = options.get("response_format")

python/packages/core/tests/openai/test_openai_chat_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -890,17 +890,17 @@ def test_multiple_function_calls_in_single_message(openai_unit_test_env: dict[st
890890
assert prepared[0]["tool_calls"][1]["id"] == "call_2"
891891

892892

893-
def test_prepare_options_preserves_parallel_tool_calls_when_no_tools(openai_unit_test_env: dict[str, str]) -> None:
894-
"""Test that parallel_tool_calls is preserved even when no tools are present."""
893+
def test_prepare_options_removes_parallel_tool_calls_when_no_tools(openai_unit_test_env: dict[str, str]) -> None:
894+
"""Test that parallel_tool_calls is removed when no tools are present."""
895895
client = OpenAIChatClient()
896896

897897
messages = [ChatMessage(role="user", text="test")]
898898
options = {"allow_multiple_tool_calls": True}
899899

900900
prepared_options = client._prepare_options(messages, options)
901901

902-
# parallel_tool_calls is preserved even when no tools (consistent with tool_choice behavior)
903-
assert prepared_options.get("parallel_tool_calls") is True
902+
# Should not have parallel_tool_calls when no tools
903+
assert "parallel_tool_calls" not in prepared_options
904904

905905

906906
async def test_streaming_exception_handling(openai_unit_test_env: dict[str, str]) -> None:

0 commit comments

Comments
 (0)