Skip to content

fix: Preserve function call thought signatures across conversation turns - #36

Merged
superdav42 merged 3 commits into
WordPress:trunkfrom
Arkenon:fix/thought-signature-round-trip
Aug 31, 2026
Merged

superdav42 merged 3 commits into
WordPress:trunkfrom
Arkenon:fix/thought-signature-round-trip

Conversation

@Arkenon

@Arkenon Arkenon commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #34

Problem

Gemini thinking models attach a thoughtSignature to every functionCall part they return, and the Google AI API requires it to be sent back unchanged with the conversation history on every following turn:

Bad Request (400) - Function call is missing a thought_signature in functionCall parts.

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.php

  • parseResponseCandidateMessagePart() builds the MessagePart from functionCall alone and ignores the sibling thoughtSignature key.
  • getMessagePartData() serializes a function call part back to ['functionCall' => [...]] with no thoughtSignature.

No new storage is needed to fix this: the AI Client MessagePart DTO has carried thought signatures since 1.3.0 — constructor argument, getThoughtSignature(), and the thoughtSignature key in toArray() / 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 survives Message::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() and supportsThoughtSignatures().

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 googleSearch response those arrive alongside toolCall / toolResponse parts 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 ProviderMetadata features in GoogleProvider::providerMetadata():

protected static function supportsThoughtSignatures(): bool
{
    return version_compare(AiClient::VERSION, '1.3.0', '>=');
}

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-dev for wordpress/php-ai-client is raised from ^0.4 to ^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 MessagePart that has no getThoughtSignature() 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 to wordpress/php-ai-client 0.4.2 → 1.4.0 plus its transitive dev dependency shift (php-http/message-factory out, nyholm/psr7 in).

If you'd rather keep the ^0.4 floor, I'm happy to drop the composer change and add a narrow phpstan.neon.dist ignore instead — just say which you prefer.

Testing

Verified against wordpress/php-ai-client 1.3.1 (the version bundled with WordPress 7.0) with a thinking Gemini model and a real WordPress Abilities tool set:

  • Before: the first turn returns a functionCall, and the follow-up request carrying the tool result is rejected with the error above.
  • After: the parsed MessagePart exposes the signature, it survives a toArray() / fromArray() round trip, and the next request serializes it back as a sibling of functionCall. Multi-turn tool calling completes.

composer phpstan (level max) and composer phpcs are clean — the only reported error is the pre-existing _doing_it_wrong symbol lookup in GoogleImageGenerationModel.php:85, which is present on trunk as 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

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.
@Arkenon
Arkenon requested a review from a team July 27, 2026 20:39
@github-actions

github-actions Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Arkenon <arkenon@git.wordpress.org>
Co-authored-by: superdav42 <superdav42@git.wordpress.org>
Co-authored-by: nextend <nextendweb@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@jeffpaul jeffpaul added this to the 1.3.0 milestone Aug 17, 2026
@jeffpaul
jeffpaul requested a review from superdav42 August 24, 2026 17:52

@superdav42 superdav42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.3.1 is our min supported version of the client so we don't need to check for thought signature support.

Comment on lines +838 to +851
/**
* 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', '>=');
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.3.1 is our min supported version so we can assume it is present and don't need this check here.

Comment on lines +829 to +832
if (!self::supportsThoughtSignatures()) {
return null;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't actually need this check

$thoughtSignature = isset($partData['thoughtSignature']) && is_string($partData['thoughtSignature'])
? $partData['thoughtSignature']
: null;
if ($thoughtSignature !== null && self::supportsThoughtSignatures()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can assume support is present.

Suggested change
if ($thoughtSignature !== null && self::supportsThoughtSignatures()) {
if ($thoughtSignature !== null) {

@superdav42 superdav42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@superdav42
superdav42 merged commit c6344cb into WordPress:trunk Aug 31, 2026
14 checks passed
@jeffpaul jeffpaul modified the milestones: 1.3.0, 1.2.0 Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Function call thought signatures are dropped, breaking multi-turn tool calling with thinking models

3 participants