From 5cca5559c4ec549965201207543d5e643ecd7af2 Mon Sep 17 00:00:00 2001 From: hkarmoush Date: Tue, 20 Oct 2026 18:03:02 +0300 Subject: [PATCH 1/4] fix(py/plugins/anthropic): send back empty signed thinking blocks 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. --- .../src/genkit_anthropic/_models.py | 4 +- .../tests/anthropic_models_test.py | 49 ++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/packages/genkit-anthropic/src/genkit_anthropic/_models.py b/packages/genkit-anthropic/src/genkit_anthropic/_models.py index 8dec656c..454bf851 100644 --- a/packages/genkit-anthropic/src/genkit_anthropic/_models.py +++ b/packages/genkit-anthropic/src/genkit_anthropic/_models.py @@ -756,7 +756,9 @@ def _to_anthropic_block(self, part: Part) -> dict[str, Any] | None: An Anthropic content block dict, or None if unrecognized. """ 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. + if reasoning is not None: # Claude only accepts thinking it signed. Unsigned reasoning, such as # history from a model that signs nothing, is dropped, the same as on # Bedrock's Claude path. diff --git a/packages/genkit-anthropic/tests/anthropic_models_test.py b/packages/genkit-anthropic/tests/anthropic_models_test.py index 1d36eef6..dce35142 100644 --- a/packages/genkit-anthropic/tests/anthropic_models_test.py +++ b/packages/genkit-anthropic/tests/anthropic_models_test.py @@ -41,6 +41,8 @@ ModelSupports, OutputConfig, ToolDefinition, + ToolRequest, + ToolResponse, ) @@ -1828,8 +1830,8 @@ async def test_generate_claude_drops_thinking_part_without_thought_signature( assert sent[1] == {'role': 'assistant', 'content': [{'type': 'text', 'text': '391'}]} -def test_empty_reasoning_part_is_skipped() -> None: - """Test that empty reasoning parts produce no Anthropic block.""" +def test_empty_signed_reasoning_part_encodes_as_thinking_block() -> None: + """Claude can sign a thinking block with empty text; it must be sent back, not dropped.""" mock_client = MagicMock() model = AnthropicModel(model_name='claude-sonnet-4', client=mock_client) @@ -1840,10 +1842,53 @@ def test_empty_reasoning_part_is_skipped() -> None: ), ] + anthropic_messages = model._to_anthropic_messages(messages) + assert anthropic_messages[0]['content'] == [{'type': 'thinking', 'thinking': '', 'signature': 'sig-abc'}] + + +def test_empty_unsigned_reasoning_part_is_skipped() -> None: + """Test that empty reasoning without a signature produces no Anthropic block.""" + mock_client = MagicMock() + model = AnthropicModel(model_name='claude-sonnet-4', client=mock_client) + + messages = [Message(role=Role.MODEL, content=[Part.from_reasoning('')])] + anthropic_messages = model._to_anthropic_messages(messages) assert anthropic_messages[0]['content'] == [] +@pytest.mark.asyncio +async def test_generate_claude_sends_back_empty_signed_thinking_before_tool_use() -> None: + """An empty signed thinking block from a tool-call turn stays at the start of that turn.""" + mock_client = _mock_client_for_generate() + # The plugin checks the client for a credential before each call. + mock_client.api_key = 'test-key' + plugin = Anthropic(api_key='test-key') + plugin._runtime_client = lambda: mock_client + ai = Genkit(plugins=[plugin]) + history = [ + Message(role=Role.USER, content=[Part.from_text('what is the weather in Paris?')]), + Message( + role=Role.MODEL, + content=[ + Part.from_reasoning('', metadata={'thoughtSignature': 'sig-abc'}), + Part(tool_request=ToolRequest(ref='toolu_1', name='getWeather', input={'city': 'Paris'})), + ], + ), + Message( + role=Role.TOOL, + content=[Part(tool_response=ToolResponse(ref='toolu_1', name='getWeather', output='sunny'))], + ), + ] + + 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 + sent = create.call_args.kwargs['messages'] + assert sent[1]['content'][0] == {'type': 'thinking', 'thinking': '', 'signature': 'sig-abc'} + assert sent[1]['content'][1]['type'] == 'tool_use' + + def test_redacted_thinking_part_round_trips() -> None: """Test that redacted thinking custom data encodes as a redacted block.""" mock_client = MagicMock() From 65eb459016b655fa407f2b08e52f9123088f9d1c Mon Sep 17 00:00:00 2001 From: hkarmoush Date: Mon, 16 Nov 2026 23:06:40 +0300 Subject: [PATCH 2/4] fix(py/plugins/anthropic): clarify signed-reasoning comment and assert standard endpoint in test --- packages/genkit-anthropic/src/genkit_anthropic/_models.py | 2 +- packages/genkit-anthropic/tests/anthropic_models_test.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/genkit-anthropic/src/genkit_anthropic/_models.py b/packages/genkit-anthropic/src/genkit_anthropic/_models.py index 454bf851..67cf611a 100644 --- a/packages/genkit-anthropic/src/genkit_anthropic/_models.py +++ b/packages/genkit-anthropic/src/genkit_anthropic/_models.py @@ -757,7 +757,7 @@ def _to_anthropic_block(self, part: Part) -> dict[str, Any] | None: """ reasoning = part.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. + # must start with it, so signed empty reasoning is kept. if reasoning is not None: # Claude only accepts thinking it signed. Unsigned reasoning, such as # history from a model that signs nothing, is dropped, the same as on diff --git a/packages/genkit-anthropic/tests/anthropic_models_test.py b/packages/genkit-anthropic/tests/anthropic_models_test.py index dce35142..51cc8d4a 100644 --- a/packages/genkit-anthropic/tests/anthropic_models_test.py +++ b/packages/genkit-anthropic/tests/anthropic_models_test.py @@ -1883,8 +1883,9 @@ async def test_generate_claude_sends_back_empty_signed_thinking_before_tool_use( 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 - sent = create.call_args.kwargs['messages'] + # No beta config here, so this must go through the standard endpoint. + mock_client.messages.create.assert_awaited_once() + sent = mock_client.messages.create.call_args.kwargs['messages'] assert sent[1]['content'][0] == {'type': 'thinking', 'thinking': '', 'signature': 'sig-abc'} assert sent[1]['content'][1]['type'] == 'tool_use' From 9aad71fa0c0f335b7c53d590e605b6e88e97bbb2 Mon Sep 17 00:00:00 2001 From: hkarmoush Date: Mon, 16 Nov 2026 23:31:49 +0300 Subject: [PATCH 3/4] fix(py/plugins/bedrock): keep signed reasoning with empty text 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. --- .../src/genkit_amazon_bedrock/_converters.py | 4 ++- .../src/genkit_amazon_bedrock/_stream.py | 2 +- .../tests/converters_test.py | 21 +++++++++++ .../tests/stream_test.py | 36 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/_converters.py b/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/_converters.py index e59dcf8e..76d6ded4 100644 --- a/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/_converters.py +++ b/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/_converters.py @@ -325,7 +325,9 @@ def _reasoning_part_to_blocks(part: Part) -> list[dict[str, Any]]: blocks.append({'reasoningContent': {'redactedContent': redacted}}) signature = _metadata_str(metadata, REASONING_SIGNATURE_METADATA_KEY) text = part.reasoning or '' - if text and signature: + # Claude can sign a thinking block with empty text, and a tool-use turn has to + # start with it, so any signed reasoning is kept. + if signature: blocks.append({'reasoningContent': {'reasoningText': {'text': text, 'signature': signature}}}) return blocks diff --git a/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/_stream.py b/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/_stream.py index 505ddd3e..168fe6ad 100644 --- a/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/_stream.py +++ b/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/_stream.py @@ -200,7 +200,7 @@ def _blocks_to_parts(blocks: dict[int, _StreamBlock], tools: list[ToolDefinition continue # One block can carry both reasoning and text; reasoning comes first. reasoning = ''.join(block.reasoning) - if reasoning or block.redacted: + if reasoning or block.redacted or block.signature: parts.append(bedrock_reasoning_part(reasoning, block.signature, bytes(block.redacted) or None)) text = ''.join(block.text) if text: diff --git a/packages/genkit-amazon-bedrock/tests/converters_test.py b/packages/genkit-amazon-bedrock/tests/converters_test.py index e9f402d0..01f20c02 100644 --- a/packages/genkit-amazon-bedrock/tests/converters_test.py +++ b/packages/genkit-amazon-bedrock/tests/converters_test.py @@ -626,6 +626,27 @@ def test_redacted_only_reasoning_part_still_replays() -> None: assert kwargs['messages'][0]['content'] == [{'reasoningContent': {'redactedContent': b'blob'}}] +def test_empty_signed_reasoning_part_still_replays() -> None: + # Claude can sign a thinking block with empty text; it must be sent back, not dropped. + part = Part.from_reasoning('', metadata={REASONING_SIGNATURE_METADATA_KEY: 'sig-abc'}) + request = ModelRequest(messages=[Message(role=Role.MODEL, content=[part])]) + kwargs = build_converse_request('amazon.nova-lite-v1:0', request) + assert kwargs['messages'][0]['content'] == [ + {'reasoningContent': {'reasoningText': {'text': '', 'signature': 'sig-abc'}}} + ] + + +def test_empty_unsigned_reasoning_part_is_not_replayed() -> None: + request = ModelRequest( + messages=[ + Message(role=Role.USER, content=[Part.from_text('q')]), + Message(role=Role.MODEL, content=[Part.from_reasoning('')]), + ] + ) + kwargs = build_converse_request('amazon.nova-lite-v1:0', request) + assert [m['role'] for m in kwargs['messages']] == ['user'] + + def test_generic_reasoning_part_is_not_replayed() -> None: request = ModelRequest( messages=[ diff --git a/packages/genkit-amazon-bedrock/tests/stream_test.py b/packages/genkit-amazon-bedrock/tests/stream_test.py index 0075cf2e..2c2e4e45 100644 --- a/packages/genkit-amazon-bedrock/tests/stream_test.py +++ b/packages/genkit-amazon-bedrock/tests/stream_test.py @@ -309,6 +309,42 @@ async def test_reasoning_text_streams_and_signature_accumulates() -> None: assert text_part.text == 'answer' +async def test_signed_reasoning_with_empty_text_is_kept() -> None: + ctx, chunks = streaming_ctx() + + response = await consume_converse_stream( + events( + reasoning_delta(0, {'signature': 'sig-abc'}), + tool_start(1, 'call_1', 'noop'), + tool_delta(1, '{}'), + tool_stop(1), + message_stop('tool_use'), + ), + text_request(), + ctx, + ) + + assert response.message is not None + reasoning_part, tool_part = response.message.content + assert reasoning_part.reasoning == '' + assert reasoning_part.metadata is not None + assert reasoning_part.metadata[REASONING_SIGNATURE_METADATA_KEY] == 'sig-abc' + assert tool_part.tool_request is not None + + +async def test_reasoning_without_text_or_signature_is_dropped() -> None: + ctx, _ = streaming_ctx() + + response = await consume_converse_stream( + events(text_delta(0, 'answer'), message_stop('end_turn')), + text_request(), + ctx, + ) + + assert response.message is not None + assert [part.text for part in response.message.content] == ['answer'] + + async def test_redacted_reasoning_accumulates_as_base64_without_chunks() -> None: ctx, chunks = streaming_ctx() From c9d2406db5453f0eca9b4c52ea75cd76707f09c7 Mon Sep 17 00:00:00 2001 From: Jeff Huang Date: Mon, 30 Nov 2026 19:38:12 +0000 Subject: [PATCH 4/4] test(bedrock): send empty reasoning delta in unsigned drop test --- packages/genkit-amazon-bedrock/tests/stream_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/genkit-amazon-bedrock/tests/stream_test.py b/packages/genkit-amazon-bedrock/tests/stream_test.py index 2c2e4e45..9d822c2d 100644 --- a/packages/genkit-amazon-bedrock/tests/stream_test.py +++ b/packages/genkit-amazon-bedrock/tests/stream_test.py @@ -336,7 +336,7 @@ async def test_reasoning_without_text_or_signature_is_dropped() -> None: ctx, _ = streaming_ctx() response = await consume_converse_stream( - events(text_delta(0, 'answer'), message_stop('end_turn')), + events(reasoning_delta(0, {'text': ''}), text_delta(1, 'answer'), message_stop('end_turn')), text_request(), ctx, )