Skip to content

fix(prompt): require a non-empty answer body in the RAG system prompt - #547

Merged
Ahmath-Gadji merged 1 commit into
mainfrom
fix/llm-always-answer-prompt
Jun 24, 2026
Merged

fix(prompt): require a non-empty answer body in the RAG system prompt#547
Ahmath-Gadji merged 1 commit into
mainfrom
fix/llm-always-answer-prompt

Conversation

@EnjoyBacon7

@EnjoyBacon7 EnjoyBacon7 commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Problem

A streamed RAG answer can collapse to empty content — e.g. the model emits little more than the mandated [Sources: ...] line, which extract_and_strip_sources_block() then strips to "". This yields a blank assistant turn.

On the next request the client (e.g. Twake) replays that blank turn in the chat history, and the upstream LLM (Mistral via litellm) rejects it:

litellm.BadRequestError: OpenAIException - Invalid assistant message:
role='assistant' content='' tool_calls=None  (HTTP 400)

Observed in production: sending test to a partition triggers the 400 on the follow-up turn.

Fix

Add a rule to sys_prompt_tmpl.txt requiring the model to always produce at least one sentence of text — and, when no source is relevant, to briefly say the documents don't cover the question before ending with [Sources: none] — rather than returning an empty body.

Scope / follow-up

This is a prompt-level mitigation and reduces (does not eliminate) blank turns. The robust fix is a server-side guard that drops/coalesces empty assistant messages before forwarding history to the LLM (the inbound check in routers/openai.py only validates the last message). Tracking that separately.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Ensured the assistant always provides at least one sentence in responses, preventing empty response bodies. Added a fallback explanation when insufficient context is available.

A streamed RAG answer could end up empty (e.g. the model emits only the
`[Sources: ...]` line, which extract_and_strip_sources_block() strips to ""),
producing a blank assistant turn. When that blank turn is replayed in the
chat history on the next request, the upstream LLM (Mistral via litellm)
rejects it with `Invalid assistant message: role='assistant' content=''` (400).

Add a rule instructing the model to always produce at least one sentence of
text, and to briefly explain the documents don't cover the question (then
`[Sources: none]`) rather than returning an empty body.

This is a prompt-level mitigation; a server-side guard that drops/coalesces
empty assistant messages before forwarding history remains the robust fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 202cea7a-11e7-4383-beb3-fbf88841b8e5

📥 Commits

Reviewing files that changed from the base of the PR and between a24e83c and 52be26f.

📒 Files selected for processing (1)
  • prompts/example1/sys_prompt_tmpl.txt

📝 Walkthrough

Walkthrough

A single rule is appended to the system prompt template (prompts/example1/sys_prompt_tmpl.txt) requiring the assistant to always output at least one sentence of text, including a fallback instruction for when the retrieved context does not cover the question.

Changes

System Prompt Non-empty Response Rule

Layer / File(s) Summary
Non-empty response rule
prompts/example1/sys_prompt_tmpl.txt
Adds one explicit rule at line 14 mandating that the response body always contains at least one sentence, with a fallback message when the provided context is insufficient.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐇 A rule so small, just one line long,
To keep replies from going wrong,
When context fails, don't stay mute,
A sentence blooms from every root.
Hop along — no empty stew! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and concisely summarizes the main change: enforcing non-empty answer bodies in the RAG system prompt.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/llm-always-answer-prompt

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Fix issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants