-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(ai): v5: preserve providerMetadata on invalid tool calls and tool results #10733
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
fix(ai): v5: preserve providerMetadata on invalid tool calls and tool results #10733
Conversation
Fixes missing provider metadata propagation for tool-result parts in `convertToModelMessages`. Previously, `callProviderMetadata` from tool UI parts was only being propagated to tool-call parts but not to the corresponding tool-result parts. This meant provider-specific metadata (like cache control, execution signatures, etc.) was missing from tool results. Both provider-executed and client-executed tool results now correctly receive `providerOptions` from `callProviderMetadata`, ensuring consistency with how other message parts handle provider metadata.
|
Basically, Gemini-3 models requires thought signatures not only in file parts, but also tool parts, I'm still running v5 so I submitted a merge specifically for v5. I'm pretty sure #10508 is still needed though |
|
I THINK that having this as a separate PR is correct because even if #10734 gets backported, it will not include the provider metadata on |
|
I manually ran the reproduction script with the changes in d565a2e and verified that it works |
|
You MAY want to closely look at #10734, honestly I'm uncertain whether only that one will suffice or if you need that one and this one. |
…ults (#10734) ## Background Provider metadata (such as Google Gemini's `thoughtSignature`) was being lost in two scenarios: 1. **Tool result propagation**: Provider-executed tool results within assistant message content were not receiving `providerOptions` from `callProviderMetadata`. This was partially addressed in PR #10361 (which fixed client-executed tool results) but missed the provider-executed case. 2. **Invalid tool call handling**: When a tool call failed input validation (e.g., Zod schema mismatch), the error handler was creating an invalid tool call object without preserving the `providerMetadata` and `providerExecuted` fields from the original tool call. Both issues are critical for providers like Google Gemini 3 that use provider metadata to maintain reasoning context across tool execution cycles. Gemini 3 enforces strict validation and returns a 400 error when signatures are missing: _"Unable to submit request because function call ... is missing a thought_signature"_. ## Summary <!-- What did you change? --> This PR implements two related fixes for provider metadata preservation: ### Fix 1: Provider-Executed Tool Result Metadata Propagation **File**: `packages/ai/src/ui/convert-to-model-messages.ts` Added `providerOptions` propagation from `callProviderMetadata` to provider-executed tool-result parts in assistant message content (lines 223-225). ### Fix 2: Invalid Tool Call Metadata Preservation **File**: `packages/ai/src/generate-text/parse-tool-call.ts` Added `providerMetadata` and `providerExecuted` preservation in the error handler (lines 95-96). When tool input validation fails, the invalid tool call object now includes these fields from the original `LanguageModelV3ToolCall`, matching the behavior of the success paths. **After this PR:** - All tool-result parts consistently receive `providerOptions` from `callProviderMetadata` - Invalid tool calls preserve provider metadata through validation failures - Gemini 3 `thoughtSignature` validation passes for both execution modes and error paths ## Manual Verification> I manually ran: [google-vertex-code-execution.](examples/ai-core/src/stream-text/google-vertex-code-execution.ts) [google-vertex-fullstream](examples/ai-core/src/stream-text/google-vertex-fullstream.ts) [google-vertex-grounding](examples/ai-core/src/stream-text/google-vertex-grounding.ts) with both the `gemini-3-pro-preview` and `gemini-2.5-pro` models and confirmed they function correctly Finally, I manually ran the provided reproduction script in #10560, and verified that it works correctly. ## Related Issues ## Related Issues Fixes #10560 Fixes #10721 **Backport PR:** - #10733 - Similar port of complete fix to `release-v5.0` branch **Related PRs:** - #10361 - Original partial fix (client-executed tool results only) **Potentially Related PRs:** - #10462 - `fix(google): add thought signature to gemini 3 pro image parts` - #10508 - Provider metadata handling improvements
Background
Provider metadata (such as Google Gemini's
thoughtSignature) was being lost in two scenarios:Tool result propagation: When tool results were converted to model messages, the
callProviderMetadatafrom tool UI parts was not being propagated to tool-result parts. This was fixed in main branch (PR fix(provider/google): preserve thoughtSignature through tool execution #10361, merged November 19, 2025) but not backported to v5.0.Invalid tool call handling: When a tool call failed input validation (e.g., Zod schema mismatch), the error handler was creating an invalid tool call object without preserving the
providerMetadatafield from the original tool call.This is particularly problematic for providers like Google Gemini that use provider metadata to track
thoughtSignaturethrough reasoning and tool execution cycles.Summary
This PR includes two related fixes for provider metadata preservation on the
release-v5.0branch:Fix 1: Tool Result Metadata Propagation (Backport from main)
Backported changes ensure that
callProviderMetadatafrom tool UI parts is correctly propagated to tool-result parts asproviderOptionsin two locations:Fix 2: Invalid Tool Call Metadata Preservation (New)
Added
providerMetadatapreservation in the error handler ofparse-tool-call.ts(line 88). When tool input validation fails, the invalid tool call object now includes theproviderMetadatafrom the originalLanguageModelV2ToolCall, matching the behavior of the success path.File: packages/ai/src/generate-text/parse-tool-call.ts:88
Both fixes ensure consistency in how provider metadata flows through the entire tool execution lifecycle (tool-call → tool-result) and error paths.
Manual Verification
Added comprehensive test coverage:
In addition, I manually ran:
google-vertex-code-execution.
google-vertex-fullstream
google-vertex-grounding
with both the
gemini-3-pro-previewandgemini-2.5-promodels and confirmed they function correctly.Finally, I manually ran the provided reproduction script in #10560, and verified that it works correctly.
Checklist
pnpm changesetin the project root)pnpm prettier-fixin the project root)Future Work
Related Issues
Fixes #10560
Fixes #10721
Related PR:
Potentially Related PRs:
fix(google): add thought signature to gemini 3 pro image parts