Repository navigation
fix: Preserve extended thinking signatures across conversation turns - #31
Merged
superdav42 merged 4 commits intoAug 31, 2026
Merged
Conversation
Anthropic requires every `thinking` block returned by the model to be echoed back unchanged on all following turns of the same conversation, including its opaque signature. The connector captured only the thinking text when parsing a response and re-serialized the block without a signature, so any turn where the model reasoned before calling a tool failed on the next request with "messages.N.content.0.thinking.signature: Field required". Carry the signature on the MessagePart DTO, which has supported thought signatures since AI Client 1.3.0, so it also survives Message::toArray() / fromArray() for clients that persist the conversation between requests. Gate the use of the DTO field on the installed AI Client version, matching how AnthropicProvider::providerMetadata() already gates newer client features, and skip thinking blocks whose signature is missing rather than sending a block the API is guaranteed to reject.
|
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. |
5 tasks
superdav42
requested changes
Aug 21, 2026
superdav42
left a comment
Member
There was a problem hiding this comment.
Remove these checks and I think we are g2g
Comment on lines
+426
to
+429
| protected static function supportsThoughtSignatures(): bool | ||
| { | ||
| return version_compare(AiClient::VERSION, '1.3.0', '>='); | ||
| } |
Member
There was a problem hiding this comment.
we are targeting >= 1.3.1 for all new releases so we can remove these checks and assume that thought signature is supported.
Remove supportsThoughtSignatures() checks
This was referenced Sep 9, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #30
Problem
With extended thinking enabled, any turn where the model reasons before calling a tool breaks on the next request:
The API requires every
thinkingblock the model returns to be echoed back unchanged on all following turns of the same conversation, including its opaquesignature. The connector captures only the thinking text when parsing a response and re-serializes the block without a signature, so the round-trip is impossible today.This makes single-shot tool calls appear to work while any flow where the model thinks first and then calls a tool ("create a post about X", "search, decide, then write") fails on the second request — after the tool has already run.
Root cause
src/Models/AnthropicTextGenerationModel.phpparseResponseContentMessagePart()builds theMessagePartfrom$partData['thinking']only; the siblingsignaturekey is never read.getMessagePartData()emits['type' => 'thinking', 'thinking' => …]with nosignature.Change
The AI Client
MessagePartDTO has carried thought signatures since 1.3.0 —MessagePart::__construct()third argument,MessagePart::getThoughtSignature(), and thethoughtSignaturekey intoArray()/fromArray(). Nothing needs to be stored outside the existing message history; the connector simply was not using the field.Read it when parsing:
and write it back when serializing:
The AI Client version is gated the same way
AnthropicProvider::providerMetadata()already gates newer client features:Why the DTO field rather than a connector-local part class
My first attempt carried the signature on an
AnthropicThinkingMessagePart extends MessagePartinside the connector. It passes an in-memory test and it is wrong:MessagePart::fromArray()always returnsnew self(...), so the subclass — and the signature with it — is silently dropped the moment a client persists the conversation and rehydrates it.That is not an edge case. Any WordPress chat UI stores the history between HTTP requests, so the signature was lost on exactly the turn that needs it and the 400 came straight back. Using
thoughtSignaturemeans the value survivesMessage::toArray()/Message::fromArray()for free, which is presumably why the field exists.Worth noting this is the same DTO field the Google connector needs for its own
thoughtSignatureround-trip, so both providers converge on one representation rather than each inventing a part subclass.Unsigned thinking blocks
A
thinkingblock sent without its signature is always rejected, so conversation history that was persisted before this fix keeps failing even after upgrading. Since thinking blocks only have to be echoed back for the assistant turn they belong to,prepareMessagesParam()now drops an unsigned thinking block rather than sending one the API is guaranteed to reject. The block is kept if it is the only content left, so a message can never end up with empty content.This is a small behavioral addition rather than a strict bug fix — happy to drop it if you'd rather keep the PR to the round-trip alone, though it is what makes existing stored conversations recover instead of staying permanently broken.
Compatibility
On AI Client < 1.3.0 the version gate short-circuits and the signature is not round-tripped, i.e. exactly today's behavior — no regression, no fatal on the missing DTO argument.
About the composer change
require-devwas pinned towordpress/php-ai-client: ^0.4, which made PHPStan resolveMessagePartwithout the third constructor argument orgetThoughtSignature()and fail the build. I bumped it to^1.3 || dev-trunk(lock: 0.4.2 → 1.4.0, which drops the abandonedphp-http/message-factoryand pullsnyholm/psr7— dev-only transitive changes).WordPress 7.0 bundles 1.3.1, so this only affects what the analysis runs against, not the runtime floor. If you'd rather not move the dev pin, the alternative is a
phpstan.neon.distignore for the two new symbols — tell me which you prefer.Testing
Verified against
wordpress/php-ai-client1.3.1 (the version bundled with WordPress 7.0), driving the model with a stubbed HTTP transporter:thinkingblock with asignatureparses into a thought-channelMessagePartcarrying that signature.type: thinkingwith the signature attached.Message::toArray()→Message::fromArray(), and the rehydrated part still serializes with it — the case the subclass approach failed.trunkwith the same harness before the change.composer phpstan(level max) reports no errors.composer phpcsis clean apart from a CRLF line-ending complaint from my Windows checkout, which it reports for every file ontrunktoo.Related
pause_turnis handled in a separate branch for #29; the two are independent and merge cleanly together.This patch was drafted with the help of Claude, then manually reviewed and verified against a live Anthropic setup before submitting.
🤖 Generated with Claude Code