Skip to content

fix(py/plugins/anthropic): send back empty signed thinking blocks - #147

Merged
huangjeff5 merged 4 commits into
genkit-ai:mainfrom
hkarmoush:fix/anthropic-bedrock-empty-signed-thinking
Nov 30, 2026
Merged

huangjeff5 merged 4 commits into
genkit-ai:mainfrom
hkarmoush:fix/anthropic-bedrock-empty-signed-thinking

Conversation

@hkarmoush

Copy link
Copy Markdown
Contributor

Moved over from genkit-ai/genkit#6839 now that the Python SDK lives here. It was approved there, including the Bedrock follow-up.

Claude sometimes returns a thinking block with an empty thinking string and a valid signature, mostly on the turn right after a tool call. _to_anthropic_block checked if reasoning:, so that block was quietly dropped when the history was sent back. With thinking enabled, an assistant turn that contains tool_use has to start with its thinking block, so the next request in the tool loop gets rejected.

The check is now reasoning is not None. Unsigned reasoning is still dropped like before, so cross-provider history behaves the same. The Bedrock converter and stream accumulator had the same drop, so they now keep any signed reasoning block too.

Tests cover signed empty reasoning being kept and unsigned empty reasoning still being dropped, on both the Anthropic and Bedrock sides, plus an ai.generate round trip checking the thinking block stays first.

uv run pytest packages/genkit-anthropic/tests packages/genkit-amazon-bedrock/tests passes (975 passed, live tests skipped).

Claude can return a thinking block with an empty `thinking` string and
a valid signature, often on the turn right after a tool call. The
request converter checked `if reasoning:`, so the block was dropped.
With thinking enabled, an assistant turn that uses a tool has to start
with its thinking block, so the next request failed.

Check `is not None` instead. Unsigned reasoning is still dropped.

Same bug as #6798 on the JS side.
A model on Bedrock can sign a thinking block with empty text, and a
tool-use turn has to start with that block. The request encoder only
emitted reasoning when both text and signature were set, and the stream
accumulator only kept a block when it had text or redacted content, so
the block was lost on the next request.

Encode and keep any block that has a signature. Unsigned empty reasoning
is still dropped.

Same fix as the earlier commit in this PR.
@hkarmoush
hkarmoush force-pushed the fix/anthropic-bedrock-empty-signed-thinking branch from 9f33c61 to 9aad71f Compare November 29, 2026 22:57

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM! Pushed a small test tweak (c9d2406) so test_reasoning_without_text_or_signature_is_dropped sends an empty reasoning_delta to cover the drop path directly.

@huangjeff5
huangjeff5 enabled auto-merge (squash) November 30, 2026 19:52
@huangjeff5
huangjeff5 merged commit 0d096ca into genkit-ai:main Nov 30, 2026
20 checks passed
huangjeff5 added a commit that referenced this pull request Nov 30, 2026
Resolve semantic conflict with #147 (0d096ca): update
test_generate_claude_sends_back_empty_signed_thinking_before_tool_use
to stub plugin._backend.client instead of the removed
plugin._runtime_client.
@huangjeff5 huangjeff5 mentioned this pull request Dec 3, 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.

2 participants