The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): flatten list-shaped thinking block payloads in extract_reasoning
liuhao102477fd36317fda
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.pyindex 51c12f20f6b..96ed1c21cd2 100644--- a/agent/agent_runtime_helpers.py+++ b/agent/agent_runtime_helpers.py@@ -1334,11 +1334,11 @@ _INLINE_REASONING_PATTERNS = tuple(def extract_reasoning(agent, assistant_message) -> Optional[str]:"""Reasoning text from ``reasoning`` / ``reasoning_content`` / ``reasoning_details``(OpenRouter unified), else inline thinking blocks in the content; None when absent."""+ from agent.message_content import flatten_message_text+parts: List[str] = []def _add(text) -> None:- from agent.message_content import flatten_message_text-text = flatten_message_text(text, sep="")if text and text not in parts:parts.append(text)@@ -1356,7 +1356,10 @@ def extract_reasoning(agent, assistant_message) -> Optional[str]:# Refs #21944.for block in content:if isinstance(block, dict) and block.get("type") == "thinking":- _add((block.get("thinking") or block.get("text") or "").strip())+ # Non-strict OpenAI-compatible backends (Mistral via custom provider)+ # deliver the thinking value as a JSON array, not a string (#106006);+ # flatten first so .strip() never sees a list.+ _add(flatten_message_text(block.get("thinking") or block.get("text") or "", sep="").strip())if not parts and isinstance(content, str) and content:for pattern in _INLINE_REASONING_PATTERNS:for block in pattern.findall(content):diff --git a/tests/agent/test_run_agent.py b/tests/agent/test_run_agent.pyindex a4b964f0e72..e066c42fd48 100644--- a/tests/agent/test_run_agent.py+++ b/tests/agent/test_run_agent.py@@ -452,6 +452,27 @@ class TestExtractReasoning:msg = _mock_assistant_msg(reasoning="thinking hard")assert agent._extract_reasoning(msg) == "thinking hard"+ def test_thinking_block_string_payload_still_extracted(self, agent):+ msg = _mock_assistant_msg(+ content=[{"type": "thinking", "thinking": " block reasoning "}]+ )+ assert agent._extract_reasoning(msg) == "block reasoning"++ def test_thinking_block_list_payload_flattened_not_crashed(self, agent):+ # Non-strict OpenAI-compatible backends (Mistral via custom provider) can+ # deliver the thinking value as a JSON array; .strip() on a list crashed+ # the whole API call with AttributeError (#106006). Flatten instead.+ msg = _mock_assistant_msg(+ content=[{"type": "thinking", "thinking": ["list-shaped reasoning", "part two"]}]+ )+ assert agent._extract_reasoning(msg) == "list-shaped reasoningpart two"++ def test_thinking_block_whitespace_only_payload_dropped(self, agent):+ msg = _mock_assistant_msg(+ content=[{"type": "thinking", "thinking": " "}]+ )+ assert agent._extract_reasoning(msg) is None+class TestSessionFilenameSafety:def test_safe_session_filename_component_contains_traversal(self):