From 8f8572e63c528a22cf0fe767c7a0cc90555205fd Mon Sep 17 00:00:00 2001 From: x Date: Mon, 2 Nov 2026 22:24:58 -0500 Subject: [PATCH] Python: map Vertex AI RECITATION/BLOCKLIST/PROHIBITED_CONTENT/SPII finish reasons to CONTENT_FILTER The Vertex AI connector only mapped SAFETY to CONTENT_FILTER, so responses Gemini stopped for recitation, blocklist, prohibited content or SPII came back with finish_reason=None. Map them the same way the Google AI connector already does. --- .../ai/google/vertex_ai/services/utils.py | 12 +++++++++++- .../vertex_ai/services/test_vertex_ai_utils.py | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/python/semantic_kernel/connectors/ai/google/vertex_ai/services/utils.py b/python/semantic_kernel/connectors/ai/google/vertex_ai/services/utils.py index f027880e1faf..d97ca6d4b7b4 100644 --- a/python/semantic_kernel/connectors/ai/google/vertex_ai/services/utils.py +++ b/python/semantic_kernel/connectors/ai/google/vertex_ai/services/utils.py @@ -32,6 +32,16 @@ logger: logging.Logger = logging.getLogger(__name__) +# Finish reasons where Gemini stopped the response for the content, not for length or a natural end. +_VERTEX_AI_CONTENT_FILTER_REASONS = frozenset({ + Candidate.FinishReason.SAFETY, + Candidate.FinishReason.RECITATION, + Candidate.FinishReason.BLOCKLIST, + Candidate.FinishReason.PROHIBITED_CONTENT, + Candidate.FinishReason.SPII, +}) + + def finish_reason_from_vertex_ai_to_semantic_kernel( finish_reason: Candidate.FinishReason, ) -> SemanticKernelFinishReason | None: @@ -45,7 +55,7 @@ def finish_reason_from_vertex_ai_to_semantic_kernel( if finish_reason == Candidate.FinishReason.MAX_TOKENS: return SemanticKernelFinishReason.LENGTH - if finish_reason == Candidate.FinishReason.SAFETY: + if finish_reason in _VERTEX_AI_CONTENT_FILTER_REASONS: return SemanticKernelFinishReason.CONTENT_FILTER return None diff --git a/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_utils.py b/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_utils.py index f05ec0d11187..b8b9f52c2801 100644 --- a/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_utils.py +++ b/python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_utils.py @@ -30,6 +30,21 @@ def test_finish_reason_from_vertex_ai_to_semantic_kernel(): assert finish_reason_from_vertex_ai_to_semantic_kernel(Candidate.FinishReason.OTHER) is None +@pytest.mark.parametrize( + "vertex_reason", + [ + Candidate.FinishReason.SAFETY, + Candidate.FinishReason.RECITATION, + Candidate.FinishReason.BLOCKLIST, + Candidate.FinishReason.PROHIBITED_CONTENT, + Candidate.FinishReason.SPII, + ], +) +def test_finish_reason_from_vertex_ai_content_filter_reasons(vertex_reason): + """Responses Gemini stopped for their content map to CONTENT_FILTER.""" + assert finish_reason_from_vertex_ai_to_semantic_kernel(vertex_reason) == FinishReason.CONTENT_FILTER + + def test_format_user_message(): """Test format_user_message.""" user_message = ChatMessageContent(role=AuthorRole.USER, content="User message")