Skip to content

Round-trip Google thought signatures and thought token usage - #26

Merged
superdav42 merged 1 commit into
WordPress:trunkfrom
bradvin:fix/google-thought-signatures
Sep 4, 2026
Merged

superdav42 merged 1 commit into
WordPress:trunkfrom
bradvin:fix/google-thought-signatures

Conversation

@bradvin

@bradvin bradvin commented May 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #15.

This adds Google thought metadata support to GoogleTextGenerationModel in two places:

  • preserves Google thoughtSignature values when parsing response parts into AI Client MessagePart instances
  • serializes stored MessagePart thought signatures back to Google request parts
  • passes Google usageMetadata.thoughtsTokenCount into TokenUsage when the installed wordpress/php-ai-client supports thought token usage
  • keeps compatibility with older AI Client versions by checking AiClient::VERSION, DTO methods, and constructor arity before using newer thought metadata fields

Why this is needed

Google requires thought signatures to be returned on subsequent tool-calling requests. Without preserving and returning the exact signature, Google can reject a later tool-call turn with errors like:

Function call is missing a thought_signature in functionCall parts.

This is especially visible in multi-step tool flows where the model returns a functionCall, the client executes the function, and the next request includes the prior model function call plus the function response. If the provider drops the signature while converting between Google parts and AI Client MessageParts, the next request cannot satisfy Google's validation.

How this relates to #21

Thank you to @dilipom13 for #21. That PR identified the same underlying AI Client compatibility concern and introduced several useful ideas this PR keeps, including version-aware support checks and constructor-arity checks for newer DTO fields.

This PR differs in scope:

The intent is complementary rather than competitive: #21 covered important compatibility concepts; this PR extends that approach to the function-calling round trip that Google validates.

Why we needed this in practice

We hit this while using the Google provider with a WordPress AI popup builder that relies on tools/function calls. The provider successfully received model function calls, but the signature was lost before the next request. Google then rejected the follow-up request because the previous model functionCall part no longer included its required thought_signature.

The fix needs to live in the provider because the provider owns both conversions:

  • Google response part -> AI Client MessagePart
  • AI Client MessagePart -> Google request part

Validation

  • php -l src/Models/GoogleTextGenerationModel.php
  • Runtime smoke test against wordpress/php-ai-client 1.3.1:
    • thought support detection returns both signatures and tokenUsage
    • functionCall thoughtSignature parses and serializes as sig-123
    • usageMetadata.thoughtsTokenCount becomes TokenUsage::getThoughtTokens() === 3
  • Integration smoke through FooConvert popup builder test suite: php tests/run.php

composer lint was not runnable locally because this clone does not currently have phpcs installed (sh: phpcs: command not found).

@jeffpaul jeffpaul added this to the 1.3.0 milestone Aug 17, 2026
@jeffpaul

Copy link
Copy Markdown
Member

@bradvin is this work still in progress or can this move out of draft and ready for review?

@bradvin
bradvin marked this pull request as ready for review August 24, 2026 18:37
@bradvin
bradvin requested a review from a team August 24, 2026 18:37
@github-actions

github-actions Bot commented Aug 24, 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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @foo-bender.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Unlinked contributors: foo-bender.

Co-authored-by: superdav42 <superdav42@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>
Co-authored-by: bradvin <bradvin@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>

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

@bradvin

bradvin commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

@jeffpaul thanks for the nudge - this should have been sent for review months ago

@jeffpaul
jeffpaul requested a review from superdav42 August 26, 2026 13:51
@superdav42

Copy link
Copy Markdown
Member

A related PR #36 was approved and merged first, not because it was better but I just happened to review it first. It only
coverd the required functionCall thought-signature round trip.

PR #44 was also merged to correct total-token accounting. we need to rebase this pr. It should:

  • Preserve thought signatures on non-function-call model response parts.
  • Populate TokenUsage::thoughtTokens.
  • Ensure completion tokens include thought tokens, consistent with the TokenUsage contract.
  • PHP AI Client 1.3.1 is now the minimum supported version, so the compatibility checks for older releases can also be simplified.

I think #21 could be closed as redundant.

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

Please rebase. and remove checks for 1.3.0. We are targeting 1.3.1 as the minimum supported php-ai-client as this was released with WP 7.0

@foo-bender
foo-bender force-pushed the fix/google-thought-signatures branch from 07bb95a to 50ab994 Compare September 3, 2026 10:04
@foo-bender

Copy link
Copy Markdown
Contributor

Updated in 50ab994:

  • rebased onto current trunk
  • removed the obsolete PHP AI Client 1.3.0 compatibility handling
  • preserved thought signatures on non-function-call response parts
  • populated thoughtTokens and included thought tokens in completion totals
  • added regression coverage

The full PHP 7.4–8.4 test matrix is green. Ready for re-review.

@bradvin

bradvin commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

@superdav42 pls re-review and let me know what is outstanding

@jeffpaul
jeffpaul requested a review from superdav42 September 3, 2026 23:18

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

Re-reviewed, The requested changes look good:

Ship it!

@superdav42
superdav42 merged commit 1cd8adb into WordPress:trunk Sep 4, 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.

Add support for thought signatures and thought tokens count in results

4 participants