Skip to content

fix(session): filter empty text content before sending to API#1350

Open
walker83 wants to merge 2 commits into
XiaomiMiMo:mainfrom
walker83:fix/empty-message-content
Open

fix(session): filter empty text content before sending to API#1350
walker83 wants to merge 2 commits into
XiaomiMiMo:mainfrom
walker83:fix/empty-message-content

Conversation

@walker83

Copy link
Copy Markdown

Summary

Filter out empty text content when converting messages to API format. This prevents the "all messages must have non-empty content" error (HTTP 400) that can occur after pruning clears reasoning content.

Problem

As reported in #1326, long-running sessions can hit a 400 error:

Bad Request: {"error":{"message":"messages.271: all messages must have non-empty content","type":"invalid_request_error"}}

The root cause is in toModelMessagesEffect:

  • prune.ts:225 clears reasoning content by setting text: ""
  • message-v2.ts:683,745 converts these empty-text parts to API messages without filtering
  • The API rejects messages with empty content

Fix

Add part.text.length > 0 checks when building user and assistant message parts:

// Before (user messages)
if (part.type === "text" && !part.ignored)

// After
if (part.type === "text" && !part.ignored && part.text.length > 0)

// Before (assistant messages)  
if (part.type === "text")

// After
if (part.type === "text" && part.text.length > 0)

Changes

  • packages/opencode/src/session/message-v2.ts: Add empty text content filter

Testing

  1. Start a session and have a long conversation (>100 messages)
  2. Trigger pruning (context overflow)
  3. Continue the conversation
  4. Verify no 400 errors occur

Related Issues

Checklist

  • Code follows project style guidelines
  • Single file change
  • Backward compatible

walker83 added 2 commits June 25, 2026 21:21
…ng-running sessions

Add MAX_MESSAGES_PER_SESSION constant (1000) and warning log when sessions
exceed this limit. This helps users identify when to start a new session or
run /compact to avoid the memory issues reported in XiaomiMiMo#1221.

Related: XiaomiMiMo#1221, XiaomiMiMo#813, XiaomiMiMo#680
Refs: XiaomiMiMo#1221
Add checks to skip text parts with empty strings when converting
messages to API format. This prevents the 'all messages must have
non-empty content' error (400) that occurs after pruning clears
reasoning content.

Fixes XiaomiMiMo#1326
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

模型会在一些轮次后陷入400错误并彻底卡死

1 participant