Skip to content

ModelOutputThunk field refactor: partition fields into sub-structures #793

Closed

Description

@ajbozarth

Background

Raised in #782 (PR review by @jakelorocco): ModelOutputThunk has grown to have a large number of public fields with different concerns mixed together. As of #782, the public fields are:

  • parsed_repr, tool_calls, _thinking — output/result data
  • usage, model, provider, ttfb_ms, streaming — backend execution metadata

With ttfb_ms and streaming added in #782, now is a good time to address this before the class grows further.

Problem

The fields are all flat on the class, making it harder to understand what group of concerns each belongs to, and making __init__, _copy_from, __copy__, and __deepcopy__ progressively longer with each addition.

Proposed direction

Partition related fields into sub-structures. One concrete proposal is a GenerationMetadata dataclass to hold the backend execution metadata fields (usage, model, provider, ttfb_ms, streaming), accessed as mot.meta or mot.telemetry.

However, as @jakelorocco noted, there may be more than just metadata worth sectioning out, and there may be better groupings than this initial proposal. This issue is meant to open that design discussion before committing to an approach.

Considerations

  • Backwards compatibility — mot.model, mot.usage, etc. are part of the public API. A deprecation cycle with property aliases would be needed.
  • Scope — the refactor touches ModelOutputThunk itself, all backends that populate the metadata fields, both metrics plugins that read from them, and any user-facing docs.
  • Design — worth deciding whether sub-structures should be dataclasses, TypedDicts, or plain classes, and whether they should be optional or always present.

References

Activity

  1. self-assigned this
    on Apr 8, 2026
  2. added this to the Release 0.5.0 milestone on Apr 20, 2026
  3. ajbozarth commented on Apr 20, 2026

    @ajbozarth
    MemberAuthor

    Proposed design

    After tracing all call sites, I'd propose: one GenerationMetadata dataclass on ModelOutputThunk, accessed as mot.meta.

    Why one sub-object (not two, not none)

    All five metadata fields move together as a unit: every backend populates them all in post_processing(), the three metrics plugins consume them all together, and they share the same copy semantics in _copy_from/__copy__/__deepcopy__. Grouping them into one object directly addresses the maintenance concern — the copy methods shrink from 5 field copies to 1.

    Shape

    @dataclass
    class GenerationMetadata:
        usage: dict[str, Any] | None = None
        model: str | None = None
        provider: str | None = None
        ttfb_ms: float | None = None
        streaming: bool = False

    Access: mot.meta.model, mot.meta.usage, mot.meta.ttfb_ms, etc.

    mot.meta is always present (initialized in __init__ as GenerationMetadata()), so callers never need an is not None guard on the object itself — only on the fields within it, as today.

    Back-compat

    All five fields were added in the same telemetry epic over the last ~5 weeks. Given this, a hard break with no deprecated aliases seems appropriate — update all internal callers (5 backends + 3 metrics plugins) in the same PR.

    Blast radius

    Location Change
    mellea/core/base.py Add GenerationMetadata; replace 5 flat fields; simplify copy methods
    5 backends (openai, litellm, ollama, watsonx, huggingface) mot.model = ... → mot.meta.model = ... etc.
    mellea/telemetry/metrics_plugins.py mot.model → mot.meta.model etc.
    mellea/core/__init__.py Export GenerationMetadata for user type hints
    3 test files Update field assignments

    Happy to hold until the other open metrics PRs are merged to avoid conflicts.

  4. ajbozarth commented on Apr 20, 2026

    @ajbozarth
    MemberAuthor

    @jakelorocco I worked with Claude and came up with the above proposal, but wanted to wait too implement it until I got an ok from you on design since you originally pitched the idea. I would also want to wait until my other telemetry PR merge to prevent too many conflicts.

    If this proposal looks good I'll implement it immediately after #882 and #883 are merged

  5. jakelorocco commented on Apr 22, 2026

    @jakelorocco
    Contributor

    I like the grouping of GenerationMetadata and having it be a dataclass. I have a few concerns about the specifics:

    1. MoTs inherit from CBlocks which is where the ._meta property comes from. We either need to rework that as well or move the GenerationMetadata to a differently named field.
    2. The ._meta dict is used for purposes beyond the properties in GenerationMetadata. We store the response from the model in that field. That is partly unplanned and mostly grew out of a slow drift away from it's original purpose. We can probably rename this field.
    3. As a part of (2), we should actually restructure how we handle some of the existing _meta fields as well. The existing ._meta is a catchall, and we should also refactor the fields that remain in it. For instance, telemetry_span can probably moved into another new substructure. All of the hf_output, *_response, etc... fields that consist of the inference provider's response can likely be moved into a better named field.
    4. Substructure for "Computation Logistics" (the name could be better...). Basically everything from _async_queue to _first_chunk_received (maybe with the exception of _start) and ._computed are all utilized for figuring out how to actually process / receive the model's output. This seems like a natural grouping.
    5. This proposal should also probably look to standardize how we want to externalize any of the remaining fields. For instance, _thinking probably shouldn't be private.

    This is also a lot more work than your initial proposal, but I think it can be done in self-contained chunks. I mostly outlined the above to make sure that we have a consistent approach the GenerationMetadata fits into.

  6. ajbozarth commented on Apr 22, 2026

    @ajbozarth
    MemberAuthor

    @jakelorocco I've opened #908 adding GenerationMetadata, that change blocks #890 so I wanted to get it in quickly. I then opened #909 as a follow up part 2 to this work addressing the other items you proposed. We can further break that issue into subtasks if desired, I know the item about spans might be caught by my initial cleanup work when I pick up #444

  7. psschwei commented on Apr 23, 2026

    @psschwei
    Contributor

    Linking to my comment : #909 (comment)

    tl/dr version: need to coordinate with the changes in #891 (and not block that issue)

  8. ajbozarth commented on Apr 23, 2026

    @ajbozarth
    MemberAuthor

    need to coordinate with the changes in #891 (and not block that issue)

    I would argue the changes in #908 going in first would only aid in the development of #891 by moving all the metadata into a single object. Though the follow up work in #909 definitely make sense to hold off on until #891 is done, or include it in that work peripherally.

    Either way I'll make sure to sync with @planetf1 tomorrow so we're on the same page

  9. ajbozarth commented on Apr 23, 2026

    @ajbozarth
    MemberAuthor

    Had Claude analyze the potential conflict and added #908 (comment) detailing the small refactor #891 will need after that PR is merged (it's tiny)

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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions