From 5d51777b26a9eee921d0c8e2fa062bcc0f07daee Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Fri, 6 Nov 2026 23:54:35 -0400 Subject: [PATCH] Python: count Vertex AI thinking tokens in completion_tokens usage --- .../services/vertex_ai_chat_completion.py | 4 +++- .../services/vertex_ai_text_completion.py | 4 +++- .../services/test_vertex_ai_chat_completion.py | 15 +++++++++++++++ .../services/test_vertex_ai_text_completion.py | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_chat_completion.py b/python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_chat_completion.py index 4cff1811c2af..40245cbe5293 100644 --- a/python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_chat_completion.py +++ b/python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_chat_completion.py @@ -355,7 +355,9 @@ def _get_metadata_from_response(self, response: GenerationResponse) -> dict[str, "prompt_feedback": response.prompt_feedback, "usage": CompletionUsage( prompt_tokens=response.usage_metadata.prompt_token_count, - completion_tokens=response.usage_metadata.candidates_token_count, + completion_tokens=( + response.usage_metadata.candidates_token_count + response.usage_metadata.thoughts_token_count + ), ), } diff --git a/python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_text_completion.py b/python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_text_completion.py index aa47ebc055f9..a5587a70e0cd 100644 --- a/python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_text_completion.py +++ b/python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_text_completion.py @@ -191,7 +191,9 @@ def _get_metadata_from_response(self, response: GenerationResponse) -> dict[str, "prompt_feedback": response.prompt_feedback, "usage": CompletionUsage( prompt_tokens=response.usage_metadata.prompt_token_count, - completion_tokens=response.usage_metadata.candidates_token_count, + completion_tokens=( + response.usage_metadata.candidates_token_count + response.usage_metadata.thoughts_token_count + ), ), } diff --git a/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_chat_completion.py b/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_chat_completion.py index fae4ee28d4fe..f53bc40d86e6 100644 --- a/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_chat_completion.py +++ b/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_chat_completion.py @@ -543,3 +543,18 @@ def test_create_streaming_chat_message_content_without_thought_signature(vertex_ # endregion thought_signature deserialization tests + + +def test_vertex_ai_chat_completion_usage_counts_thinking_tokens(vertex_ai_unit_test_env) -> None: + from google.cloud.aiplatform_v1beta1.types.prediction_service import GenerateContentResponse + from vertexai.generative_models import GenerationResponse + + response = GenerationResponse._from_gapic( + GenerateContentResponse( + usage_metadata=GenerateContentResponse.UsageMetadata( + prompt_token_count=5, candidates_token_count=10, thoughts_token_count=40, total_token_count=55 + ) + ) + ) + usage = VertexAIChatCompletion()._get_metadata_from_response(response)["usage"] + assert (usage.prompt_tokens, usage.completion_tokens) == (5, 50) diff --git a/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_text_completion.py b/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_text_completion.py index 4894aca5282b..589f45a5e0d0 100644 --- a/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_text_completion.py +++ b/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_text_completion.py @@ -128,3 +128,18 @@ async def test_vertex_ai_streaming_text_completion( # endregion streaming text completion + + +def test_vertex_ai_text_completion_usage_counts_thinking_tokens(vertex_ai_unit_test_env) -> None: + from google.cloud.aiplatform_v1beta1.types.prediction_service import GenerateContentResponse + from vertexai.generative_models import GenerationResponse + + response = GenerationResponse._from_gapic( + GenerateContentResponse( + usage_metadata=GenerateContentResponse.UsageMetadata( + prompt_token_count=5, candidates_token_count=10, thoughts_token_count=40, total_token_count=55 + ) + ) + ) + usage = VertexAITextCompletion()._get_metadata_from_response(response)["usage"] + assert (usage.prompt_tokens, usage.completion_tokens) == (5, 50)