You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ModelOutputThunk field refactor: partition fields into sub-structures #793
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
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.
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.
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.
@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
I like the grouping of GenerationMetadata and having it be a dataclass. I have a few concerns about the specifics:
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.
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.
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.
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.
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.
@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
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
Activity
ajbozarth commentedon Apr 20, 2026
Proposed design
After tracing all call sites, I'd propose: one
GenerationMetadatadataclass onModelOutputThunk, accessed asmot.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
Access:
mot.meta.model,mot.meta.usage,mot.meta.ttfb_ms, etc.mot.metais always present (initialized in__init__asGenerationMetadata()), so callers never need anis not Noneguard 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
mellea/core/base.pyGenerationMetadata; replace 5 flat fields; simplify copy methodsopenai,litellm,ollama,watsonx,huggingface)mot.model = ...→mot.meta.model = ...etc.mellea/telemetry/metrics_plugins.pymot.model→mot.meta.modeletc.mellea/core/__init__.pyGenerationMetadatafor user type hintsHappy to hold until the other open metrics PRs are merged to avoid conflicts.
ajbozarth commentedon Apr 20, 2026
@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
jakelorocco commentedon Apr 22, 2026
I like the grouping of GenerationMetadata and having it be a dataclass. I have a few concerns about the specifics:
._metaproperty comes from. We either need to rework that as well or move the GenerationMetadata to a differently named field.._metadict 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.telemetry_spancan probably moved into another new substructure. All of thehf_output,*_response, etc... fields that consist of the inference provider's response can likely be moved into a better named field._async_queueto_first_chunk_received(maybe with the exception of_start) and._computedare all utilized for figuring out how to actually process / receive the model's output. This seems like a natural grouping._thinkingprobably 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
GenerationMetadatafits into.ajbozarth commentedon Apr 22, 2026
@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 #444psschwei commentedon Apr 23, 2026
Linking to my comment : #909 (comment)
tl/dr version: need to coordinate with the changes in #891 (and not block that issue)
ajbozarth commentedon Apr 23, 2026
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
ajbozarth commentedon Apr 23, 2026
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)