Skip to content

fix(opencode): resolve Perplexity Sonar 400 error - enforce message alternation - #9418

Closed
pfitz wants to merge 2 commits into
anomalyco:devfrom
pfitz:dev
Closed

pfitz wants to merge 2 commits into
anomalyco:devfrom
pfitz:dev

Conversation

@pfitz

@pfitz pfitz commented Jan 19, 2026

Copy link
Copy Markdown

Problem

Perplexity Sonar models reject requests with 400 error when consecutive user messages are sent (violates alternation requirement).

Solution

Merge consecutive user messages for Perplexity providers only. Preserves all content with \n\n separator.

Testing

  • Added test: perplexity-message-alternation.test.ts
  • All 74 provider transform tests pass
  • All 6 LLM tests pass
  • Verified: Mistral and all other providers unaffected
  • Tested scenarios: tools with attachments, multi-step interactions

Fixes #9416

…ation

- Add ensureMessageAlternation() to merge consecutive user messages
- Add mergeMessageContent() for safe content combination
- Apply fix only to Perplexity providers (perplexity providerID or @ai-sdk/perplexity npm)
- Preserve full backward compatibility with all other providers
- Add comprehensive test case to prevent regression
- Addresses: 'After the (optional) system message(s), user and assistant roles should be alternating'

Fixes issue where tools returning attachments created consecutive user messages, causing Perplexity API to reject requests with 400 Bad Request error.
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search results, I found one potentially related PR:

Related PR Found:

This PR might be related as it also addresses the issue of consecutive user messages, which is the core problem your fix addresses for Perplexity Sonar. However, it appears to be an older PR that may have addressed a similar issue differently.

The other results (Perplexity websearch integration and TUI layout system) are unrelated to your message alternation fix.

@github-actions

Copy link
Copy Markdown
Contributor

Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR.

@github-actions github-actions Bot closed this Mar 22, 2026
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.

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

1 participant