Skip to content

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

Closed

Description

@planetf1

What goes wrong

#1632 makes the OpenAI, LiteLLM, and Ollama backends raise ValueError when the assembled conversation has no user content. That happens when a caller records turns on a SimpleContext and then generates with an empty action (#1597). LocalHFBackend has no such check. The same call sends the model one empty user turn, raises nothing, and returns an answer to a prompt the caller never wrote.

With ibm-granite/granite-4.2-3b the recorded turn is dropped and the model reasons about a message that doesn't exist:

import asyncio

from mellea.backends.huggingface import LocalHFBackend
from mellea.core import CBlock
from mellea.stdlib.context import SimpleContext


async def main():
    backend = LocalHFBackend(model_id="ibm-granite/granite-4.2-3b")
    ctx = SimpleContext().add(CBlock("Summarise this report: revenue rose 12% in Q3."))
    mot, _ = await backend.generate_from_context(CBlock(""), ctx)
    print(repr(await mot.avalue()))


asyncio.run(main())

Output:

'Okay, the user sent a message that\'s just the word "hello". I need to respond appropriately'

Expected: the same ValueError the other backends raise.

Mechanism

Both HF chat paths, standard (mellea/backends/huggingface.py:1782) and KV-cache (:1587), build messages through to_chat() in mellea/backends/utils.py:74. It takes ctx.view_for_generation(), which is empty for SimpleContext by design, appends the action, and hands the result to apply_chat_template without checking for user content. SimpleContext inherits _is_chat_context = True (mellea/core/base.py:2066), so it takes this path.

Capturing the conversation passed to apply_chat_template shows [{'role': 'user', 'content': ''}] on both HuggingFaceTB/SmolLM2-135M-Instruct and ibm-granite/granite-4.2-3b. A non-empty prompt through the same capture shows the expected user turn.

Suggested fix

Call has_user_content() in to_chat() straight after the action is appended and raise the same ValueError as the other backends. One call covers both HF paths. HF already rejects images and audio via _check_no_multimodal_blocks, so only the text and documents branches apply.

has_user_content() lives in mellea/helpers/openai_compatible_helpers.py. Once a non-OpenAI-compatible backend calls it, a backend-neutral helpers module is a better home.

Test: mirror test/backends/test_simple_context_guard.py, with SimpleContext + CBlock("") raising before generation, plus a documents pass-through case.

Scope and limits

  • Out of scope: WatsonxAIBackend (deprecated) and _generate_from_intrinsic, which doesn't append the action.
  • The token-burning stall from feat: add Granite 4.2 model defaults #1587 didn't reproduce here. Granite 4.2-3b answered the blank prompt in 74 tokens against a 1024-token cap (seed 0, one run). On HF the cost is a silently wrong answer.

Activity

  1. added
    bug
    on Oct 2, 2026
  2. planetf1 commented on Oct 5, 2026

    @planetf1
    ContributorAuthor

    Status: confirmed as the sole remaining gap from #1597 — scope is one guard call

    Verified against main at 4c53add4e. Both HF generation paths (standard at ~huggingface.py:1782 and KV-cache at ~:1587) assemble their conversation through the shared to_chat() in mellea/backends/utils.py:92-98 — view_for_generation() (empty for SimpleContext by design) plus the action, handed to apply_chat_template with no user-content check. One guard at that single site therefore covers both paths, matching the ValueError the OpenAI, LiteLLM, and Ollama backends already raise (PR #1632).

    Two scoping notes for whoever picks this up:

    1. Sequence after fix(backends): reject empty user content before sending chat request #1632. PR fix(backends): reject empty user content before sending chat request #1632 adds the has_user_content helper (mellea/helpers/openai_compatible_helpers.py) — it's a generic list[Message] → bool despite the module name, so the HF path should call it rather than re-implement the check. Land this PR against main once fix(backends): reject empty user content before sending chat request #1632 is in; the two don't conflict file-wise (huggingface.py/utils.py vs the three OpenAI-shaped backends) but the helper import does depend on it.
    2. Test shape. The repro in this issue (recorded SimpleContext turn + empty action → expect ValueError) is the regression test; it should assert the same exception type the other three backends raise so the parity holds on the type, not just the behaviour.
  3. added
    p1
    area/backends
    area/stdlib
    on Oct 5, 2026
  4. self-assigned this
    on Oct 6, 2026
  5. added
    triage/accepted
    on Oct 12, 2026
  6. removed their assignment
    on Oct 20, 2026
  7. removed
    triage/accepted
    on Oct 20, 2026
  8. self-assigned this
    on Oct 21, 2026
  9. added a commit that references this issue on Oct 28, 2026
    c9e8949
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/backendsarea/stdlibbugp1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions