openclaw - ✅(Solved) Fix [Bug]: Custom OpenAI Adapter HTTP 400: Session Startup Prompt Breaks Tool Call Chain (2026.3.23+) [1 pull requests, 3 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#55544Fetched 2026-04-08 01:38:12
View on GitHub
Comments
3
Participants
1
Timeline
11
Reactions
0
Author
Participants
Timeline (top)
commented ×3closed ×2labeled ×2cross-referenced ×1

Regression: Custom OpenAI-compatible adapters fail with HTTP 400 on session startup due to invalid prompt injection breaking tool call adjacency (works in 2026.3.13).

Error Message

Evidence A: API Error Logs (HTTP 400 - Bad Request)

  1. OpenAI Adapter Error: "error": { "message": "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.", "param": "messages.[2].role" }

  2. DeepSeek Adapter Error: "error": { "message": "Invalid messages at 2: {'role': 'user', 'content': 'A new session was started...'}", "status": 400 }

Analysis: The error consistently flags message index [2]. This corresponds to the OpenClaw-injected "Session Startup" prompt, which is inserted between a prior assistant message (tool_calls) and the subsequent tool response, violating strict API adjacency rules.

Evidence B: Version Regression Confirmation

  • Version 2026.3.13: ✅ Works correctly (No error).
  • Version 2026.3.23+: ❌ Fails consistently on startup.

Root Cause

Regression: Custom OpenAI-compatible adapters fail with HTTP 400 on session startup due to invalid prompt injection breaking tool call adjacency (works in 2026.3.13).

Fix Action

Fix / Workaround

The agent becomes unresponsive and requires a downgrade to version 2026.3.13 to function.

Severity: Major (Significant functionality loss, workaround available). Affected Systems: Specifically custom OpenAI-compatible providers (e.g., DeepSeek, local LLMs) using the openai-completions adapter. Unaffected Systems: Google Gemini and Anthropic Claude adapters are verified working correctly. Frequency: Always (100%) on session startup or reset when using tools. Consequence: Agents relying on custom OpenAI-compatible models fail (HTTP 400) immediately upon session startup or reset due to invalid message ordering. Users must downgrade to 2026.3.13 to restore functionality.

PR fix notes

PR #55670: fix: discord OOM, OpenAI tool adjacency, subagent fallback, auth cache

Description (problem / solution / changelog)

Summary

Fixes four independent high-impact bugs:

  • #55606 — Discord health-monitor triggers excessive stale-socket reconnects on quiet servers, leaking memory until OOM. Removed blanket lastEventAt inflation from debug messages and added a quiet-server guard using lastConnectedAt so connected channels with no real events aren't treated as stale.

  • #55544 — Session reset prompt (/new, /reset) breaks tool_call adjacency for custom OpenAI-compatible providers (HTTP 400). Enabled validateAnthropicTurns for all openai-completions providers so orphaned tool_calls from prior sessions are stripped before sending.

  • #55581 — Subagent loses its original task when model fallback triggers, receiving a generic "Continue where you left off" recovery message instead. Now preserves the original body when isNewSession is true since the transcript is empty and the fallback model needs the actual task.

  • #55562 — Gateway in-memory auth cache overwrites per-agent API keys on disk during usage/cooldown updates. Added runtime cache write-through after locked saves so subsequent reads via ensureAuthProfileStore() return fresh data.

Test plan

  • pnpm vitest run src/gateway/channel-health-policy.test.ts — 16 tests pass (2 new)
  • pnpm vitest run src/agents/transcript-policy.policy.test.ts — 4 tests pass (2 new)
  • pnpm vitest run src/agents/auth-profiles.runtime-snapshot-save.test.ts — 2 tests pass (1 new)
  • pnpm tsc --noEmit — clean

Changed files

  • extensions/discord/src/monitor/provider.lifecycle.reconnect.ts (modified, +4/-1)
  • src/agents/agent-command.ts (modified, +1/-0)
  • src/agents/auth-profiles.runtime-snapshot-save.test.ts (modified, +55/-0)
  • src/agents/auth-profiles/store.ts (modified, +7/-0)
  • src/agents/command/attempt-execution.ts (modified, +7/-1)
  • src/agents/pi-embedded-helpers.validate-turns.test.ts (modified, +46/-0)
  • src/agents/pi-embedded-helpers/turns.ts (modified, +92/-19)
  • src/agents/transcript-policy.policy.test.ts (modified, +18/-0)
  • src/agents/transcript-policy.test.ts (modified, +5/-2)
  • src/agents/transcript-policy.ts (modified, +6/-1)
  • src/gateway/channel-health-policy.test.ts (modified, +42/-0)
  • src/gateway/channel-health-policy.ts (modified, +12/-0)

Code Example

Evidence A: API Error Logs (HTTP 400 - Bad Request)
--------------------------------------------------
1. OpenAI Adapter Error:
"error": {
  "message": "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.",
  "param": "messages.[2].role"
}

2. DeepSeek Adapter Error:
"error": {
  "message": "Invalid messages at 2: {'role': 'user', 'content': 'A new session was started...'}",
  "status": 400
}

Analysis:
The error consistently flags message index [2]. This corresponds to the OpenClaw-injected "Session Startup" prompt, which is inserted between a prior assistant message (tool_calls) and the subsequent tool response, violating strict API adjacency rules.

Evidence B: Version Regression Confirmation
--------------------------------------------------
- Version 2026.3.13:Works correctly (No error).
- Version 2026.3.23+:Fails consistently on startup.
RAW_BUFFERClick to expand / collapse

Bug type

Regression (worked before, now fails)

Beta release blocker

No

Summary

Regression: Custom OpenAI-compatible adapters fail with HTTP 400 on session startup due to invalid prompt injection breaking tool call adjacency (works in 2026.3.13).

Steps to reproduce

Regression Context (Crucial):

  • Verified working in version 2026.3.13.
  • Verified failing consistently in versions 2026.3.23 and above (including latest).

Steps to Reproduce (Custom OpenAI-compatible Provider):

  1. Configure openclaw.json with a custom provider using the "openai-completions" adapter (e.g., DeepSeek).
  2. Start a fresh session (WebChat or openclaw tui).
  3. Send a message that triggers a tool call (e.g., "Check weather").
  4. Observe the immediate API failure (HTTP 400 Bad Request) due to invalid message structure injected during session startup.

Note: No explicit /reset or /new command is required. The session startup logic itself injects the erroneous prompt.

Expected behavior

The agent should successfully initialize a session (or reset one) and process tool calls without breaking the API message chain.

Specifically, the injected "Session Startup" prompt should NOT be inserted between a tool_calls message and its corresponding tool response message. This violates the OpenAI API protocol requirement for strict adjacency.

Reference: Verified behavior in version 2026.3.13, where session startup and tool calls work correctly without triggering HTTP 400 errors.

Actual behavior

The session reset ("A new session was started via /new or /reset...") system prompt is incorrectly injected at message index 2, breaking the tool_call chain.

This causes immediate API failures (HTTP 400 Bad Request) when the reset is followed by tool usage.

Evidence (via OpenAI-compatible endpoint):

  • OpenAI Error: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.
  • DeepSeek Error: Invalid messages at 2: {'role': 'user', 'content': 'A new session was started...'}

The agent becomes unresponsive and requires a downgrade to version 2026.3.13 to function.

OpenClaw version

2026.3.24

Operating system

Ubuntu 24.04

Install method

npm

Model

custom openai-completions models

Provider / routing chain

Custom Provider (OpenAI-compatible) in openclaw.json -> Target API (DeepSeek/OpenAI)

Additional provider/model setup details

Regression Context:

  • Last Known Good Version: 2026.3.13
  • First Known Bad Version: 2026.3.23 (Issue persists in latest)

This regression appears to be introduced by changes to session context management between 2026.3.13 and 2026.3.23, specifically affecting how the initial system prompt is injected for custom OpenAI-compatible providers.

Logs, screenshots, and evidence

Evidence A: API Error Logs (HTTP 400 - Bad Request)
--------------------------------------------------
1. OpenAI Adapter Error:
"error": {
  "message": "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.",
  "param": "messages.[2].role"
}

2. DeepSeek Adapter Error:
"error": {
  "message": "Invalid messages at 2: {'role': 'user', 'content': 'A new session was started...'}",
  "status": 400
}

Analysis:
The error consistently flags message index [2]. This corresponds to the OpenClaw-injected "Session Startup" prompt, which is inserted between a prior assistant message (tool_calls) and the subsequent tool response, violating strict API adjacency rules.

Evidence B: Version Regression Confirmation
--------------------------------------------------
- Version 2026.3.13: ✅ Works correctly (No error).
- Version 2026.3.23+: ❌ Fails consistently on startup.

Impact and severity

Severity: Major (Significant functionality loss, workaround available). Affected Systems: Specifically custom OpenAI-compatible providers (e.g., DeepSeek, local LLMs) using the openai-completions adapter. Unaffected Systems: Google Gemini and Anthropic Claude adapters are verified working correctly. Frequency: Always (100%) on session startup or reset when using tools. Consequence: Agents relying on custom OpenAI-compatible models fail (HTTP 400) immediately upon session startup or reset due to invalid message ordering. Users must downgrade to 2026.3.13 to restore functionality.

Additional information

Regression Confirmation:

  • Last Known Good Version: 2026.3.13 (Verified working correctly)
  • First Known Bad Version: 2026.3.24 (Consistently fails with HTTP 400 on session reset)

Context: The issue specifically affects the openai-completions adapter. Using skipBootstrap: true or contextPruning: off does NOT resolve the problem, confirming the reset prompt injection logic is hard-coded in the core session management.

extent analysis

Fix Plan

To resolve the regression issue with custom OpenAI-compatible adapters, we need to modify the session startup logic to prevent the injection of the "Session Startup" prompt between a tool_calls message and its corresponding tool response message.

Here are the steps to fix the issue:

  • Modify the sessionStartup function to check if the previous message is a tool_calls message.
  • If it is, append the "Session Startup" prompt after the corresponding tool response message.
  • Update the openai-completions adapter to handle the modified session startup logic.

Example code changes:

// Modified sessionStartup function
function sessionStartup(session) {
  const previousMessage = session.getMessageHistory().slice(-1)[0];
  if (previousMessage && previousMessage.role === 'tool_calls') {
    // Append the "Session Startup" prompt after the corresponding tool response message
    session.appendMessage({ role: 'user', content: 'A new session was started...' });
  } else {
    // Inject the "Session Startup" prompt at the beginning of the session
    session.prependMessage({ role: 'user', content: 'A new session was started...' });
  }
}

// Updated openai-completions adapter
function openaiCompletionsAdapter(session) {
  // Handle the modified session startup logic
  if (session.getMessageHistory().length > 0 && session.getMessageHistory()[0].role === 'user') {
    // Remove the injected "Session Startup" prompt
    session.getMessageHistory().shift();
  }
  // Proceed with the original adapter logic
}

Verification

To verify that the fix worked, follow these steps:

  • Configure the openclaw.json file with a custom provider using the openai-completions adapter.
  • Start a fresh session (WebChat or openclaw tui).
  • Send a message that triggers a tool call (e.g., "Check weather").
  • Observe that the agent successfully initializes the session and processes the tool call without breaking the API message chain.

Extra Tips

  • Ensure that the openai-completions adapter is updated to handle the modified session startup logic.
  • Test the fix with different custom OpenAI-compatible providers to ensure that the issue is resolved across all affected systems.
  • Consider adding additional logging or debugging statements to help identify and resolve any future issues related to session startup and tool calls.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

The agent should successfully initialize a session (or reset one) and process tool calls without breaking the API message chain.

Specifically, the injected "Session Startup" prompt should NOT be inserted between a tool_calls message and its corresponding tool response message. This violates the OpenAI API protocol requirement for strict adjacency.

Reference: Verified behavior in version 2026.3.13, where session startup and tool calls work correctly without triggering HTTP 400 errors.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING