A programming framework for agentic AI
Fix completion tokens none issue 6352 (#6665)
Tejas Dharani11b7743b7d7b
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 c6f3d11be..5cca01e3d 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@@ -625,10 +625,12 @@ class BaseOpenAIChatCompletionClient(ChatCompletionClient):if create_params.response_format is not None:result = cast(ParsedChatCompletion[Any], result)+ # Handle the case where OpenAI API might return None for token counts+ # even when result.usage is not Noneusage = RequestUsage(# TODO backup token counting- prompt_tokens=result.usage.prompt_tokens if result.usage is not None else 0,- completion_tokens=(result.usage.completion_tokens if result.usage is not None else 0),+ prompt_tokens=getattr(result.usage, "prompt_tokens", 0) if result.usage is not None else 0,+ completion_tokens=getattr(result.usage, "completion_tokens", 0) if result.usage is not None else 0,)logger.info(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 d45be4a03..72635a706 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@@ -367,9 +367,39 @@ async def test_openai_chat_completion_client_create_stream_no_usage_explicit(monassert chunks[0] == "Hello"assert chunks[1] == " Another Hello"assert chunks[2] == " Yet Another Hello"- assert isinstance(chunks[-1], CreateResult)- assert chunks[-1].content == "Hello Another Hello Yet Another Hello"- assert chunks[-1].usage == RequestUsage(prompt_tokens=0, completion_tokens=0)+++async def test_openai_chat_completion_client_none_usage(monkeypatch: pytest.MonkeyPatch) -> None:+ """Test that completion_tokens and prompt_tokens handle None usage correctly.++ This test addresses issue #6352 where result.usage could be None,+ causing TypeError in logging when trying to access completion_tokens.+ """++ async def _mock_create_with_none_usage(*args: Any, **kwargs: Any) -> ChatCompletion:+ await asyncio.sleep(0.1)+ # Create a ChatCompletion with None usage (which can happen in some API scenarios)+ return ChatCompletion(+ id="id",+ choices=[+ Choice(finish_reason="stop", index=0, message=ChatCompletionMessage(content="Hello", role="assistant"))+ ],+ created=0,+ model="gpt-4o",+ object="chat.completion",+ usage=None, # This is the scenario from the issue+ )++ monkeypatch.setattr(AsyncCompletions, "create", _mock_create_with_none_usage)+ client = OpenAIChatCompletionClient(model="gpt-4o", api_key="api_key")++ # This should not raise a TypeError+ result = await client.create(messages=[UserMessage(content="Hello", source="user")])++ # Verify that the usage is correctly set to 0 when usage is None+ assert result.usage.prompt_tokens == 0+ assert result.usage.completion_tokens == 0@pytest.mark.asyncio