Files
semantic-kernel/python/tests/unit/utils
cc83889787 Python: fix(python/google): preserve thought_signature in Gemini function call parts (#13609)
### Motivation and Context

Fixes #13480

When using Gemini models (2.5 Pro / 3 Pro) with thinking enabled, the
API returns a `thought_signature` on function call `Part` objects. Per
[Google's Thought Signatures
documentation](https://ai.google.dev/gemini-api/docs/thought-signatures),
this signature **must** be included in subsequent requests. Without it,
follow-up turns with tool results fail with:

```
HTTP 400: Function call is missing a thought_signature in functionCall parts
```

The .NET connector was already fixed in #13418. This PR applies the
equivalent fix to the Python connector.

### Description

**Response parsing** (capture `thought_signature`):
- `google_ai_chat_completion.py`: When creating `FunctionCallContent`
from response parts, capture `part.thought_signature` into
`FunctionCallContent.metadata` (both non-streaming and streaming paths)
- `vertex_ai_chat_completion.py`: Same capture via
`part_dict.get("thought_signature")` for Vertex AI

**Request building** (restore `thought_signature`):
- `google_ai/services/utils.py`: In `format_assistant_message()`, when a
`FunctionCallContent` has `thought_signature` in its metadata, construct
the `Part` directly with the `thought_signature` parameter instead of
using `Part.from_function_call()`
- `vertex_ai/services/utils.py`: In `format_assistant_message()`,
include `thought_signature` in the `Part.from_dict()` dictionary when
present

**Backward compatible**: When `thought_signature` is not present
(thinking disabled or older models), behavior is identical to before.

### Test Coverage

Added 4 new tests:
- `test_format_assistant_message_with_thought_signature` (Google AI) —
verifies round-trip preservation
- `test_format_assistant_message_without_thought_signature` (Google AI)
— verifies backward compatibility
- `test_format_assistant_message_with_thought_signature` (Vertex AI) —
verifies round-trip preservation
- `test_format_assistant_message_without_thought_signature` (Vertex AI)
— verifies backward compatibility

All 110 existing Google connector tests continue to pass.

### Contribution Checklist

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [x] All unit tests pass, and I have added new tests
- [x] I didn't break anyone 😄

---------

Co-authored-by: giulio-leone <[email protected]>
Co-authored-by: Copilot <[email protected]>
2026-03-13 13:06:41 +00:00
..