Add PerplexityLLMAdapter for message ordering strictness#4009
Merged
Conversation
kompfner
added a commit
that referenced
this pull request
Mar 12, 2026
kompfner
added a commit
that referenced
this pull request
Mar 12, 2026
102fa69 to
cced0b7
Compare
…straints Perplexity's API is stricter than OpenAI about conversation history: - Requires strict alternation between user/tool and assistant messages - Disallows system messages except as the initial message - Requires the last message to be user or tool The new adapter transforms messages before sending to satisfy all three constraints: merging consecutive initial system messages, converting non-initial system to user, merging consecutive same-role messages, and removing trailing assistant messages. Also adds dual-system-instruction warnings to Cerebras, Fireworks, Mistral, Perplexity, and SambaNova services (matching the existing BaseOpenAILLMService pattern), and updates the warning text in BaseOpenAILLMService to be more descriptive.
cced0b7 to
e4bf628
Compare
Perplexity allows multiple initial system messages, so don't merge them. Instead, skip system-system pairs during the consecutive same-role merge step. Broaden the trailing message fix to convert any trailing system message to user (not just a lone system message), so contexts with only system messages don't fail.
Add test exercising the step 3 ordering where stripping a trailing assistant exposes a system message that then gets converted to user. Move the reasoning about when a trailing system message can occur into the docstring.
Perplexity appears to have statefulness within a conversation, so converting a system message to "user" in one call and then back to "system" in the next (after more messages are appended) causes API errors. Remove the trailing system→user conversion entirely — if the context only has system messages, the API call will fail but the mistake will be caught right away.
8a8bb38 to
99f2812
Compare
aconchillo
approved these changes
Mar 12, 2026
Contributor
|
LGTM! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PerplexityLLMAdapterthat transforms conversation messages to satisfy Perplexity's stricter API constraints before sending requests. Perplexity requires strict role alternation (user/tool ↔ assistant), no non-initial system messages, and the last message to be user/tool — conversation histories that work fine with OpenAI can cause errors with Perplexity (PerplexityLLMServicesubclassesOpenAILLMService).PerplexityLLMServicenow uses this adapter viaadapter_class = PerplexityLLMAdapter.BaseOpenAILLMServicebehavior.Testing
11 test cases cover: standard passthrough, initial system preservation, multiple initial system preservation, consecutive same-role merging, non-initial system conversion, trailing assistant removal, system-only context preserved, system exposed after trailing assistant removal, consecutive assistant merge+removal, tool message preservation, and empty messages.
🤖 Generated with Claude Code