Repository navigation
Description
Deferred from the mot.raw migration (#1329, closes #1215).
Summary
For the OpenAI-shaped backends (openai, watsonx, litellm), mot.raw.response holds a different shape depending on the path:
- Non-streaming: the full top-level response dict (
chunk.model_dump()) — has achoicesarray, top-levelusage,model,id. - Streaming: the merged choice-level dict returned by
chat_completion_delta_merge(...)— hasfinish_reason/messageat the top, nochoiceswrapper, no top-levelusage.
This is not by design; it is an artifact of how each path assembles its response (dump the whole object vs. reassemble from deltas). It predates #1215 — the old _meta keys had the same split, and oai_chat_response_choice existed precisely to give tool extraction a consistent choice-level view across both paths.
#1329 preserved this per-path split (consumers detect the shape via "choices" in response) rather than change behavior during a container refactor.
Why it matters
Consumers reading mot.raw.response must branch on shape, which partly defeats the point of a typed escape hatch. Three call sites already carry the shape-detection logic:
mellea/backends/{openai,watsonx,litellm}.pypost_processing— tool extraction (response["choices"][0]vs.response).cli/serve/utils.py:extract_finish_reason—choices[0].finish_reasonvs. top-levelfinish_reason.mellea/stdlib/components/chat.py:_parse— both the tool-call and non-tool branches (response["choices"][0]vs.response["message"]).
Proposed work
Make mot.raw.response always the top-level shape:
- Streaming
post_processingwraps the merged choice into a top-level envelope, e.g.{"choices": [merged], "usage": <streamed usage>, ...}, instead of storing the bare choice. - Tool extraction,
extract_finish_reason, andchat.py:_parsethen readresponse["choices"][0]unconditionally, dropping the shape-detection branches. - Folding streamed usage into the envelope could also remove the separate streaming-usage handling (usage would always come from
response["usage"]).
Likely requires either changing chat_completion_delta_merge to return top-level, or wrapping its output at each call site. Touches openai, watsonx, litellm, and the three consumer call sites; behavior-affecting, so it needs its own PR with streaming tests per backend.
Acceptance
-
mot.raw.responseis the top-level response shape on both paths for all OpenAI-shaped backends. - Shape-detection branches removed from all three consumer sites: backend tool extraction,
extract_finish_reason, andchat.py:_parse. - Streaming tests confirm
finish_reasonandusageresolve from the top-level response.
Activity
cptnm3 commentedon Jul 7, 2026
Hi @ajbozarth , @psschwei I'd like to work on this.
ajbozarth commentedon Jul 7, 2026
@cptnm3 feel free to take a stab at this and open a PR. Tag @jakelorocco and me for review, we were the ones who decided to leave this as a follow up issue :)
ajbozarth commentedon Jul 20, 2026
@cptnm3 are you still interested in picking this up? If not I'll unassigned so others could choose to pick up, no pressure either way.
cptnm3 commentedon Jul 20, 2026
Hi @ajbozarth, thanks for checking in. I was occupied last week but I would still like to give this a shot this week. I hope that's ok
ajbozarth commentedon Jul 20, 2026
No problem, was just checking in since it had been a couple weeks with no PR :)