-
Notifications
You must be signed in to change notification settings - Fork 4.4k
.Net: Add ThoughtSignature support for Gemini function calling #13418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
.Net: Add ThoughtSignature support for Gemini function calling #13418
Conversation
… 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.
|
@microsoft-github-policy-service agree |
|
Any forecast when this will be merged? we cannot use Gemini 3 flash with functions. |
|
@Cozmopolit have you tested this with Gemini 3 Flash? See the fix here. |
|
@roldengarm did it fix the issue? you were able to call functions with gemini 3 flash using this fix? |
|
@TommyAgami92 yes, with the fix I did, it works fine with function calling and thoughts enabled. If you want to check out for yourself, see this branch and instructions to run here It has all changes from @Cozmopolit from this branch as well, so I can't raise a PR (yet). But, at this stage I think this PR is not complete as it doesn't work for me with Gemini3 without my fixes. |
|
@roldengarm thanks a lot , will check it out! |
|
Will check out your branch and amend this PR tommorow. Thanks! |
When thinking is enabled, Gemini 3 models may return function calls across multiple streaming chunks, each with its own ThoughtSignature. The previous implementation only processed the first chunk with tool calls, causing subsequent function calls to be silently dropped. Changes: - Accumulate all tool calls from streaming chunks (preserving ThoughtSignature) - Include pre-tool-call content in the combined message for chat history - Accumulate ReasoningContent (Items) across chunks - Use final chunk's metadata for accurate usage stats and finish reason - Use lazy initialization to avoid overhead when AutoInvoke is disabled Fixes function calling with Gemini 2.5 Flash/Pro when thinkingConfig is set.
|
@Cozmopolit just checking if you had a chance to look into this? Please bear in mind my code change was quickly tested and I haven't added/updated automated tests, but I'm happy to add that if you confirm the change is OK. |
|
@roldengarm There is a PR to your branch&fork waiting for your comment. |
|
While I try to get this right and we wait for MS to react here, you might want to consider: |
Thanks @Cozmopolit , looks good, approved 👍 |
|
@rogerbarreto as you reviewed my previous PR regarding Gemini & Thinking, would this also be a PR you could review please? |
Motivation and Context
Fixes #13417
Gemini 2.5/3 Pro with thinking enabled requires
thoughtSignaturetokens for function calling. Without this fix, function calling fails with HTTP 400: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
thoughtSignaturesupport to the Gemini connector:Model Changes:
ThoughtSignatureproperty toGeminiPartfor JSON serializationThoughtSignatureproperty toGeminiFunctionToolCallto carry signature through SK abstractionsThoughtSignatureproperty toGeminiMetadatafor text response signaturesGeminiChatMessageContentacceptingGeminiPart[]for signature captureResponse Processing:
thoughtSignaturefrom function call responses intoGeminiFunctionToolCallthoughtSignaturefrom text responses intoGeminiMetadataRequest Building:
thoughtSignatureto function call parts when building requests from chat historythoughtSignatureto text parts for model messagesBug Fixes:
Test Coverage:
Contribution Checklist
semantic-kernel-gemini-connector-pr.md