Skip to content

Fix: avoid empty system prompts (Perplexity Sonar 400)#137

Open
rasulkireev wants to merge 1 commit intomainfrom
fix/sonar-empty-system-prompt
Open

Fix: avoid empty system prompts (Perplexity Sonar 400)#137
rasulkireev wants to merge 1 commit intomainfrom
fix/sonar-empty-system-prompt

Conversation

@rasulkireev
Copy link
Owner

@rasulkireev rasulkireev commented Feb 10, 2026

This fixes a Perplexity Sonar 400 error: 'system message content cannot be empty or contain only whitespace'.

Root cause: several @agent.system_prompt helper functions returned an empty string (e.g. when no project pages / no keywords / no user_prompt). Some providers reject empty system messages.

Change: return an explicit non-empty no-op instruction instead of an empty string in:

  • core/agents/system_prompts.py:add_project_pages (no project pages)
  • core/agents/system_prompts.py:add_target_keywords (no focus keywords)
  • core/agents/title_suggestions_agent.py:add_user_prompt (no user prompt)

This prevents request construction from including invalid empty system messages.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Enhanced system prompt consistency to ensure AI agents receive non-empty guidance messages when optional user inputs are not provided.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 10, 2026

📝 Walkthrough

Walkthrough

System prompt handling across multiple agent modules changed to return non-empty placeholder messages instead of empty strings when default conditions are met, accommodating provider requirements that reject empty system messages.

Changes

Cohort / File(s) Summary
System Prompt Helpers
core/agents/system_prompts.py
Modified add_project_pages and add_target_keywords to return explicit no-op messages instead of empty strings; added explanatory comments for provider compatibility.
Title Suggestions Agent
core/agents/title_suggestions_agent.py
Modified add_user_prompt system prompt assembly to return "No additional user prompt was provided." instead of empty string when user prompt is absent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Fix blog post content mismatch #94: Both PRs modify the same system-prompt helper functions (add_project_pages and add_target_keywords) and alter prompt content/return values.

Poem

🐰 Whiskers twitching with delight,
Empty strings? No, that's not right!
Now prompts always have a say,
No silent whispers fade away!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: replacing empty system prompts with non-empty no-op messages to fix a Perplexity Sonar 400 error.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/sonar-empty-system-prompt

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
core/agents/title_suggestions_agent.py (1)

66-111: ⚠️ Potential issue | 🟠 Major

add_feedback_history can still return an empty string — same root cause this PR fixes.

When none of the feedback lists (neutral_suggestions, liked_suggestions, disliked_suggestions) are populated, feedback_sections stays [] and "\n".join([]) returns "". This will trigger the same Perplexity Sonar 400 error on empty system messages that this PR addresses elsewhere.

Proposed fix
+        if not feedback_sections:
+            return "No previous title feedback was provided."
+
         return "\n".join(feedback_sections)

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 10, 2026

Greptile Overview

Greptile Summary

This PR addresses Perplexity Sonar’s strict validation that system message content cannot be empty/whitespace by ensuring several @agent.system_prompt helpers return non-empty strings even when optional context is missing.

Changes are localized to prompt-construction utilities:

  • core/agents/system_prompts.py now returns explicit no-op instructions when project_pages or project_keywords are absent, rather than "".
  • core/agents/title_s verification_agent.py now returns a non-empty placeholder system message when ctx.deps.user_prompt is missing.

Net effect: request construction should no longer include invalid empty system prompts, preventing 400s from providers that reject them (e.g. Perplexity Sonar).

Confidence Score: 4/5

  • This PR is likely safe to merge and should resolve the reported provider 400s, with one prompt-formatting issue worth fixing for clarity.
  • The changes are small and directly address empty-string system prompts, which is consistent with the reported Perplexity Sonar validation error. The only clear issue found is an unrelated but user-impacting prompt formatting bug (add_webpage_content concatenates fields without separators), which can degrade model behavior but won’t break runtime.
  • core/agents/system_prompts.py (add_webpage_content formatting)

Important Files Changed

Filename Overview
core/agents/system_prompts.py Replaced empty-string system prompt returns with explicit non-empty no-op instructions for missing project pages and missing focus keywords (avoids Perplexity Sonar 400 on empty system messages).
core/agents/title_suggestions_agent.py Ensures title suggestion agent’s optional user prompt system message is never empty by returning a non-empty placeholder when ctx.deps.user_prompt is missing.

Sequence Diagram

sequenceDiagram
  participant Caller as App code
  participant Agent as pydantic_ai.Agent
  participant SP as @agent.system_prompt fns
  participant Provider as LLM provider (Perplexity Sonar)

  Caller->>Agent: create_title_suggestions_agent(...)
  Agent->>SP: evaluate system prompts (incl. add_todays_date, add_user_prompt)
  alt ctx.deps.user_prompt missing
    SP-->>Agent: "No additional user prompt was provided."
  else ctx.deps.user_prompt present
    SP-->>Agent: "IMPORTANT USER REQUEST: ..."
  end
  Agent->>Provider: send request with system messages
  Provider-->>Agent: 200 OK (system content non-empty)

  Caller->>SP: add_project_pages(ctx)
  alt ctx.deps.project_pages empty
    SP-->>Caller: "No internal project pages were provided..."
  else pages present
    SP-->>Caller: "REQUIRED/OPTIONAL PAGES TO LINK..."
  end

  Caller->>SP: add_target_keywords(ctx)
  alt ctx.deps.project_keywords empty
    SP-->>Caller: "No specific SEO focus keywords were provided."
  else keywords present
    SP-->>Caller: "Focus Keywords for SEO..."
  end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 10, 2026

Additional Comments (1)

core/agents/system_prompts.py
Missing separators in string
add_webpage_content() concatenates strings without newlines/spaces ("Web page content:" + f"Title: ..." + f"Description: ..."), which produces a single run-on line like Web page content:Title:...Description:...Content:... and can materially reduce prompt clarity. Consider adding explicit \n separators (or spaces) between fields so the provider receives a readable structured system message.

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.

1 participant