fix: Perplexity Sonar models return 400 error with consecutive user messages #6804

Open
opened 2026-02-16 18:05:19 -05:00 by yindo · 1 comment
Owner

Originally created by @pfitz on GitHub (Jan 19, 2026).

Originally assigned to: @rekram1-node on GitHub.

Problem

Perplexity's Sonar models (sonar-pro, sonar-reasoning-pro) reject requests with 400 Bad Request when consecutive user messages are sent, violating their API's message alternation requirement (user/assistant/user/assistant).

Root Cause

When tools return attachments, OpenCode creates a user message for them. If followed by another user message, this breaks the alternation requirement.

Solution

Merge consecutive user messages for Perplexity providers only. Content is combined with \n\n separator, preserving all information without affecting other providers.

Testing

  • New test: perplexity-message-alternation.test.ts
  • All 74 provider transform tests pass
  • All 6 LLM tests pass
  • Verified: Mistral and other providers unaffected
Originally created by @pfitz on GitHub (Jan 19, 2026). Originally assigned to: @rekram1-node on GitHub. ## Problem Perplexity's Sonar models (sonar-pro, sonar-reasoning-pro) reject requests with 400 Bad Request when consecutive user messages are sent, violating their API's message alternation requirement (user/assistant/user/assistant). ## Root Cause When tools return attachments, OpenCode creates a user message for them. If followed by another user message, this breaks the alternation requirement. ## Solution Merge consecutive user messages for Perplexity providers only. Content is combined with `\n\n` separator, preserving all information without affecting other providers. ## Testing - New test: perplexity-message-alternation.test.ts - All 74 provider transform tests pass - All 6 LLM tests pass - Verified: Mistral and other providers unaffected
Author
Owner

@github-actions[bot] commented on GitHub (Jan 19, 2026):

This issue might be a duplicate of existing issues or related to systemic problems. Please check:

  • #9364: Claude Extended Thinking - similar message ordering/alternation requirement issues
  • #9108: Venice provider fails with message validation (400 Bad Request)
  • #9233: Vertex AI Gemini - provider-specific message/schema validation errors
  • #8903: Whitespace content causes API validation errors in provider transformations
  • #8679: Empty user messages breaking Claude/OpenAI-compatible proxies
  • #8700: Synthetic user messages created for tool returns affecting message alternation

These issues suggest a systemic pattern with how OpenCode handles provider-specific message format requirements and tool attachment handling.

Feel free to ignore if this specific Perplexity case requires isolated attention.

@github-actions[bot] commented on GitHub (Jan 19, 2026): This issue might be a duplicate of existing issues or related to systemic problems. Please check: - #9364: Claude Extended Thinking - similar message ordering/alternation requirement issues - #9108: Venice provider fails with message validation (400 Bad Request) - #9233: Vertex AI Gemini - provider-specific message/schema validation errors - #8903: Whitespace content causes API validation errors in provider transformations - #8679: Empty user messages breaking Claude/OpenAI-compatible proxies - #8700: Synthetic user messages created for tool returns affecting message alternation These issues suggest a systemic pattern with how OpenCode handles provider-specific message format requirements and tool attachment handling. Feel free to ignore if this specific Perplexity case requires isolated attention.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#6804