Skip to content

Conversation

@Cozmopolit
Copy link
Contributor

Motivation and Context

Fixes #13417

Gemini 2.5/3 Pro with thinking enabled requires thoughtSignature tokens for function calling. Without this fix, function calling fails with HTTP 400:

Function call is missing a thought_signature in functionCall parts

This PR implements full ThoughtSignature support as specified in Google's Thought Signatures documentation.

Full Specification used for this Fix is attached.

Description

This PR adds thoughtSignature support to the Gemini connector:

Model Changes:

  • Add ThoughtSignature property to GeminiPart for JSON serialization
  • Add ThoughtSignature property to GeminiFunctionToolCall to carry signature through SK abstractions
  • Add ThoughtSignature property to GeminiMetadata for text response signatures
  • Add constructor overload to GeminiChatMessageContent accepting GeminiPart[] for signature capture

Response Processing:

  • Capture thoughtSignature from function call responses into GeminiFunctionToolCall
  • Capture thoughtSignature from text responses into GeminiMetadata

Request Building:

  • Restore thoughtSignature to function call parts when building requests from chat history
  • Restore thoughtSignature to text parts for model messages

Bug Fixes:

  • Fix empty ToolCalls list incorrectly treated as containing tools (streaming path)
  • Fix grammar: "an Gemini" → "a Gemini" in XML doc comment

Test Coverage:

  • 29 new unit tests covering serialization, capture, restore, and edge cases
  • All 376 tests pass

Contribution Checklist

semantic-kernel-gemini-connector-pr.md

… calling

Implements support for Gemini's thoughtSignature token which is required
when using function calling with thinking mode enabled (Gemini 2.5 Pro).

Changes:
- Add ThoughtSignature property to GeminiPart for JSON serialization
- Add ThoughtSignature property to GeminiFunctionToolCall (follows OpenAI Id pattern)
- Add constructor overload to GeminiChatMessageContent accepting GeminiPart[]
- Update response processing to capture ThoughtSignature from function calls
- Add ThoughtSignature to GeminiMetadata for text responses
- Include ThoughtSignature when building request parts for function calls
- Restore ThoughtSignature from Metadata for text response history
- Fix empty ToolCalls check in streaming path (is not null -> is { Count: > 0 })
- Fix grammar: 'an Gemini' -> 'a Gemini' in XML doc

Fixes microsoft#13417
Add comprehensive unit tests for ThoughtSignature functionality:
- GeminiPartTests: 8 tests for serialization/deserialization
- GeminiFunctionToolCallTests: 5 tests for ThoughtSignature capture
- GeminiRequestTests: 7 tests for request building with ThoughtSignature
- GeminiMetadataTests: 4 tests for ThoughtSignature property (new file)
- GeminiChatGenerationFunctionCallingTests: 3 tests for response processing
- GeminiChatStreamingFunctionCallingTests: 2 tests for empty ToolCalls handling

Add test data files:
- chat_function_with_thought_signature_response.json
- chat_text_with_thought_signature_response.json

Fix streaming auto-invoke loop:
- Reset LastMessage at start of each iteration to detect tool calls
- Exit loop when no tool calls found (prevents infinite loop)

All 376 tests pass.
@Cozmopolit Cozmopolit requested a review from a team as a code owner December 13, 2025 16:46
@moonbox3 moonbox3 added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel labels Dec 13, 2025
@github-actions github-actions bot changed the title Add ThoughtSignature support for Gemini function calling .Net: Add ThoughtSignature support for Gemini function calling Dec 13, 2025
@Cozmopolit
Copy link
Contributor Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.Net: Bug: Gemini function calling fails with HTTP 400 when thinking is enabled (missing thoughtSignature support)

3 participants