Repository navigation
Round-trip Google thought signatures and thought token usage - #26
Conversation
|
@bradvin is this work still in progress or can this move out of draft and ready for review? |
|
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 Unlinked AccountsThe 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. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
@jeffpaul thanks for the nudge - this should have been sent for review months ago |
|
A related PR #36 was approved and merged first, not because it was better but I just happened to review it first. It only PR #44 was also merged to correct total-token accounting. we need to rebase this pr. It should:
I think #21 could be closed as redundant. |
superdav42
left a comment
There was a problem hiding this comment.
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
07bb95a to
50ab994
Compare
|
Updated in 50ab994:
The full PHP 7.4â8.4 test matrix is green. Ready for re-review. |
|
@superdav42 pls re-review and let me know what is outstanding |
Summary
Fixes #15.
This adds Google thought metadata support to
GoogleTextGenerationModelin two places:thoughtSignaturevalues when parsing response parts into AI ClientMessagePartinstancesMessagePartthought signatures back to Google request partsusageMetadata.thoughtsTokenCountintoTokenUsagewhen the installedwordpress/php-ai-clientsupports thought token usageAiClient::VERSION, DTO methods, and constructor arity before using newer thought metadata fieldsWhy 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:
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 ClientMessageParts, 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:
thoughtSignaturefor thought text parts.functionCallparts and serializes signatures back into outgoing request parts, which is required for Google tool-calling round trips.wordpress/php-ai-client >= 1.3.0threshold referenced in Add support for thought signatures and thought tokens count in results #15, while still checking methods and constructor arity defensively.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
functionCallpart no longer included its requiredthought_signature.The fix needs to live in the provider because the provider owns both conversions:
MessagePartMessagePart-> Google request partValidation
php -l src/Models/GoogleTextGenerationModel.phpwordpress/php-ai-client1.3.1:signaturesandtokenUsagefunctionCallthoughtSignatureparses and serializes assig-123usageMetadata.thoughtsTokenCountbecomesTokenUsage::getThoughtTokens() === 3php tests/run.phpcomposer lintwas not runnable locally because this clone does not currently havephpcsinstalled (sh: phpcs: command not found).