Skip to content

fix(backends): guard LocalHFBackend against empty user content - #1757

Merged
markstur merged 2 commits into
generative-computing:mainfrom
markstur:issue_1705
Oct 28, 2026
Merged

markstur merged 2 commits into
generative-computing:mainfrom
markstur:issue_1705

Conversation

@markstur

Copy link
Copy Markdown
Contributor

Pull Request

Issue

Fixes #1705

Description

LocalHFBackend was the last chat backend without an empty-user-content guard: a SimpleContext with 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 raise ValueError here, #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 same ValueError with 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 of has_user_content() apply on this path.

Follow-up tracked in #1756: relocate has_user_content() from mellea/helpers/openai_compatible_helpers.py to a backend-neutral helpers module.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

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.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

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

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]>
@markstur
markstur requested a review from a team as a code owner October 21, 2026 18:25
@github-actions github-actions Bot added the bug Something isn't working label Oct 21, 2026

@jakelorocco jakelorocco 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.

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.

Comment on lines +527 to +557

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."
)

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.

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?

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.

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]>
@markstur

markstur commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

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 jakelorocco 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.

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!

@markstur
markstur added this pull request to the merge queue Oct 27, 2026
Merged via the queue into generative-computing:main with commit c9e8949 Oct 28, 2026
12 checks passed
@markstur
markstur deleted the issue_1705 branch October 28, 2026 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LocalHFBackend silently answers a blank prompt when SimpleContext is given an empty action

2 participants