Skip to content

refactor(backends): unify mot.raw.response shape across streaming and non-streaming #1330

Closed

Description

@ajbozarth

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 a choices array, top-level usage, model, id.
  • Streaming: the merged choice-level dict returned by chat_completion_delta_merge(...) — has finish_reason / message at the top, no choices wrapper, no top-level usage.

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}.py post_processing — tool extraction (response["choices"][0] vs. response).
  • cli/serve/utils.py:extract_finish_reason — choices[0].finish_reason vs. top-level finish_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_processing wraps 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, and chat.py:_parse then read response["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.response is 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, and chat.py:_parse.
  • Streaming tests confirm finish_reason and usage resolve from the top-level response.

Activity

  1. added
    area/backends
    area/stdlib
    on Jun 23, 2026
  2. added
    p1
    on Jun 24, 2026
  3. cptnm3 commented on Jul 7, 2026

    @cptnm3
    Contributor

    Hi @ajbozarth , @psschwei I'd like to work on this.

  4. ajbozarth commented on Jul 7, 2026

    @ajbozarth
    MemberAuthor

    @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 :)

  5. ajbozarth commented on Jul 20, 2026

    @ajbozarth
    MemberAuthor

    @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.

  6. cptnm3 commented on Jul 20, 2026

    @cptnm3
    Contributor

    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

  7. ajbozarth commented on Jul 20, 2026

    @ajbozarth
    MemberAuthor

    I was occupied last week but I would still like to give this a shot this week. I hope that's ok

    No problem, was just checking in since it had been a couple weeks with no PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/backendsarea/stdlibp1refactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions