fix(prompt): require a non-empty answer body in the RAG system prompt - #547
Conversation
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>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughA single rule is appended to the system prompt template ( ChangesSystem Prompt Non-empty Response Rule
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Problem
A streamed RAG answer can collapse to empty content — e.g. the model emits little more than the mandated
[Sources: ...]line, whichextract_and_strip_sources_block()then strips to"". This yields a blankassistantturn.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:
Observed in production: sending
testto a partition triggers the 400 on the follow-up turn.Fix
Add a rule to
sys_prompt_tmpl.txtrequiring 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
assistantmessages before forwarding history to the LLM (the inbound check inrouters/openai.pyonly validates the last message). Tracking that separately.🤖 Generated with Claude Code
Summary by CodeRabbit