Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Fix Perplexity API Role Compatibility (Issue #448) - #454

Merged
caarlos0 merged 3 commits into
charmbracelet:mainfrom
minkiha:fix/openai-system-message-remap
Mar 10, 2025
Merged

caarlos0 merged 3 commits into
charmbracelet:mainfrom
minkiha:fix/openai-system-message-remap

Conversation

@minkiha

@minkiha minkiha commented Mar 4, 2025

Copy link
Copy Markdown
Contributor

Fix Perplexity API Role Compatibility (Issue #448)

This fix maintains compatibility with legacy o1 models while solving the Perplexity error.

The Problem

A previous update (commit e5e4bdd) added support for o1-preview and o1-mini models by converting system messages to user messages. However, this breaks Perplexity models because their API doesn't allow multiple consecutive messages with the same role.

How to See the Issue

Try this command and you'll see an error:

mods --model llama-3.1-sonar-small-128k-online --role shell list all files

Solution

  • Only remap system messages for the two models that need it (o1-preview and o1-mini)
  • Keep the original message roles for all other models that go through the function createOpenAIStream (stream.go)

Note

The system message remapping is not needed for these models, which handle system messages correctly without modification:

  • o1
  • o3-mini
  • gpt-4.5-preview

@minkiha
minkiha requested a review from caarlos0 as a code owner March 4, 2025 23:33
Comment thread stream.go Outdated
Comment on lines +25 to +36
messages := m.messages
if mod.Name == "o1-preview" || mod.Name == "o1-mini" {
messages = []openai.ChatCompletionMessage{}
for _, message := range m.messages {
if message.Role == openai.ChatMessageRoleSystem {
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: message.Content,
})
} else {
messages = append(messages, message)
}

@caarlos0 caarlos0 Mar 5, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could probably simplify this to something like:

if mod.Name == "o1-preview" || mod.Name == "o1-mini" {
	for i := range m.messages {
		if message.Role != openai.ChatMessageRoleSystem {
			continue
		}
		m.messages[i].Role = openai.ChatMessageRoleUser
	}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah you're right, simplified it.

@caarlos0 caarlos0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR!

made one one small suggestion

@caarlos0

Copy link
Copy Markdown
Contributor

thanks!

@caarlos0 caarlos0 added the bug Something isn't working label Mar 10, 2025
@caarlos0
caarlos0 merged commit f0daa27 into charmbracelet:main Mar 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants