Skip to content

Conversation

@pfitz
Copy link

@pfitz pfitz commented Jan 19, 2026

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
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.

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