Skip to content

Build and run agents you can see, understand and trust.

fix(models): preserve Responses reasoning text (#2916)

kinoe29e9ff0c9ec
diff --git a/src/agentscope/formatter/_openai_response_formatter.py b/src/agentscope/formatter/_openai_response_formatter.py
index bd55b7da9..5f337c629 100644
--- a/src/agentscope/formatter/_openai_response_formatter.py
+++ b/src/agentscope/formatter/_openai_response_formatter.py
@@ -197,6 +197,7 @@ class OpenAIResponseFormatter(_OpenAIResponseFormatterBase):
msg = msgs[i]
content_parts: list[dict] = []
function_calls: list[dict] = []
+ reasoning_item_ids: set[str] = set()
for block in msg.get_content_blocks():
if isinstance(block, TextBlock):
@@ -282,6 +283,12 @@ class OpenAIResponseFormatter(_OpenAIResponseFormatterBase):
None,
)
if reasoning_item_id:
+ # Summary and raw reasoning can occupy separate
+ # blocks while sharing one upstream reasoning item.
+ # Replay that complete item only once per message.
+ if reasoning_item_id in reasoning_item_ids:
+ continue
+ reasoning_item_ids.add(reasoning_item_id)
if content_parts and block.thinking:
items.append(
{
diff --git a/src/agentscope/model/_openai_response/_model.py b/src/agentscope/model/_openai_response/_model.py
index e283e5ba2..569ba12b7 100644
--- a/src/agentscope/model/_openai_response/_model.py
+++ b/src/agentscope/model/_openai_response/_model.py
@@ -282,7 +282,7 @@ class OpenAIResponseModel(ChatModelBase):
usage: ChatUsage | None = None
response_id: str = _generate_id()
text_id: str = _generate_id()
- reasoning_block_ids: dict[str, str] = {}
+ reasoning_block_ids: dict[str, dict[tuple[str, int], str]] = {}
# Mapping from Responses API item id (fc_xxx) to (call_id, name)
# so subsequent argument deltas can be routed to the right tool
# call block.
@@ -310,15 +310,25 @@ class OpenAIResponseModel(ChatModelBase):
id=response_id,
)
- if event_type == "response.reasoning_summary_text.delta":
- # Reasoning summary text is NOT emitted by all models.
- # As of 2026-05, o1 and o4-mini do not stream reasoning
- # summary deltas. This handler exists for forward
- # compatibility with models that do expose it.
+ if event_type in {
+ "response.reasoning_summary_text.delta",
+ "response.reasoning_text.delta",
+ }:
+ # Summary and raw reasoning are distinct representations
+ # and can both contain multiple parts for the same item.
+ part_index = (
+ event.summary_index
+ if event_type
+ == "response.reasoning_summary_text.delta"
+ else event.content_index
+ )
delta_res.append_thinking(
event.delta,
block_id=reasoning_block_ids.setdefault(
event.item_id,
+ {},
+ ).setdefault(
+ (event_type, part_index),
_generate_id(),
),
)
@@ -385,17 +395,22 @@ class OpenAIResponseModel(ChatModelBase):
None,
)
if reasoning_item_id:
- delta_res.append_thinking(
- thinking="",
- block_id=reasoning_block_ids.setdefault(
- reasoning_item_id,
- _generate_id(),
- ),
- reasoning_item_id=reasoning_item_id,
- reasoning_item_raw=(
- _dump_reasoning_item(output_item)
- ),
+ block_ids = reasoning_block_ids.setdefault(
+ reasoning_item_id,
+ {},
+ )
+ if not block_ids:
+ block_ids[("metadata", 0)] = _generate_id()
+ reasoning_item_raw = _dump_reasoning_item(
+ output_item,
)
+ for block_id in block_ids.values():
+ delta_res.append_thinking(
+ thinking="",
+ block_id=block_id,
+ reasoning_item_id=reasoning_item_id,
+ reasoning_item_raw=reasoning_item_raw,
+ )
if delta_res.content or usage:
delta_res.usage = usage
@@ -426,18 +441,30 @@ class OpenAIResponseModel(ChatModelBase):
if item_type == "reasoning":
reasoning_item_id = getattr(item, "id", None)
reasoning_item_raw = _dump_reasoning_item(item)
- combined_summary = " ".join(
- getattr(s, "text", "")
- for s in getattr(item, "summary", [])
- if getattr(s, "text", "")
- )
+ # Serialize once to handle both SDK content objects and
+ # provider-specific content dictionaries uniformly. Keep
+ # summary and raw reasoning in separate thinking blocks.
+ summary_parts = [
+ part["text"]
+ for part in reasoning_item_raw.get("summary", []) or []
+ if part.get("text")
+ ]
+ reasoning_parts = [
+ part["text"]
+ for part in reasoning_item_raw.get("content", []) or []
+ if part.get("type") == "reasoning_text"
+ and part.get("text")
+ ]
+ thinking_texts = summary_parts + reasoning_parts
# Keep even empty-summary reasoning items: the API requires
# reasoning_item_id to be echoed back in multi-turn history.
- if combined_summary or reasoning_item_id:
+ if not thinking_texts and reasoning_item_id:
+ thinking_texts = [""]
+ for thinking in thinking_texts:
content_blocks.append(
ThinkingBlock(
type="thinking",
- thinking=combined_summary,
+ thinking=thinking,
reasoning_item_id=reasoning_item_id,
reasoning_item_raw=reasoning_item_raw,
),
diff --git a/tests/formatter_openai_response_test.py b/tests/formatter_openai_response_test.py
index f9e80aa95..b6899e3a0 100644
--- a/tests/formatter_openai_response_test.py
+++ b/tests/formatter_openai_response_test.py
@@ -431,7 +431,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
)
async def test_chat_formatter_replays_raw_reasoning_item(self) -> None:
- """A valid raw reasoning item is replayed without reconstruction."""
+ """Split thinking blocks replay one complete item before tool calls."""
reasoning_item_raw = {
"type": "reasoning",
"id": "rs_encrypted",
@@ -445,7 +445,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"status": "completed",
}
thinking = ThinkingBlock(
- thinking="",
+ thinking="summary",
reasoning_item_id="rs_encrypted",
reasoning_item_raw=reasoning_item_raw,
)
@@ -455,7 +455,23 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
[
AssistantMsg(
name="assistant",
- content=[thinking, TextBlock(text="reply")],
+ content=[
+ thinking,
+ TextBlock(text="reply"),
+ thinking.model_copy(
+ update={
+ "id": "raw-block",
+ "thinking": "reasoning",
+ },
+ ),
+ ToolCallBlock(id="call_1", name="lookup", input="{}"),
+ ToolResultBlock(
+ id="call_1",
+ name="lookup",
+ output=[TextBlock(text="result")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ],
),
],
)
@@ -470,6 +486,17 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
{"type": "output_text", "text": "reply"},
],
},
+ {
+ "type": "function_call",
+ "call_id": "call_1",
+ "name": "lookup",
+ "arguments": "{}",
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call_1",
+ "output": "result",
+ },
],
)
self.assertIsNot(res[0], reasoning_item_raw)
diff --git a/tests/model_openai_response_test.py b/tests/model_openai_response_test.py
index a1a79226c..b2547fe0d 100644
--- a/tests/model_openai_response_test.py
+++ b/tests/model_openai_response_test.py
@@ -409,6 +409,98 @@ class TestOpenAIResponseNonStream(IsolatedAsyncioTestCase):
],
)
+ async def test_reasoning_content_with_optional_summary(self) -> None:
+ """Raw reasoning remains visible and separate from its summary."""
+ for summary in ([], [_MockReasoningSummary(text="Summary")]):
+ with self.subTest(summary=summary):
+ item = _MockReasoningItem(
+ id="rs_content",
+ summary=summary,
+ content=[
+ {
+ "type": "reasoning_text",
+ "text": "Raw reasoning",
+ },
+ ],
+ )
+ self.mock_client.responses.create = AsyncMock(
+ return_value=_mock_completion(
+ text="Answer",
+ reasoning_output_item=item,
+ ),
+ )
+ result = await self.model([])
+ texts = (["Summary"] if summary else []) + ["Raw reasoning"]
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ *[
+ ThinkingBlock.model_construct(
+ id=A,
+ created_at=A,
+ thinking=text,
+ reasoning_item_id=item.id,
+ reasoning_item_raw=item.model_dump(
+ exclude_none=True,
+ ),
+ )
+ for text in texts
+ ],
+ TextBlock.model_construct(
+ id=A,
+ created_at=A,
+ text="Answer",
+ ),
+ ],
+ ),
+ )
+
+ async def test_reasoning_preserves_part_boundaries(self) -> None:
+ """Each summary and reasoning content part remains separate."""
+ item = _MockReasoningItem(
+ id="rs_parts",
+ summary=[
+ _MockReasoningSummary(text="Summary one"),
+ _MockReasoningSummary(text="Summary two"),
+ ],
+ content=[
+ {"type": "reasoning_text", "text": "Reasoning one"},
+ {"type": "reasoning_text", "text": "Reasoning two"},
+ ],
+ )
+ self.mock_client.responses.create = AsyncMock(
+ return_value=_mock_completion(
+ reasoning_output_item=item,
+ ),
+ )
+
+ result = await self.model([])
+ raw = item.model_dump(exclude_none=True)
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ created_at=A,
+ thinking=text,
+ reasoning_item_id=item.id,
+ reasoning_item_raw=raw,
+ )
+ for text in (
+ "Summary one",
+ "Summary two",
+ "Reasoning one",
+ "Reasoning two",
+ )
+ ],
+ ),
+ )
+
async def test_reasoning_raw_item_excludes_none_fields(self) -> None:
"""Optional null SDK fields are not stored for history replay."""
reasoning_item = _MockReasoningItem.model_validate(
@@ -655,6 +747,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):
"response.reasoning_summary_text.delta",
delta="Thinking",
item_id="rs_123",
+ summary_index=0,
response=MagicMock(id="resp-2"),
),
_make_event(
@@ -730,6 +823,171 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):
],
)
+ async def test_stream_reasoning_text_with_optional_summary(self) -> None:
+ """Raw and summary deltas stay separate without done repeats."""
+ parts = {
+ "reasoning_summary_text": ("Summary ", "text"),
+ "reasoning_text": ("Raw ", "reasoning"),
+ }
+ for kinds in (
+ ("reasoning_text",),
+ ("reasoning_summary_text", "reasoning_text"),
+ ):
+ with self.subTest(kinds=kinds):
+ item = _MockReasoningItem(
+ id="rs_text",
+ summary=[_MockReasoningSummary(text="Summary text")]
+ if len(kinds) == 2
+ else [],
+ content=[
+ {"type": "reasoning_text", "text": "Raw reasoning"},
+ ],
+ )
+ raw = item.model_dump(exclude_none=True)
+ events = [
+ _make_event(
+ f"response.{kind}.delta",
+ item_id=item.id,
+ delta=parts[kind][index],
+ **(
+ {"summary_index": 0}
+ if kind == "reasoning_summary_text"
+ else {"content_index": 0}
+ ),
+ )
+ for index in range(2)
+ for kind in kinds
+ ]
+ events.extend(
+ _make_event(
+ f"response.{kind}.done",
+ item_id=item.id,
+ text="".join(parts[kind]),
+ )
+ for kind in kinds
+ )
+ events.extend(
+ [
+ _make_event(
+ "response.output_text.delta",
+ delta="Answer",
+ ),
+ _make_event(
+ "response.completed",
+ response=_mock_completion(
+ reasoning_output_item=item,
+ ),
+ ),
+ ],
+ )
+ self.mock_client.responses.create = AsyncMock(
+ return_value=_MockAsyncEventStream(events),
+ )
+ responses = [r async for r in await self.model([])]
+ answer = TextBlock.model_construct(
+ id=A,
+ created_at=A,
+ text="Answer",
+ )
+ self.assertEqual(
+ (responses[0].is_last, responses[0].content),
+ (
+ False,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ created_at=A,
+ thinking=parts[kinds[0]][0],
+ ),
+ ],
+ ),
+ )
+ self.assertEqual(
+ (responses[-1].is_last, responses[-1].content),
+ (
+ True,
+ [
+ *[
+ ThinkingBlock.model_construct(
+ id=A,
+ created_at=A,
+ thinking="".join(parts[kind]),
+ reasoning_item_id=item.id,
+ reasoning_item_raw=raw,
+ )
+ for kind in kinds
+ ],
+ answer,
+ ],
+ ),
+ )
+
+ async def test_stream_reasoning_preserves_part_boundaries(self) -> None:
+ """Reasoning stream indices identify independent thinking blocks."""
+ item = _MockReasoningItem(
+ id="rs_parts",
+ summary=[
+ _MockReasoningSummary(text="Summary one"),
+ _MockReasoningSummary(text="Summary two"),
+ ],
+ content=[
+ {"type": "reasoning_text", "text": "Reasoning one"},
+ {"type": "reasoning_text", "text": "Reasoning two"},
+ ],
+ )
+ events = [
+ _make_event(
+ "response.reasoning_summary_text.delta",
+ item_id=item.id,
+ summary_index=index,
+ delta=part.text,
+ )
+ for index, part in enumerate(item.summary)
+ ]
+ events.extend(
+ _make_event(
+ "response.reasoning_text.delta",
+ item_id=item.id,
+ content_index=index,
+ delta=part["text"],
+ )
+ for index, part in enumerate(item.content or [])
+ )
+ events.append(
+ _make_event(
+ "response.completed",
+ response=_mock_completion(reasoning_output_item=item),
+ ),
+ )
+ self.mock_client.responses.create = AsyncMock(
+ return_value=_MockAsyncEventStream(events),
+ )
+
+ responses = [response async for response in await self.model([])]
+ raw = item.model_dump(exclude_none=True)
+
+ self.assertEqual(
+ (responses[-1].is_last, responses[-1].content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ created_at=A,
+ thinking=text,
+ reasoning_item_id=item.id,
+ reasoning_item_raw=raw,
+ )
+ for text in (
+ "Summary one",
+ "Summary two",
+ "Reasoning one",
+ "Reasoning two",
+ )
+ ],
+ ),
+ )
+
async def test_stream_preserves_multiple_reasoning_items(self) -> None:
"""Streaming keeps every reasoning item's encrypted payload."""
first_raw = {
@@ -760,11 +1018,13 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):
"response.reasoning_summary_text.delta",
delta="First",
item_id="rs_first",
+ summary_index=0,
),
_make_event(
"response.reasoning_summary_text.delta",
delta="Second",
item_id="rs_second",
+ summary_index=0,
),
_make_event("response.completed", response=completed_resp),
]