Repository navigation
Conversation
There was a problem hiding this comment.
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.
|
@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. |
6d865e3 to
8b8e449
Compare
There was a problem hiding this comment.
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 checksif text and signature, and the stream accumulator at_stream.py:203checksif 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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
8b8e449 to
4e842af
Compare
…t standard endpoint in test
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.
|
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. |
|
@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? |
|
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. |
Python side of #6798 (JS fix is in #6838).
Claude sometimes returns a thinking block with an empty
thinkingstring and a valid signature, mostly on the turn right after a tool call._to_anthropic_blockcheckedif reasoning:, so that block was quietly dropped when the history was sent back. With thinking enabled, an assistant turn that containstool_usehas 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:{type: thinking, thinking: '', signature}ai.generate: a[thinking(''), tool_use]turn followed by the tool response, checking the thinking block is still first in what goes to the clientuv run pytest packages/genkit-anthropic/testspasses (336 passed, 5 live skipped), ruff, pyright and ty are clean.