Skip to content

fix: Preserve extended thinking signatures across conversation turns - #31

Merged
superdav42 merged 4 commits into
WordPress:trunkfrom
Arkenon:fix/anthropic-thinking-signature
Aug 31, 2026
Merged

superdav42 merged 4 commits into
WordPress:trunkfrom
Arkenon:fix/anthropic-thinking-signature

Conversation

@Arkenon

@Arkenon Arkenon commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #30

Problem

With extended thinking enabled, any turn where the model reasons before calling a tool breaks on the next request:

Bad Request (400) - messages.1.content.0.thinking.signature: Field required

The API requires every thinking block the model returns to be echoed back unchanged on all following turns of the same conversation, including its opaque signature. 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.php

  • parseResponseContentMessagePart() builds the MessagePart from $partData['thinking'] only; the sibling signature key is never read.
  • getMessagePartData() emits ['type' => 'thinking', 'thinking' => …] with no signature.

Change

The AI Client MessagePart DTO has carried thought signatures since 1.3.0 — MessagePart::__construct() third argument, MessagePart::getThoughtSignature(), and the thoughtSignature key in toArray() / fromArray(). Nothing needs to be stored outside the existing message history; the connector simply was not using the field.

Read it when parsing:

$signature = isset($partData['signature']) && is_string($partData['signature'])
    ? $partData['signature']
    : null;
if ($signature !== null && $signature !== '' && self::supportsThoughtSignatures()) {
    return new MessagePart($partData['thinking'], MessagePartChannelEnum::thought(), $signature);
}
return new MessagePart($partData['thinking'], MessagePartChannelEnum::thought());

and write it back when serializing:

$signature = $this->getMessagePartThoughtSignature($part);
if ($signature !== null) {
    $thinkingData['signature'] = $signature;
}

The AI Client version is gated the same way AnthropicProvider::providerMetadata() already gates newer client features:

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

Why the DTO field rather than a connector-local part class

My first attempt carried the signature on an AnthropicThinkingMessagePart extends MessagePart inside the connector. It passes an in-memory test and it is wrong: MessagePart::fromArray() always returns new 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 thoughtSignature means the value survives Message::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 thoughtSignature round-trip, so both providers converge on one representation rather than each inventing a part subclass.

Unsigned thinking blocks

A thinking block 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-dev was pinned to wordpress/php-ai-client: ^0.4, which made PHPStan resolve MessagePart without the third constructor argument or getThoughtSignature() and fail the build. I bumped it to ^1.3 || dev-trunk (lock: 0.4.2 → 1.4.0, which drops the abandoned php-http/message-factory and pulls nyholm/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.dist ignore for the two new symbols — tell me which you prefer.

Testing

Verified against wordpress/php-ai-client 1.3.1 (the version bundled with WordPress 7.0), driving the model with a stubbed HTTP transporter:

  • A thinking block with a signature parses into a thought-channel MessagePart carrying that signature.
  • Serializing that part back emits type: thinking with the signature attached.
  • The signature survives Message::toArray() → Message::fromArray(), and the rehydrated part still serializes with it — the case the subclass approach failed.
  • An unsigned thought part is dropped when the message has other content, and kept when it is the only content.
  • Reproduced the original failure on trunk with the same harness before the change.

composer phpstan (level max) reports no errors. composer phpcs is clean apart from a CRLF line-ending complaint from my Windows checkout, which it reports for every file on trunk too.

Related

pause_turn is 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

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.
@Arkenon
Arkenon requested a review from a team July 27, 2026 21:23
@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: jeffpaul <jeffpaul@git.wordpress.org>
Co-authored-by: superdav42 <superdav42@git.wordpress.org>

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

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

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', '>=');
}

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 are targeting >= 1.3.1 for all new releases so we can remove these checks and assume that thought signature is supported.

@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! Thanks!

@superdav42
superdav42 merged commit eeccd37 into WordPress:trunk Aug 31, 2026
2 checks passed
@whyisjake whyisjake mentioned this pull request Oct 1, 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.

Extended thinking signature is dropped on parse and never re-serializ ed, breaking multi-turn tool use ("thinking.signature: Field required")

3 participants