A programming framework for agentic AI
fix: Enhance OpenAI client to handle additional stop reasons and improve tool call validation in tests to address empty tool_calls list. (#5223)
Eric Zhub441d5b43a2d
diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.pyindex 79c13442c..131e7288a 100644--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py@@ -336,6 +336,10 @@ def normalize_stop_reason(stop_reason: str | None) -> FinishReasons:stop_reason = stop_reason.lower()KNOWN_STOP_MAPPINGS: Dict[str, FinishReasons] = {+ "stop": "stop",+ "length": "length",+ "content_filter": "content_filter",+ "function_calls": "function_calls","end_turn": "stop","tool_calls": "function_calls",}@@ -552,7 +556,7 @@ class BaseOpenAIChatCompletionClient(ChatCompletionClient):content: Union[str, List[FunctionCall]]if choice.message.function_call is not None:raise ValueError("function_call is deprecated and is not supported by this model client.")- elif choice.message.tool_calls is not None:+ elif choice.message.tool_calls is not None and len(choice.message.tool_calls) > 0:if choice.finish_reason != "tool_calls":warnings.warn(f"Finish reason mismatch: {choice.finish_reason} != tool_calls "diff --git a/python/packages/autogen-ext/tests/models/test_openai_model_client.py b/python/packages/autogen-ext/tests/models/test_openai_model_client.pyindex d629cdc42..0e568d45a 100644--- a/python/packages/autogen-ext/tests/models/test_openai_model_client.py+++ b/python/packages/autogen-ext/tests/models/test_openai_model_client.py@@ -600,6 +600,25 @@ async def test_tool_calling(monkeypatch: pytest.MonkeyPatch) -> None:object="chat.completion",usage=CompletionUsage(prompt_tokens=10, completion_tokens=5, total_tokens=0),),+ # Should not be returning tool calls when the tool_calls are empty+ ChatCompletion(+ id="id5",+ choices=[+ Choice(+ finish_reason="stop",+ index=0,+ message=ChatCompletionMessage(+ content="I should make a tool call.",+ tool_calls=[],+ role="assistant",+ ),+ )+ ],+ created=0,+ model=model,+ object="chat.completion",+ usage=CompletionUsage(prompt_tokens=10, completion_tokens=5, total_tokens=0),+ ),]mock = _MockChatCompletion(chat_completions)monkeypatch.setattr(AsyncCompletions, "create", mock.mock_create)@@ -652,6 +671,11 @@ async def test_tool_calling(monkeypatch: pytest.MonkeyPatch) -> None:assert create_result.content == [FunctionCall(id="1", arguments=r'{"input": "task"}', name="_pass_function")]assert create_result.finish_reason == "function_calls"+ # Should not be returning tool calls when the tool_calls are empty+ create_result = await model_client.create(messages=[UserMessage(content="Hello", source="user")], tools=[pass_tool])+ assert create_result.content == "I should make a tool call."+ assert create_result.finish_reason == "stop"+async def _test_model_client(model_client: OpenAIChatCompletionClient) -> None:# Test basic completion