From 892fc7ed588380372e5ccd2b02ce1064bfdd41e9 Mon Sep 17 00:00:00 2001 From: ManuelPeixotoCegid Date: Wed, 8 Oct 2025 15:54:26 +0100 Subject: [PATCH 1/3] added logic to extract text from ThinkingBlock --- autogen/oai/anthropic.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/autogen/oai/anthropic.py b/autogen/oai/anthropic.py index eeb8e2729dd..a7d9ce53332 100644 --- a/autogen/oai/anthropic.py +++ b/autogen/oai/anthropic.py @@ -89,7 +89,7 @@ with optional_import_block(): from anthropic import Anthropic, AnthropicBedrock, AnthropicVertex from anthropic import __version__ as anthropic_version - from anthropic.types import Message, TextBlock, ToolUseBlock + from anthropic.types import Message, TextBlock, ToolUseBlock,ThinkingBlock TOOL_ENABLED = anthropic_version >= "0.23.1" if TOOL_ENABLED: @@ -486,7 +486,14 @@ def _extract_json_response(self, response: Message) -> Any: return response # Extract content from response - content = response.content[0].text if response.content else "" + if response.content: + if type(response.content[0]) == TextBlock: + content = response.content[0].text + + elif type(response.content[0]) == ThinkingBlock: + content = response.content[0].thinking + else: + content = "" # Try to extract JSON from tags first json_match = re.search(r"(.*?)", content, re.DOTALL) From d2dec0534cbfcc8a023f72cd02a1bfbf6cab30c4 Mon Sep 17 00:00:00 2001 From: ManuelPeixotoCegid Date: Thu, 9 Oct 2025 09:11:04 +0100 Subject: [PATCH 2/3] Reorder imports in anthropic.py --- autogen/oai/anthropic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogen/oai/anthropic.py b/autogen/oai/anthropic.py index a7d9ce53332..23056d86879 100644 --- a/autogen/oai/anthropic.py +++ b/autogen/oai/anthropic.py @@ -89,7 +89,7 @@ with optional_import_block(): from anthropic import Anthropic, AnthropicBedrock, AnthropicVertex from anthropic import __version__ as anthropic_version - from anthropic.types import Message, TextBlock, ToolUseBlock,ThinkingBlock + from anthropic.types import Message, TextBlock, ThinkingBlock, ToolUseBlock TOOL_ENABLED = anthropic_version >= "0.23.1" if TOOL_ENABLED: From 02e356895ce700dd7e6dfeabda2530960a9abc4f Mon Sep 17 00:00:00 2001 From: ManuelPeixotoCegid Date: Fri, 24 Oct 2025 10:19:18 +0100 Subject: [PATCH 3/3] added unit test for thinking block --- test/oai/test_anthropic.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/oai/test_anthropic.py b/test/oai/test_anthropic.py index a6e3a1c2470..0a13d8fa086 100644 --- a/test/oai/test_anthropic.py +++ b/test/oai/test_anthropic.py @@ -13,7 +13,7 @@ from autogen.oai.anthropic import AnthropicClient, AnthropicLLMConfigEntry, _calculate_cost with optional_import_block() as result: - from anthropic.types import Message, TextBlock + from anthropic.types import Message, TextBlock, ThinkingBlock from typing import Literal @@ -291,6 +291,35 @@ class MathReasoning(BaseModel): with pytest.raises(ValueError, match="No valid JSON found in response for Structured Output."): anthropic_client._extract_json_response(no_json_response) + + # Test case 5: Plain JSON without tags, using ThinkingBlock - SHOULD STILL PASS + plain_response = Message( + id="msg_123", + content=[ + ThinkingBlock( + signature="json_response", + thinking="""Here's the solution: + { + "steps": [ + {"explanation": "Step 1", "output": "8x = -30"}, + {"explanation": "Step 2", "output": "x = -3.75"} + ], + "final_answer": "x = -3.75" + }""", + type="thinking", + ) + ], + model="claude-3-5-sonnet-latest", + role="assistant", + stop_reason="end_turn", + type="message", + usage={"input_tokens": 10, "output_tokens": 25}, + ) + + result = anthropic_client._extract_json_response(plain_response) + assert isinstance(result, MathReasoning) + assert len(result.steps) == 2 + assert result.final_answer == "x = -3.75" @run_for_optional_imports(["anthropic"], "anthropic")