Repository navigation
fix(backends): guard LocalHFBackend against empty user content - #1757
Conversation
Both HF chat paths (standard and KV-cache) assemble their conversation through to_chat(), which had no user-content check. A SimpleContext with recorded turns plus an empty action sent the model a blank user prompt, which it answered silently instead of raising like the OpenAI, LiteLLM, and Ollama backends do (generative-computing#1597). Call has_user_content() in to_chat() right after the action is appended and raise the same ValueError, covering both paths with one guard. HF rejects images and audio upstream (_check_no_multimodal_blocks), so only the text and documents branches of the check apply here. Closes generative-computing#1705 Assisted-by: Claude Code Signed-off-by: Mark Sturdevant <[email protected]>
jakelorocco
left a comment
There was a problem hiding this comment.
I think this is okay from the current perspective; but I actually disagree with the has_user_content check. I think we should check if the context is empty (or if there is no custom system prompt).
Some times we may want to just replay a model output thunk or a tool message. It's also possible that a user may have put everything into the system prompt.
|
|
||
| assert any(msg["role"] == "user" for msg in conversation), ( | ||
| "User message with documents must pass the guard even when its text " | ||
| "content is empty." | ||
| ) | ||
|
|
||
|
|
||
| def test_hf_guard_does_not_trigger_when_ctx_has_real_user_message(): | ||
| """Guardian-style regression: a chat context with real history is left alone. | ||
|
|
||
| Counterpart of the Ollama test above: a `ChatContext` that already carries | ||
| a real user turn must pass the guard even when the action itself is an | ||
| empty structural placeholder. | ||
| """ | ||
| from mellea.backends.utils import to_chat | ||
| from mellea.core import CBlock | ||
| from mellea.stdlib.components import Message as MelleaMessage | ||
| from mellea.stdlib.context import ChatContext | ||
|
|
||
| backend = _make_hf_backend() | ||
| gctx = ChatContext() | ||
| gctx = gctx.add(MelleaMessage("user", "Is this code safe?")) | ||
| gctx = gctx.add(MelleaMessage("assistant", "It looks fine.")) | ||
|
|
||
| conversation = to_chat(CBlock(value=""), gctx, backend.formatter, None) | ||
|
|
||
| assert any(msg["role"] == "user" for msg in conversation), ( | ||
| "ChatContext with real user history must pass the guard even when " | ||
| "the action is empty — see guardian-style usage pattern." | ||
| ) | ||
|
|
There was a problem hiding this comment.
Can we also just add an assertion here that some mock method in generate from backend gets called or something so that we know for sure it passes?
There was a problem hiding this comment.
updated to go through backend.generate_from_context() and then assert apply_chat_template.called.
Open to suggestions if I missed the mark on that.
Review feedback on generative-computing#1757: the two HF pass-through tests called to_chat() directly and inferred the guard's pass from its return value. Drive the full backend path instead and assert apply_chat_template was invoked on the mocked tokenizer, so the pass is observed where generation actually happens. The background generation task is awaited so a failure there fails the test rather than warning. Assisted-by: Claude Code Signed-off-by: Mark Sturdevant <[email protected]>
Yeah that's an interesting improvement. From a PR perspective, would it make sense to merge this fix this backend to parity w/ the other 2 before we change the has_user_content() criteria (and name) to impact/improve all the backends? Or is the feeling that this current parity PR is not worth doing right now? I'm open to either way, but from where I sit (biased) this PR is neat and clean to get parity and close the issue as written. I could then do the follow-up issue that affects all the backends next and there could be some design decision churn on that issue (but should be nicely isolated to one helper function change). |
jakelorocco
left a comment
There was a problem hiding this comment.
I think it makes sense for the backends to be at parity. Please create a follow-up issue / pr for the behavior change. Thank you!
c9e8949
Pull Request
Issue
Fixes #1705
Description
LocalHFBackendwas the last chat backend without an empty-user-content guard: aSimpleContextwith recorded turns plus an empty action assembled a conversation whose only user message was blank, which the model answered silently instead of raising (the OpenAI, LiteLLM, and Ollama backends already raiseValueErrorhere, #1597/#1632).Both HF chat paths (standard and KV-cache) assemble their conversation through the shared
to_chat()helper, so the fix is one guard call there, right after the action is appended — raising the sameValueErrorwith the same message as the other three backends. HF rejects images and audio upstream (_check_no_multimodal_blocks), so only the text and documents branches ofhas_user_content()apply on this path.Follow-up tracked in #1756: relocate
has_user_content()frommellea/helpers/openai_compatible_helpers.pyto a backend-neutral helpers module.Testing
Attribution
Adding a new component, requirement, sampling strategy, or tool?
If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.
NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.
🤖 Generated with Claude Code