AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): replay encrypted reasoning items so OpenAI tool loops survive (#13438)
Krzysztof Czerwinski94fc759cb2ba
diff --git a/autogpt_platform/backend/backend/blocks/orchestrator.py b/autogpt_platform/backend/backend/blocks/orchestrator.pyindex 51c21fb478..d64a85fca3 100644--- a/autogpt_platform/backend/backend/blocks/orchestrator.py+++ b/autogpt_platform/backend/backend/blocks/orchestrator.py@@ -255,10 +255,24 @@ def _convert_raw_response_to_dict(# OpenAI Responses API: extract individual output items.# Strip 'status' — it's a response-only field that OpenAI rejects# when the item is sent back as input on the next API call.- items = [+ converted = [{k: v for k, v in json.to_dict(item).items() if k != "status"}for item in raw_response.output]+ # A reasoning item (gpt-5*, o3*) is only replayable on the next turn+ # when it carries encrypted_content (we request it via+ # include=["reasoning.encrypted_content"] with store=false). If the+ # blob is ever absent — API edge case or behaviour change — re-sending+ # the rs_... id triggers a server-side lookup that 404s and kills the+ # loop. Drop such items: losing prior reasoning context degrades+ # gracefully, a 404 does not. See OPEN-3187.+ items = [+ item+ for item in converted+ if not (+ item.get("type") == "reasoning" and not item.get("encrypted_content")+ )+ ]return items if items else [{"role": "assistant", "content": ""}]else:# Chat Completions / Anthropic return message objectsdiff --git a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_responses_api.py b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_responses_api.pyindex ac78b6d35b..3764c63568 100644--- a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_responses_api.py+++ b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_responses_api.py@@ -82,6 +82,27 @@ class _MockFunctionCall:self.status = "completed"+class _MockReasoning:+ """openai.types.responses.ResponseReasoningItem++ Reasoning models (gpt-5*, o3*) emit one of these per turn. With+ ``store=False`` it is only replayable on the next turn if it carries+ ``encrypted_content`` (requested via ``include=['reasoning.encrypted_content']``);+ otherwise OpenAI tries to look it up by ``rs_...`` id and 404s.+ """++ def __init__(+ self,+ encrypted_content: str | None = "gAAAAABencrypted==",+ rs_id: str = "rs_abc123",+ ):+ self.type = "reasoning"+ self.id = rs_id+ self.summary: list = []+ self.encrypted_content = encrypted_content+ self.status = "completed"++class _MockUsage:def __init__(self, inp: int = 100, out: int = 50):self.input_tokens = inp@@ -235,6 +256,65 @@ class TestConvertRawResponseToDict:"status" not in item), f"'status' must be stripped from Responses API items: {item}"+ def test_responses_api_preserves_reasoning_encrypted_content(self):+ """Reasoning items must be re-inlined verbatim WITH ``encrypted_content``.++ This is the client-side half of the OPEN-3187 fix: with ``store=False``+ the reasoning item produced on turn N is only replayable on turn N+1 if+ its encrypted blob travels with it. Dropping it (or keeping only the+ ``rs_...`` id) makes OpenAI 404 on the next call.+ """+ resp = _MockResponse(+ output=[+ _MockReasoning(encrypted_content="gAAAAABblob=="),+ _MockFunctionCall("my_tool", "{}", call_id="call_xyz"),+ ]+ )+ result = _convert_raw_response_to_dict(resp)+ assert isinstance(result, list)++ reasoning_items = [i for i in result if i.get("type") == "reasoning"]+ assert len(reasoning_items) == 1+ rs = reasoning_items[0]+ assert rs["encrypted_content"] == "gAAAAABblob=="+ # 'status' is a response-only field OpenAI rejects on input items.+ assert "status" not in rs+ # Order must be preserved: reasoning precedes the call it produced.+ assert result[0].get("type") == "reasoning"+ assert result[1].get("type") == "function_call"++ def test_responses_api_drops_reasoning_without_encrypted_content(self):+ """A reasoning item missing ``encrypted_content`` must be dropped, not+ replayed by id.++ With ``store=False`` an un-encrypted reasoning item can only be+ re-sent as an ``rs_...`` id reference, which 404s server-side. If the+ API ever omits the blob, dropping the item degrades gracefully (lost+ reasoning context) rather than killing the loop with a hard error.+ See OPEN-3187.+ """+ resp = _MockResponse(+ output=[+ _MockReasoning(encrypted_content=None),+ _MockFunctionCall("my_tool", "{}", call_id="call_xyz"),+ ]+ )+ result = _convert_raw_response_to_dict(resp)+ assert isinstance(result, list)+ assert all(i.get("type") != "reasoning" for i in result)+ # The function_call it preceded is still replayed.+ assert [i for i in result if i.get("type") == "function_call"]++ def test_responses_api_drops_only_reasoning_when_it_is_the_sole_item(self):+ """If the only output item is an un-replayable reasoning item, the+ result must still be a valid non-empty input list (falls back to an+ empty assistant message), never an empty list the API rejects."""+ resp = _MockResponse(output=[_MockReasoning(encrypted_content=None)])+ result = _convert_raw_response_to_dict(resp)+ assert isinstance(result, list)+ assert len(result) == 1+ assert result[0].get("role") == "assistant"+# ───────────────────────────────────────────────────────────────────────────# _get_tool_requests (lines 61-86)@@ -898,11 +978,12 @@ async def test_agent_mode_conversation_valid_for_responses_api():resp1.reasoning = Noneresp1.raw_response = _MockResponse(output=[+ _MockReasoning(encrypted_content="gAAAAABturn1=="),_MockFunctionCall("story_improver",'{"prompt_values___story": "draft", "prompt_values___improvement": "polish"}',call_id="call_abc",- )+ ),])@@ -1010,12 +1091,20 @@ async def test_agent_mode_conversation_valid_for_responses_api():has_type = item.get("type") in ("function_call","function_call_output",+ "reasoning","message",)assert (has_role or has_type), f"input[{i}] has neither valid role nor type: {item!r}"+ # The turn-1 reasoning item must be replayed on turn 2 WITH its+ # encrypted blob — that's what keeps store=false reasoning models from+ # 404ing on the next call (OPEN-3187).+ reasoning_items = [i for i in second_prompt if i.get("type") == "reasoning"]+ assert len(reasoning_items) == 1+ assert reasoning_items[0].get("encrypted_content") == "gAAAAABturn1=="+@pytest.mark.asyncioasync def test_traditional_mode_conversation_valid_for_responses_api():diff --git a/autogpt_platform/backend/backend/util/llm/providers.py b/autogpt_platform/backend/backend/util/llm/providers.pyindex 69acbf99f7..cb0060ca2a 100644--- a/autogpt_platform/backend/backend/util/llm/providers.py+++ b/autogpt_platform/backend/backend/util/llm/providers.py@@ -442,7 +442,19 @@ async def _call_openai_responses(temperature=temperature if temperature is not None else openai.omit,parallel_tool_calls=parallel_tool_calls,text=text_config, # type: ignore[arg-type]+ # ``store=False`` keeps conversation state out of OpenAI's servers —+ # we replay the full input on every turn instead. The cost is that a+ # reasoning item (gpt-5*, o3*) emitted on turn N can't be referenced+ # by its ``rs_...`` id on turn N+1 (nothing was persisted to look up;+ # OpenAI 404s with "Item with id 'rs_...' not found"). Asking for+ # ``reasoning.encrypted_content`` makes OpenAI return the reasoning+ # item with an opaque encrypted blob the orchestrator re-inlines+ # verbatim next turn, so the model gets its prior reasoning back+ # without server-side state. Harmless for non-reasoning models+ # (gpt-4.1/4o) — they emit no reasoning items, so nothing comes back.+ # See OPEN-3187.store=False,+ include=["reasoning.encrypted_content"],timeout=timeout_seconds,# ``omit`` is only valid for TYPED params — the SDK strips it# there. ``extra_body`` flows raw into ``options.extra_json``diff --git a/autogpt_platform/backend/backend/util/llm/providers_test.py b/autogpt_platform/backend/backend/util/llm/providers_test.pyindex 3161518d1f..ebc6e149b1 100644--- a/autogpt_platform/backend/backend/util/llm/providers_test.py+++ b/autogpt_platform/backend/backend/util/llm/providers_test.py@@ -355,6 +355,60 @@ class TestOpenAIResponses:assert kwargs["max_output_tokens"] == 100assert kwargs["store"] is False+ @pytest.mark.asyncio+ async def test_requests_encrypted_reasoning_content_with_store_false(self):+ """Reasoning models (gpt-5*, o3*) emit ``reasoning`` items that the+ orchestrator re-inlines verbatim on the next turn. With ``store=False``+ those items are not persisted on OpenAI's servers, so re-sending them+ by id 404s ("Item with id 'rs_...' not found"). The fix keeps+ ``store=False`` but asks OpenAI to return the reasoning items'+ ``encrypted_content`` so they can be replayed inline on the next call.++ See OPEN-3187.+ """+ fake_response = SimpleNamespace() # extractors are also mocked++ async_create = AsyncMock(return_value=fake_response)+ fake_client = SimpleNamespace(responses=SimpleNamespace(create=async_create))++ with (+ patch(+ "backend.util.llm.providers.openai.AsyncOpenAI",+ return_value=fake_client,+ ),+ patch(+ "backend.util.llm.providers.extract_responses_tool_calls",+ return_value=None,+ ),+ patch(+ "backend.util.llm.providers.extract_responses_content",+ return_value="hello world",+ ),+ patch(+ "backend.util.llm.providers.extract_responses_reasoning",+ return_value=None,+ ),+ patch(+ "backend.util.llm.providers.extract_responses_usage",+ return_value=(11, 22),+ ),+ ):+ await call_provider(+ provider="openai",+ model="gpt-5-2025-08-07",+ api_key="sk-test",+ messages=[_msg("user", "hi")],+ max_tokens=100,+ )++ kwargs = async_create.call_args.kwargs+ # store must stay False — state is replayed client-side, never+ # persisted on OpenAI's servers.+ assert kwargs["store"] is False+ # The encrypted blob is what lets a store=false reasoning item be+ # replayed inline on the next turn instead of looked up by id.+ assert "reasoning.encrypted_content" in kwargs["include"]+# ---------------------------------------------------------------------------# Anthropic Messages API