Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(model): preserve encrypted reasoning items in OpenAI Responses history (#2426)
qbcd38ce0521cdb
diff --git a/src/agentscope/formatter/_openai_response_formatter.py b/src/agentscope/formatter/_openai_response_formatter.pyindex e526832e9..322751b10 100644--- a/src/agentscope/formatter/_openai_response_formatter.py+++ b/src/agentscope/formatter/_openai_response_formatter.py@@ -1,6 +1,7 @@# -*- coding: utf-8 -*-"""Formatters for the OpenAI Responses API."""from abc import ABC+from copy import deepcopyfrom fnmatch import fnmatchfrom typing import Any@@ -294,19 +295,37 @@ class OpenAIResponseFormatter(_OpenAIResponseFormatterBase):# pending assistant text for replay. Non-empty# reasoning starts a new output segment, so flush text# first.- summary = (- [{"type": "summary_text", "text": block.thinking}]- if block.thinking- else []- )- items.append(- {- "type": "reasoning",- "id": reasoning_item_id,- "summary": summary,- "content": [],- },+ reasoning_item_raw = getattr(+ block,+ "reasoning_item_raw",+ None,)+ if (+ isinstance(reasoning_item_raw, dict)+ and reasoning_item_raw.get("type") == "reasoning"+ and reasoning_item_raw.get("id")+ == reasoning_item_id+ ):+ items.append(deepcopy(reasoning_item_raw))+ else:+ summary = (+ [+ {+ "type": "summary_text",+ "text": block.thinking,+ },+ ]+ if block.thinking+ else []+ )+ items.append(+ {+ "type": "reasoning",+ "id": reasoning_item_id,+ "summary": summary,+ "content": [],+ },+ )elif isinstance(block, ToolCallBlock):function_calls.append(diff --git a/src/agentscope/model/_openai_response/_model.py b/src/agentscope/model/_openai_response/_model.pyindex 3f3ed3834..1e8059529 100644--- a/src/agentscope/model/_openai_response/_model.py+++ b/src/agentscope/model/_openai_response/_model.py@@ -28,6 +28,27 @@ else:_RESPONSES_UNSUPPORTED_KWARGS = frozenset({"modalities", "audio"})+def _dump_reasoning_item(item: Any) -> dict[str, Any]:+ """Serialize a Responses API reasoning item for history replay.++ ``exclude_none=True`` avoids emitting optional response-only fields as+ explicit ``null`` values, which some Responses-compatible APIs reject+ when the item is replayed as input.++ Args:+ item (`Any`):+ The reasoning item returned by the OpenAI SDK.++ Returns:+ `dict[str, Any]`:+ The JSON-safe item dictionary.+ """+ return item.model_dump(+ mode="json",+ exclude_none=True,+ )++class OpenAIResponseModel(ChatModelBase):"""The OpenAI Responses API chat model.@@ -266,7 +287,7 @@ class OpenAIResponseModel(ChatModelBase):usage: ChatUsage | None = Noneresponse_id: str = _generate_id()text_id: str = _generate_id()- thinking_id: str = _generate_id()+ reasoning_block_ids: dict[str, 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.@@ -288,7 +309,10 @@ class OpenAIResponseModel(ChatModelBase):# compatibility with models that do expose it.delta_res.append_thinking(event.delta,- block_id=thinking_id,+ block_id=reasoning_block_ids.setdefault(+ event.item_id,+ _generate_id(),+ ),)elif event_type == "response.output_text.delta":@@ -355,8 +379,14 @@ class OpenAIResponseModel(ChatModelBase):if reasoning_item_id:delta_res.append_thinking(thinking="",- block_id=thinking_id,+ 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)+ ),)if delta_res.content or usage:@@ -387,6 +417,7 @@ 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", [])@@ -400,6 +431,7 @@ class OpenAIResponseModel(ChatModelBase):type="thinking",thinking=combined_summary,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.pyindex 557be8c78..f9e80aa95 100644--- a/tests/formatter_openai_response_test.py+++ b/tests/formatter_openai_response_test.py@@ -430,6 +430,98 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):res,)+ async def test_chat_formatter_replays_raw_reasoning_item(self) -> None:+ """A valid raw reasoning item is replayed without reconstruction."""+ reasoning_item_raw = {+ "type": "reasoning",+ "id": "rs_encrypted",+ "summary": [+ {"type": "summary_text", "text": "summary"},+ ],+ "content": [+ {"type": "reasoning_text", "text": "reasoning"},+ ],+ "encrypted_content": "encrypted_payload",+ "status": "completed",+ }+ thinking = ThinkingBlock(+ thinking="",+ reasoning_item_id="rs_encrypted",+ reasoning_item_raw=reasoning_item_raw,+ )+ fmt = OpenAIResponseFormatter()++ res = await fmt.format(+ [+ AssistantMsg(+ name="assistant",+ content=[thinking, TextBlock(text="reply")],+ ),+ ],+ )++ self.assertListEqual(+ res,+ [+ reasoning_item_raw,+ {+ "role": "assistant",+ "content": [+ {"type": "output_text", "text": "reply"},+ ],+ },+ ],+ )+ self.assertIsNot(res[0], reasoning_item_raw)+ self.assertIsNot(res[0]["summary"], reasoning_item_raw["summary"])+ self.assertIsNot(res[0]["content"], reasoning_item_raw["content"])++ async def test_chat_formatter_invalid_raw_reasoning_uses_fallback(+ self,+ ) -> None:+ """Malformed raw items cannot bypass the reconstructed fallback."""+ fmt = OpenAIResponseFormatter()+ invalid_raw_items = [+ None,+ [],+ {+ "type": "message",+ "id": "rs_expected",+ },+ {+ "type": "reasoning",+ "id": "rs_other",+ "summary": [],+ },+ ]++ for reasoning_item_raw in invalid_raw_items:+ with self.subTest(reasoning_item_raw=reasoning_item_raw):+ thinking = ThinkingBlock(+ thinking="summary",+ reasoning_item_id="rs_expected",+ reasoning_item_raw=reasoning_item_raw,+ )+ res = await fmt.format(+ [AssistantMsg(name="assistant", content=[thinking])],+ )+ self.assertListEqual(+ res,+ [+ {+ "type": "reasoning",+ "id": "rs_expected",+ "summary": [+ {+ "type": "summary_text",+ "text": "summary",+ },+ ],+ "content": [],+ },+ ],+ )+async def test_chat_formatter_empty_thinking_echoed_with_reasoning_item_id(self,) -> None:diff --git a/tests/model_openai_response_test.py b/tests/model_openai_response_test.pyindex 42e6fb7d0..fb36adedd 100644--- a/tests/model_openai_response_test.py+++ b/tests/model_openai_response_test.py@@ -10,12 +10,15 @@ import unittestfrom unittest import IsolatedAsyncioTestCasefrom unittest.mock import AsyncMock, MagicMock+from pydantic import BaseModel+from utils import AnyStringfrom agentscope.message import (AssistantMsg,Base64Source,DataBlock,+ Msg,TextBlock,ToolCallBlock,ToolResultBlock,@@ -29,6 +32,24 @@ from agentscope.tool import ToolChoiceA = AnyString()+class _MockReasoningSummary(BaseModel):+ """Mock an OpenAI reasoning summary item."""++ text: str+ type: str = "summary_text"+++class _MockReasoningItem(BaseModel):+ """Mock an OpenAI reasoning output item."""++ id: str+ summary: list[_MockReasoningSummary]+ type: str = "reasoning"+ content: list[dict[str, Any]] | None = None+ encrypted_content: str | None = None+ status: str | None = None++# ---------------------------------------------------------------------------# Helpers# ---------------------------------------------------------------------------@@ -49,14 +70,15 @@ def _mock_completion(reasoning_summary: Any = None,reasoning_id: str = "rs_test123",response_id: str = "resp-openai-1",+ reasoning_output_item: Any = None,) -> MagicMock:"""Build a mock non-streaming Responses API response."""output = []- if reasoning_summary is not None:- reasoning_item = MagicMock()- reasoning_item.type = "reasoning"- reasoning_item.id = reasoning_id+ if reasoning_output_item is not None:+ output.append(reasoning_output_item)++ elif reasoning_summary is not None:summary_texts = (reasoning_summaryif isinstance(reasoning_summary, list)@@ -64,11 +86,13 @@ def _mock_completion(if reasoning_summary is not Noneelse [])- reasoning_item.summary = []- for summary_text in summary_texts:- summary_mock = MagicMock()- summary_mock.text = summary_text- reasoning_item.summary.append(summary_mock)+ reasoning_item = _MockReasoningItem(+ id=reasoning_id,+ summary=[+ _MockReasoningSummary(text=summary_text)+ for summary_text in summary_texts+ ],+ )output.append(reasoning_item)if text:@@ -288,6 +312,65 @@ class TestOpenAIResponseNonStream(IsolatedAsyncioTestCase):created_at=A,thinking="Thinking step...",reasoning_item_id="rs_abc999",+ reasoning_item_raw={+ "id": "rs_abc999",+ "summary": [+ {+ "text": "Thinking step...",+ "type": "summary_text",+ },+ ],+ "type": "reasoning",+ },+ ),+ TextBlock.model_construct(+ id=A,+ created_at=A,+ text="Answer",+ ),+ ],+ ),+ )++ async def test_reasoning_raw_item_round_trip(self) -> None:+ """Encrypted reasoning metadata survives parsing and formatting."""+ reasoning_item_raw = {+ "id": "rs_encrypted",+ "summary": [+ {+ "text": "Thinking step...",+ "type": "summary_text",+ },+ ],+ "type": "reasoning",+ "content": [],+ "encrypted_content": "encrypted_payload",+ "status": "completed",+ }+ reasoning_item = _MockReasoningItem.model_validate(+ reasoning_item_raw,+ )+ mock_create = AsyncMock(+ return_value=_mock_completion(+ text="Answer",+ reasoning_output_item=reasoning_item,+ ),+ )+ self.mock_client.responses.create = mock_create++ result = await self.model([])++ self.assertEqual(+ (result.is_last, result.content),+ (+ True,+ [+ ThinkingBlock.model_construct(+ id=A,+ created_at=A,+ thinking="Thinking step...",+ reasoning_item_id="rs_encrypted",+ reasoning_item_raw=reasoning_item_raw,),TextBlock.model_construct(id=A,@@ -298,6 +381,69 @@ class TestOpenAIResponseNonStream(IsolatedAsyncioTestCase):),)+ msg = AssistantMsg(+ name="assistant",+ content=result.content,+ )+ restored_msg = Msg.model_validate(msg.model_dump())+ self.assertEqual(restored_msg.content, result.content)++ formatted = await self.model.formatter.format(+ [restored_msg],+ )+ self.assertListEqual(+ formatted,+ [+ reasoning_item_raw,+ {+ "role": "assistant",+ "content": [+ {+ "type": "output_text",+ "text": "Answer",+ },+ ],+ },+ ],+ )++ 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(+ {+ "id": "rs_without_nulls",+ "summary": [],+ "type": "reasoning",+ "encrypted_content": "encrypted_payload",+ },+ )+ mock_create = AsyncMock(+ return_value=_mock_completion(+ reasoning_output_item=reasoning_item,+ ),+ )+ self.mock_client.responses.create = mock_create++ result = await self.model([])++ self.assertEqual(+ result.content,+ [+ ThinkingBlock.model_construct(+ id=A,+ created_at=A,+ thinking="",+ reasoning_item_id="rs_without_nulls",+ reasoning_item_raw={+ "id": "rs_without_nulls",+ "summary": [],+ "type": "reasoning",+ "encrypted_content": "encrypted_payload",+ },+ ),+ ],+ )+async def test_empty_reasoning_summary_response(self,) -> None:@@ -323,6 +469,11 @@ class TestOpenAIResponseNonStream(IsolatedAsyncioTestCase):created_at=A,thinking="",reasoning_item_id="rs_empty",+ reasoning_item_raw={+ "id": "rs_empty",+ "summary": [],+ "type": "reasoning",+ },),TextBlock.model_construct(id=A,@@ -447,9 +598,14 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):) -> None:"""Stream reasoning and text deltas then final withreasoning_item_id."""- reasoning_item = MagicMock()- reasoning_item.type = "reasoning"- reasoning_item.id = "rs_123"+ reasoning_item_raw = {+ "id": "rs_123",+ "summary": [],+ "type": "reasoning",+ }+ reasoning_item = _MockReasoningItem.model_validate(+ reasoning_item_raw,+ )completed_resp = MagicMock()completed_resp.id = "resp-2"@@ -463,6 +619,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):_make_event("response.reasoning_summary_text.delta",delta="Thinking",+ item_id="rs_123",response=MagicMock(id="resp-2"),),_make_event(@@ -514,6 +671,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):created_at=A,thinking="",reasoning_item_id="rs_123",+ reasoning_item_raw=reasoning_item_raw,),],),@@ -525,6 +683,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):created_at=A,thinking="Thinking",reasoning_item_id="rs_123",+ reasoning_item_raw=reasoning_item_raw,),TextBlock.model_construct(id=A,@@ -536,13 +695,97 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):],)+ async def test_stream_preserves_multiple_reasoning_items(self) -> None:+ """Streaming keeps every reasoning item's encrypted payload."""+ first_raw = {+ "id": "rs_first",+ "summary": [],+ "type": "reasoning",+ "encrypted_content": "encrypted_first",+ }+ second_raw = {+ "id": "rs_second",+ "summary": [],+ "type": "reasoning",+ "encrypted_content": "encrypted_second",+ }+ completed_resp = MagicMock()+ completed_resp.id = "resp-multiple-reasoning"+ completed_resp.output = [+ _MockReasoningItem.model_validate(first_raw),+ _MockReasoningItem.model_validate(second_raw),+ ]+ completed_resp.usage = MagicMock()+ completed_resp.usage.input_tokens = 10+ completed_resp.usage.output_tokens = 5+ completed_resp.usage.input_tokens_details = None++ events = [+ _make_event(+ "response.reasoning_summary_text.delta",+ delta="First",+ item_id="rs_first",+ ),+ _make_event(+ "response.reasoning_summary_text.delta",+ delta="Second",+ item_id="rs_second",+ ),+ _make_event("response.completed", response=completed_resp),+ ]+ self.mock_client.responses.create = AsyncMock(+ return_value=_MockAsyncEventStream(events),+ )++ gen = await self.model([])+ responses = [response async for response in gen]++ self.assertEqual(+ (responses[-1].is_last, responses[-1].content),+ (+ True,+ [+ ThinkingBlock.model_construct(+ id=A,+ created_at=A,+ thinking="First",+ reasoning_item_id="rs_first",+ reasoning_item_raw=first_raw,+ ),+ ThinkingBlock.model_construct(+ id=A,+ created_at=A,+ thinking="Second",+ reasoning_item_id="rs_second",+ reasoning_item_raw=second_raw,+ ),+ ],+ ),+ )++ formatted = await self.model.formatter.format(+ [+ AssistantMsg(+ name="assistant",+ content=responses[-1].content,+ ),+ ],+ )+ self.assertListEqual(formatted, [first_raw, second_raw])+async def test_stream_empty_reasoning_summary_keeps_reasoning_item_id(self,) -> None:"""Stream empty reasoning summary still preserves its item id."""- reasoning_item = MagicMock()- reasoning_item.type = "reasoning"- reasoning_item.id = "rs_empty"+ reasoning_item_raw = {+ "id": "rs_empty",+ "summary": [],+ "type": "reasoning",+ "encrypted_content": "encrypted_stream_payload",+ }+ reasoning_item = _MockReasoningItem.model_validate(+ reasoning_item_raw,+ )msg_item = MagicMock()msg_item.type = "message"@@ -592,6 +835,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):created_at=A,thinking="",reasoning_item_id="rs_empty",+ reasoning_item_raw=reasoning_item_raw,),],),@@ -608,6 +852,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):created_at=A,thinking="",reasoning_item_id="rs_empty",+ reasoning_item_raw=reasoning_item_raw,),],),