Repository navigation
fix: Preserve function call thought signatures across conversation turns - #36
Conversation
Thinking models attach a thought signature to every function call part and the Google AI API requires it to be sent back unchanged on all following turns of the same conversation. The signature was neither read when parsing a response nor written when serializing the history, so any multi-turn tool call failed with "Function call is missing a thought_signature in functionCall parts". The MessagePart DTO carries thought signatures since AI Client 1.3.0, so the value round-trips through the existing message history. Older AI Client versions are detected and keep the previous behavior. The php-ai-client dev requirement is raised to ^1.3 accordingly, so static analysis runs against the version that provides the API.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
superdav42
left a comment
There was a problem hiding this comment.
1.3.1 is our min supported version of the client so we don't need to check for thought signature support.
| /** | ||
| * Checks whether the AI Client in use supports thought signatures on message parts. | ||
| * | ||
| * Thought signature support was added to the `MessagePart` DTO in AI Client 1.3.0. With an | ||
| * older version the signature cannot be carried across turns, so it is ignored. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return bool True if thought signatures are supported, false otherwise. | ||
| */ | ||
| protected static function supportsThoughtSignatures(): bool | ||
| { | ||
| return version_compare(AiClient::VERSION, '1.3.0', '>='); | ||
| } |
There was a problem hiding this comment.
1.3.1 is our min supported version so we can assume it is present and don't need this check here.
| if (!self::supportsThoughtSignatures()) { | ||
| return null; | ||
| } | ||
|
|
There was a problem hiding this comment.
We don't actually need this check
| $thoughtSignature = isset($partData['thoughtSignature']) && is_string($partData['thoughtSignature']) | ||
| ? $partData['thoughtSignature'] | ||
| : null; | ||
| if ($thoughtSignature !== null && self::supportsThoughtSignatures()) { |
There was a problem hiding this comment.
We can assume support is present.
| if ($thoughtSignature !== null && self::supportsThoughtSignatures()) { | |
| if ($thoughtSignature !== null) { |
Fixes #34
Problem
Gemini thinking models attach a
thoughtSignatureto everyfunctionCallpart they return, and the Google AI API requires it to be sent back unchanged with the conversation history on every following turn:The connector neither reads the signature when parsing a response nor writes it when serializing the history, so it is lost after the first turn. That breaks tool calling at the point where it becomes useful: the model requests a tool, the client executes it and appends the result, and the very next request is rejected. Single-shot prompts are unaffected, which is why this only shows up once an agentic loop runs.
Root cause
src/Models/GoogleTextGenerationModel.phpparseResponseCandidateMessagePart()builds theMessagePartfromfunctionCallalone and ignores the siblingthoughtSignaturekey.getMessagePartData()serializes a function call part back to['functionCall' => [...]]with nothoughtSignature.No new storage is needed to fix this: the AI Client
MessagePartDTO has carried thought signatures since 1.3.0 â constructor argument,getThoughtSignature(), and thethoughtSignaturekey intoArray()/fromArray(). The connector simply wasn't using it.Change
The signature is read on the way in and written on the way out, so it round-trips through the existing message history. Because it lives inside
MessagePart, it also survivesMessage::toArray()/Message::fromArray(), which matters for clients that persist a conversation between HTTP requests rather than keeping it in memory.Two small helpers were added:
getMessagePartThoughtSignature()andsupportsThoughtSignatures().Why only function call parts
This is deliberate rather than an oversight. Function call parts are the case the API hard-requires, and the one that is currently broken.
Text parts can carry signatures too, but in a
googleSearchresponse those arrive alongsidetoolCall/toolResponseparts that have no representation in the client's message model (see #32). Echoing back a partial set of a turn's signatures seemed more likely to cause new rejections than omitting them, so I kept the scope to what is required and verifiable. Extending the same helpers to other part types later is a small change if it turns out to be needed.Compatibility
The AI Client version is gated the same way the connector already gates
ProviderMetadatafeatures inGoogleProvider::providerMetadata():With an older AI Client the signature cannot be carried by the DTO, so behavior is exactly as before. There is no streaming code path in this connector, so nothing else needs the same handling.
About the composer change
require-devforwordpress/php-ai-clientis raised from^0.4to^1.3(lock resolves to 1.4.0). This is a dev-only change, but I want to flag it rather than have it show up as noise in the diff.The reason is static analysis: with 0.4.2 installed, PHPStan sees a
MessagePartthat has nogetThoughtSignature()and a two-argument constructor, so it flags the new code even though the runtime guard makes it correct. Analyzing against the version that actually introduced the API seemed more honest than adding an ignore rule for a real signature mismatch. The lock diff is limited towordpress/php-ai-client0.4.2 â 1.4.0 plus its transitive dev dependency shift (php-http/message-factoryout,nyholm/psr7in).If you'd rather keep the
^0.4floor, I'm happy to drop the composer change and add a narrowphpstan.neon.distignore instead â just say which you prefer.Testing
Verified against
wordpress/php-ai-client1.3.1 (the version bundled with WordPress 7.0) with a thinking Gemini model and a real WordPress Abilities tool set:functionCall, and the follow-up request carrying the tool result is rejected with the error above.MessagePartexposes the signature, it survives atoArray()/fromArray()round trip, and the next request serializes it back as a sibling offunctionCall. Multi-turn tool calling completes.composer phpstan(level max) andcomposer phpcsare clean â the only reported error is the pre-existing_doing_it_wrongsymbol lookup inGoogleImageGenerationModel.php:85, which is present ontrunkas well.This branch is independent of the one for #32 and #33 and merges cleanly with both; I've been running all three together locally.
This patch was drafted with the help of Claude, then manually reviewed and verified against a live Gemini setup before submitting.
ðĪ Generated with Claude Code