Skip to content

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

Closed
hkarmoush wants to merge 3 commits into
genkit-ai:mainfrom
hkarmoush:hkarmoush-fix-py-anthropic-empty-reasoning
Closed

hkarmoush wants to merge 3 commits into
genkit-ai:mainfrom
hkarmoush:hkarmoush-fix-py-anthropic-empty-reasoning

Conversation

@hkarmoush

Copy link
Copy Markdown
Contributor

Python side of #6798 (JS fix is in #6838).

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. Unlike JS there's no local error, which makes it harder to track down.

The check is now reasoning is not None. Unsigned reasoning is still dropped like before, so cross-provider history behaves the same.

There was an existing test, test_empty_reasoning_part_is_skipped, that expected a signed empty part to be dropped. I couldn't find a reason for that in #5724, it looked like it just captured what the truthy check did, so I changed it to expect the thinking block. Tests:

  • signed empty reasoning encodes as {type: thinking, thinking: '', signature}
  • unsigned empty reasoning is still skipped
  • through ai.generate: a [thinking(''), tool_use] turn followed by the tool response, checking the thinking block is still first in what goes to the client

uv run pytest packages/genkit-anthropic/tests passes (336 passed, 5 live skipped), ruff, pyright and ty are clean.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the Anthropic model integration to ensure that empty signed reasoning blocks (thinking blocks with empty text but containing a signature) are not skipped, as Claude requires them to start a tool-use turn. It changes the check from if reasoning: to if reasoning is not None: and adds corresponding unit tests to verify that empty signed reasoning is preserved while empty unsigned reasoning is still skipped. I have no feedback to provide as the changes are well-implemented and covered by tests.

@hkarmoush

Copy link
Copy Markdown
Contributor Author

@huangjeff5 would you mind reviewing this one? Empty signed thinking blocks were getting dropped on the way back to Anthropic. It's the Python side of #6838, which already merged.

@hkarmoush
hkarmoush force-pushed the hkarmoush-fix-py-anthropic-empty-reasoning branch from 6d865e3 to 8b8e449 Compare October 30, 2026 21:57

@huangjeff5 huangjeff5 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Approved, but please make the following changes before merging

  • py/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/_converters.py:328: Bedrock's Claude path has the same drop, in both directions. The encoder here checks if text and signature, and the stream accumulator at _stream.py:203 checks if reasoning or block.redacted. Both skip a signed block with empty text.

So the same [thinking(''), tool_use] turn through Bedrock with thinking on loses its thinking block on the next request. Sync decode keeps it (_converters.py:613), so the encoder alone is enough to break it.

I suggest if signature: in the encoder and or block.signature in the accumulator, with the same two tests as on the Anthropic side.

reasoning = part.reasoning
if reasoning:
# Claude can sign a thinking block with empty text, and a tool-use turn
# must start with it, so empty reasoning is not skipped.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: unsigned empty reasoning is still skipped two lines down, so "empty reasoning is not skipped" reads wrong. Maybe: "Claude can sign a thinking block with empty text, and a tool-use turn must start with it, so signed empty reasoning is kept."


await ai.generate(model='anthropic/claude-sonnet-4', messages=history)

create = mock_client.messages.create if mock_client.messages.create.called else mock_client.beta.messages.create

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this request has no beta config, so it always hits messages.create. I suggest asserting mock_client.messages.create.assert_awaited_once() and reading call_args from it directly, so a call that silently moves to the beta endpoint fails the test instead of passing.

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 genkit-ai#6798 on the JS side.
@hkarmoush
hkarmoush force-pushed the hkarmoush-fix-py-anthropic-empty-reasoning branch from 8b8e449 to 4e842af Compare November 13, 2026 07:38
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

Copy link
Copy Markdown
Contributor Author

Thanks @huangjeff5, I made the Bedrock change you suggested: the encoder now keeps any signed reasoning block and the stream accumulator checks the signature too, with matching tests on both sides (kept when signed, still dropped when unsigned). I also took the two nits on the Anthropic side.

@MichaelDoyle

Copy link
Copy Markdown
Contributor

@huangjeff5 @hkarmoush heads up — since Python SDK development has moved to https://github.com/genkit-ai/genkit-python and this PR is approved (with the Bedrock follow-up included), could we port/re-open or cherry-pick this over to genkit-ai/genkit-python (paths map 1:1 after stripping the leading py/ prefix) so we can close this PR out here?

@hkarmoush

Copy link
Copy Markdown
Contributor Author

Thanks for the heads up, ported it over with the Bedrock follow-up included: genkit-ai/genkit-python#147. Fine to close this one whenever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants