Skip to content

refactor: move generate_from_raw hook firing into Backend base class #1183

Closed

Description

@ajbozarth

Background

PR #1181 (Phase 1 of #444) introduces the generation_batch_{pre_call,post_call,error} hooks for raw-path coverage. The chat path was simultaneously cleaned up: Backend.generate_from_chat_context is now a base-class wrapper that fires hooks and calls an abstract _generate_from_chat_context_impl per backend.

The raw path was not given the same treatment. Hook-firing logic is duplicated inline across all 5 backends' generate_from_raw implementations:

  • mellea/backends/openai.py (~lines 1192, 1233, 1288)
  • mellea/backends/litellm.py
  • mellea/backends/ollama.py
  • mellea/backends/huggingface.py
  • mellea/backends/watsonx.py

Each one redoes UUID generation, pre_call firing, try/except for error firing, latency timing, and post_call firing.

Why this matters

Beyond the duplication: the duplication blocks unit tests of the firing sites. _MockBackend.generate_from_raw would have to reimplement the production firing logic verbatim, making any test of "do the hooks fire correctly" tautological. Today's test/plugins/test_hook_call_sites.py covers chat firing because the base class owns it — raw firing has no equivalent coverage.

Scope

Mirror the chat-path fix:

  1. Backend.generate_from_raw becomes a concrete base method that fires the three batch hooks.
  2. Add an abstract _generate_from_raw_impl that each backend overrides.
  3. Strip the inline hook-firing blocks from all 5 backend implementations.
  4. Add base-class firing-site tests using a _MockBackend that overrides _generate_from_raw_impl only.

Acceptance criteria

  • No HookType.GENERATION_BATCH_* references remain in any mellea/backends/*.py file.
  • Backend.generate_from_raw fires all three batch hooks; backends only implement _generate_from_raw_impl.
  • Firing-site tests cover all three batch hooks against a mock backend.
  • All five backend implementations pass existing tests unchanged.

Discovered

While writing call-site tests for #1181.

Activity

  1. self-assigned this
    on Jun 1, 2026
  2. added a commit that references this issue on Jun 12, 2026
    8e4a730
  3. added a commit that references this issue on Jun 17, 2026
    b703c46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions