Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ def _create_chat_message_content(
items: list[CMC_ITEM_TYPES] = []
if candidate.content and candidate.content.parts:
for idx, part in enumerate(candidate.content.parts):
if getattr(part, "thought", False):
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getattr(part, "thought", False) is not safe when part can be a MagicMock (or any object whose missing attributes return truthy sentinels). In that case getattr(..., False) returns a truthy mock and this will incorrectly skip the part, breaking the existing getattr-guard tests and potentially other mock-based callers. Consider using a strict check like getattr(part, "thought", False) is True (or explicitly == True only if you avoid MagicMock’s magic methods) so only a real boolean True triggers filtering.

Suggested change
if getattr(part, "thought", False):
if getattr(part, "thought", False) is True:

Copilot uses AI. Check for mistakes.
continue
if part.text:
items.append(TextContent(text=part.text, inner_content=response, metadata=response_metadata))
elif part.function_call:
Expand Down Expand Up @@ -357,6 +359,8 @@ def _create_streaming_chat_message_content(
items: list[STREAMING_ITEM_TYPES] = []
if candidate.content and candidate.content.parts:
for idx, part in enumerate(candidate.content.parts):
if getattr(part, "thought", False):
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue in streaming path: getattr(part, "thought", False) can evaluate truthy for mocks / sentinel objects even when the attribute is effectively absent, causing parts to be dropped unexpectedly. Use a strict boolean check (e.g., getattr(part, "thought", False) is True) to filter only explicit thought parts.

Suggested change
if getattr(part, "thought", False):
if getattr(part, "thought", False) is True:

Copilot uses AI. Check for mistakes.
continue
if part.text:
items.append(
StreamingTextContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
import pytest
from google.genai import Client
from google.genai.models import AsyncModels
from google.genai.types import Content, GenerateContentConfigDict
from google.genai.types import (
Candidate,
Content,
FinishReason as GoogleFinishReason,
GenerateContentConfigDict,
GenerateContentResponse,
GenerateContentResponseUsageMetadata,
Part,
)

from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.google.google_ai.google_ai_prompt_execution_settings import (
Expand Down Expand Up @@ -259,6 +267,63 @@ async def test_google_ai_chat_completion_with_function_choice_behavior_no_tool_c
# endregion chat completion


def test_google_ai_chat_completion_filters_thought_text_parts(google_ai_unit_test_env) -> None:
google_ai_chat_completion = GoogleAIChatCompletion()

candidate = Candidate()
candidate.index = 0
candidate.content = Content(role="model", parts=[])
candidate.finish_reason = GoogleFinishReason.STOP

thinking_part = Part.from_text(text="internal reasoning")
thinking_part.thought = True # type: ignore[attr-defined]
answer_part = Part.from_text(text="final answer")

candidate.content.parts.extend([thinking_part, answer_part])

response = GenerateContentResponse()
response.candidates = [candidate]
response.usage_metadata = GenerateContentResponseUsageMetadata(
prompt_token_count=0,
cached_content_token_count=0,
candidates_token_count=0,
total_token_count=0,
)

message = google_ai_chat_completion._create_chat_message_content(response, candidate)

assert message.content == "final answer"
assert len(message.items) == 1


def test_google_ai_streaming_chat_completion_filters_thought_text_parts(google_ai_unit_test_env) -> None:
google_ai_chat_completion = GoogleAIChatCompletion()

candidate = Candidate()
candidate.index = 0
candidate.content = Content(role="model", parts=[])
candidate.finish_reason = GoogleFinishReason.STOP

thinking_part = Part.from_text(text="internal reasoning")
thinking_part.thought = True # type: ignore[attr-defined]
answer_part = Part.from_text(text="streamed answer")
candidate.content.parts.extend([thinking_part, answer_part])

response = GenerateContentResponse()
response.candidates = [candidate]
response.usage_metadata = GenerateContentResponseUsageMetadata(
prompt_token_count=0,
cached_content_token_count=0,
candidates_token_count=0,
total_token_count=0,
)

message = google_ai_chat_completion._create_streaming_chat_message_content(response, candidate)

assert message.content == "streamed answer"
assert len(message.items) == 1


# region streaming chat completion


Expand Down
Loading