fix(mastra): propagate Gemini thought_signature (providerMetadata) through the AG-UI stack#1314
Conversation
…mini thought_signature Gemini 2.5+ attaches a thought_signature to tool calls via providerMetadata. When a client tool round-trip occurs, this metadata was silently dropped at every layer, causing Gemini to reject the next request. - core: add optional providerMetadata to ToolCallSchema and ToolCallStartEventSchema - client/apply: pass providerMetadata from TOOL_CALL_START event into the ToolCall object - mastra: forward providerMetadata from both local (fullStream) and remote (processDataStream) agent paths into the ToolCallStartEvent - mastra/utils: restore providerMetadata onto tool-call parts when converting AG-UI messages back to Mastra CoreMessages
|
@antanj is attempting to deploy a commit to the CopilotKit Team on Vercel. A member of the Team first needs to authorize it. |
jpr5
left a comment
There was a problem hiding this comment.
Code review
Found 1 issue:
- Schema type mismatch on
providerMetadatabetweenToolCallStartEventSchemaandToolCallSchema. The event schema uses the flatz.record(z.string(), z.any())(Record<string, any>) while the ToolCall schema uses the nestedz.record(z.string(), z.record(z.string(), z.any()))(Record<string, Record<string, any>>). These describe the same field but with different shapes -- the event schema is strictly looser than the type it feeds into. The fix is to make the event schema match the ToolCall schema.
ag-ui/sdks/typescript/packages/core/src/events.ts
Lines 125 to 127 in 85f4a14
vs.
ag-ui/sdks/typescript/packages/core/src/types.ts
Lines 12 to 14 in 85f4a14
Thanks, resolved. |
jpr5
left a comment
There was a problem hiding this comment.
Code review (re-review after schema fix)
Schema mismatch is resolved. Found 2 issues:
- Python SDK not updated --
ToolCallandToolCallStartEventinsdks/python/ag_ui/core/are missingprovider_metadata. The repo has a strong sync discipline between Python and TypeScript SDKs (the 5 most recent commits to Python SDK types are all "align with TypeScript SDK" fixes, and commit bd5f93f updated both simultaneously for thenamefield). Python agents/clients will silently dropproviderMetadata, breaking the Geminithought_signatureuse case for Python consumers.
ag-ui/sdks/typescript/packages/core/src/types.ts
Lines 11 to 14 in 670c7e0
ag-ui/sdks/python/ag_ui/core/types.py
Lines 30 to 37 in 670c7e0
- Protobuf schema not updated --
events.protoandtypes.protoare missingprovider_metadata. The same commit bd5f93f updated proto and TypeScript simultaneously. Agents using the proto/binary transport path will silently loseproviderMetadataduring serialization.
ag-ui/sdks/typescript/packages/proto/src/proto/events.proto
Lines 1 to 10 in 670c7e0
Extend both ToolCall and ToolCallStartEvent with an optional provider_metadata map (string → Struct) in the proto definitions, Python models, and TypeScript encode/decode logic.
Resolved. |
Problem
Gemini 2.5+ models attach a
thought_signatureto tool calls viaproviderMetadata.During a client-side tool round-trip, this metadata was silently dropped at every layer
of the AG-UI stack, causing Gemini to reject the follow-up request with:
Changes
@ag-ui/coreproviderMetadata?: Record<string, Record<string, any>>toToolCallSchema(types.ts)ToolCallStartEventSchema(events.ts)@ag-ui/clientapply/default.ts: destructureproviderMetadatafromTOOL_CALL_STARTevents andconditionally include it on the
ToolCallobject stored inmessages@ag-ui/mastramastra.ts: forwardproviderMetadatafrom both the local agent (fullStream) andremote agent (
processDataStream)tool-callchunks into theToolCallStartEventutils.ts: restoreproviderMetadataontotool-callparts when convertingAssistantMessage.toolCallsback to MastraCoreMessageformat, so Geminireceives the signature on the next request
Tests
2 new tests in
message-conversion.test.ts: verifyproviderMetadatais preserved(or cleanly absent) when converting AG-UI messages to Mastra format
2 new tests in
default.tool-calls.test.ts: verifyTOOL_CALL_START → ToolCallpassthrough in the client apply layer
[Bug]: Gemini 2.5 or 3 tool calls fail with missing thought_signature in @ag-ui/mastra roundtrip (frontend/client tool flows) #1245