Skip to content

AI-assisted: Fix OpenAI Responses 400 after model switch (orphaned reasoning item)#1562

Merged
steipete merged 1 commit into
openclaw:mainfrom
roshanasingh4:fix/1540-openai-reasoning-transcript
Jan 24, 2026
Merged

AI-assisted: Fix OpenAI Responses 400 after model switch (orphaned reasoning item)#1562
steipete merged 1 commit into
openclaw:mainfrom
roshanasingh4:fix/1540-openai-reasoning-transcript

Conversation

@roshanasingh4

Copy link
Copy Markdown
Contributor

Summary

Fixes #1540: switching mid-session from Anthropic → OpenAI Responses (e.g. openai-codex-responses) can corrupt the replayed transcript and trigger:

400 Item 'rs_XXXX' of type 'reasoning' was provided without its required following item.

This PR sanitizes session history for OpenAI Responses APIs by downgrading orphaned OpenAI reasoning signatures stored in thinkingSignature (e.g. {"id":"rs_...","type":"reasoning"}) into plain text (or dropping empty blocks), preventing invalid history re-submission.

Changes

  • Add downgradeOpenAIReasoningBlocks() and wire into transcript sanitization for openai-responses + openai-codex-responses.
  • Add regression test covering the downgrade behavior.

Testing

  • pnpm vitest run src/agents/pi-embedded-helpers.downgradeopenai-reasoning.test.ts

AI transparency

  • AI-assisted PR (Codex/Clawdbot). I reviewed the change and understand what it does.

Closes #1540

@roshanasingh4 roshanasingh4 force-pushed the fix/1540-openai-reasoning-transcript branch from 301089e to 1a9f186 Compare January 24, 2026 04:51
@steipete

Copy link
Copy Markdown
Contributor

This is a pi issue.

@steipete steipete added the pi-issue Pi-related issue label Jan 24, 2026
@steipete

Copy link
Copy Markdown
Contributor

Can you attach session logs where this happened?

@drkraft

drkraft commented Jan 24, 2026

Copy link
Copy Markdown

Session logs from issue #1540

Here are anonymized session logs demonstrating the bug (from the original reporter):

{
  "description": "Session logs demonstrating issue #1540 - OpenAI reasoning block causes 400 error",
  "context": {
    "session_type": "Discord channel",
    "initial_model": "anthropic/claude-sonnet-4-5",
    "switched_to": "openai/gpt-5.2-codex",
    "clawdbot_version": "2026.1.16-2"
  },
  "sequence": [
    {
      "step": 1,
      "description": "User switches model mid-session from Anthropic to OpenAI",
      "command": "/model gpt-5.2-codex"
    },
    {
      "step": 2,
      "description": "First GPT-5.2-codex response generates thinkingSignature with OpenAI reasoning format",
      "log": {
        "type": "message",
        "timestamp": "2026-01-23T20:32:52.772Z",
        "message": {
          "role": "assistant",
          "content": [
            {
              "type": "thinking",
              "thinking": "**Reading playbook contents**\n\nI am preparing to read the playbook file to retrieve and display its contents as requested.",
              "thinkingSignature": {
                "id": "rs_07274b660fef5363006973daf27e348190bc09ed53bcc7fa0b",
                "type": "reasoning",
                "encrypted_content": "[REDACTED - base64 encrypted reasoning content]",
                "summary": [
                  {
                    "type": "summary_text",
                    "text": "**Reading playbook contents**..."
                  }
                ]
              }
            }
          ],
          "api": "openai-responses",
          "provider": "openai",
          "model": "gpt-5.2-codex",
          "stopReason": "toolUse"
        }
      }
    },
    {
      "step": 3,
      "description": "All subsequent requests fail with 400 error - orphaned reasoning item",
      "logs": [
        {
          "timestamp": "2026-01-23T20:35:36.968Z",
          "message": {
            "role": "assistant",
            "api": "openai-responses",
            "provider": "openai",
            "model": "gpt-5.2-codex",
            "stopReason": "error",
            "errorMessage": "400 Item 'rs_07274b660fef5363006973daf27e348190bc09ed53bcc7fa0b' of type 'reasoning' was provided without its required following item."
          }
        },
        {
          "timestamp": "2026-01-23T20:35:58.979Z",
          "message": {
            "role": "assistant",
            "api": "openai-responses",
            "provider": "openai",
            "model": "gpt-5.2-codex",
            "stopReason": "error",
            "errorMessage": "400 Item 'rs_07274b660fef5363006973daf27e348190bc09ed53bcc7fa0b' of type 'reasoning' was provided without its required following item."
          }
        },
        {
          "timestamp": "2026-01-23T20:39:49.894Z",
          "message": {
            "role": "assistant",
            "api": "openai-responses",
            "provider": "openai",
            "model": "gpt-5.2-codex",
            "stopReason": "error",
            "errorMessage": "400 Item 'rs_07274b660fef5363006973daf27e348190bc09ed53bcc7fa0b' of type 'reasoning' was provided without its required following item."
          }
        }
      ]
    }
  ],
  "root_cause": "The thinkingSignature contains an OpenAI reasoning block ID (rs_...) that gets replayed to the API. OpenAI Responses API requires reasoning blocks to be followed by their corresponding content item. When the history is re-submitted, the orphaned reasoning ID causes a 400 error.",
  "workaround_applied": "Manually edited transcript to remove thinkingSignature fields and error messages"
}

The bug also reproduced a second time in the same session with a different reasoning ID (rs_07274b660fef5363006973e1eaa3d08190968c2ac1368e485d), confirming it happens on every GPT-5.2-codex response that generates reasoning blocks.

@steipete steipete merged commit 202d7af into openclaw:main Jan 24, 2026
20 of 22 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Landed via temp rebase onto main.\n\n- Gate: pnpm lint && pnpm build && pnpm test\n- Land commit: 202d7af\n- Merge commit: 202d7af\n\nThanks @roshanasingh4!

dominicnunez pushed a commit to dominicnunez/openclaw that referenced this pull request Feb 26, 2026
dustin-olenslager pushed a commit to dustin-olenslager/ironclaw-supreme that referenced this pull request Mar 24, 2026
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pi-issue Pi-related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI reasoning block causes 400 error after model switch from Anthropic

3 participants