Skip to content

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

Merged
ajbozarth merged 10 commits into
generative-computing:mainfrom
cptnm3:unify-mot-raw-response
Jul 28, 2026
Merged

ajbozarth merged 10 commits into
generative-computing:mainfrom
cptnm3:unify-mot-raw-response

Conversation

@cptnm3

@cptnm3 cptnm3 commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

Issue

Fixes #1330

Description

Streaming responses from the OpenAI, Watsonx, and LiteLLM backends previously stored a bare choice-level dict in mot.raw.response ({"message": …, "finish_reason": …}), while non-streaming stored a full top-level envelope ({"choices": […], "usage": …}). Every consumer had to branch on "choices" in response to handle both shapes.

Wrap the chat_completion_delta_merge() result in a top-level envelope in post_processing() for all three backends:

merged = chat_completion_delta_merge(mot.raw.streamed_chunks)
mot.raw.response = {"choices": [merged], "usage": mot.generation.usage}

This makes mot.raw.response always carry the top-level shape, on both the streaming and non-streaming paths.

Consumer simplifications:

  • cli/serve/utils.py extract_finish_reason: remove the top-level finish_reason fallback; read choices[0].finish_reason unconditionally.
  • mellea/stdlib/components/chat.py _parse tool-call branch: replace the "choices" in response guard with a direct choices[0] index.
  • mellea/stdlib/components/chat.py _parse non-tool branch: replace the dual-fallback (response.get("choices") or [{}] + response.get("message")) with response["choices"][0].get("message", {}).

Tests:

  • Update test_parse_openai_streamed_choice_shape, test_parse_tool_calls_openai_streamed_choice_shape, test_parse_tool_calls_openai_streamed_carries_thinking, and test_tool_call_message_survives_formatter_to_openai_history to use the new top-level envelope shape.
  • Replace test_litellm_per_choice_dict_fallback with test_litellm_normalized_streaming_shape.
  • Add test_openai_streaming_normalized_shape and test_watsonx_streaming_normalized_shape to test_serve_utils.py.
  • Add test_parse_openai_streaming_normalized_shape and test_parse_tool_calls_openai_streaming_normalized_shape to test_chat.py.
  • Add test/backends/test_openai_post_processing.py, test_watsonx_post_processing.py, and test_litellm_post_processing.py with post_processing shape assertions and usage/canary tests.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

… non-streaming

Streaming responses from the OpenAI, Watsonx, and LiteLLM backends
previously stored a bare choice-level dict in mot.raw.response
({"message": …, "finish_reason": …}), while non-streaming stored a
full top-level envelope ({"choices": […], "usage": …}).  Every consumer
had to branch on "choices" in response to handle both shapes.

Wrap the chat_completion_delta_merge() result in a top-level envelope
in post_processing() for all three backends:

    merged = chat_completion_delta_merge(mot.raw.streamed_chunks)
    mot.raw.response = {"choices": [merged], "usage": mot.generation.usage}

This makes mot.raw.response always carry the top-level shape, on both
the streaming and non-streaming paths.

Consumer simplifications:
- cli/serve/utils.py extract_finish_reason: remove the top-level
  finish_reason fallback; read choices[0].finish_reason unconditionally.
- mellea/stdlib/components/chat.py _parse tool-call branch: replace
  the "choices" in response guard with a direct choices[0] index.
- mellea/stdlib/components/chat.py _parse non-tool branch: replace the
  dual-fallback (response.get("choices") or [{}] + response.get("message"))
  with response["choices"][0].get("message", {}).

Tests:
- Update test_parse_openai_streamed_choice_shape,
  test_parse_tool_calls_openai_streamed_choice_shape,
  test_parse_tool_calls_openai_streamed_carries_thinking, and
  test_tool_call_message_survives_formatter_to_openai_history to use
  the new top-level envelope shape.
- Replace test_litellm_per_choice_dict_fallback with
  test_litellm_normalized_streaming_shape.
- Add test_openai_streaming_normalized_shape and
  test_watsonx_streaming_normalized_shape to test_serve_utils.py.
- Add test_parse_openai_streaming_normalized_shape and
  test_parse_tool_calls_openai_streaming_normalized_shape to test_chat.py.
- Add test/backends/test_openai_post_processing.py,
  test_watsonx_post_processing.py, and test_litellm_post_processing.py
  with post_processing shape assertions and usage/canary tests.

Signed-off-by: Vishal V <[email protected]>
@cptnm3
cptnm3 requested a review from a team as a code owner July 27, 2026 18:57
@github-actions github-actions Bot added the enhancement New feature or request label Jul 27, 2026

@ajbozarth ajbozarth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, overall it LGTM, but I did note a bunch of tiny "nits" to fix, all in docstrings and comments, but some important to not regress

Comment on lines -628 to -630
# OpenAI-like streamed responses potentially give you chunks of tool calls.
# As a result, we have to store data between calls and only then
# check for complete tool calls in the post_processing step.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC these comment lines should stay, only the last 2 are about the code being removed

Comment thread mellea/backends/openai.py
Comment on lines -1132 to -1134
# OpenAI streamed responses give you chunks of tool calls.
# As a result, we have to store data between calls and only then
# check for complete tool calls in the post_processing step.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Comment on lines -613 to -615
# OpenAI streamed responses give you chunks of tool calls.
# As a result, we have to store data between calls and only then
# check for complete tool calls in the post_processing step.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

Comment thread test/cli/test_serve_utils.py Resolved
Comment thread test/cli/test_serve_utils.py Resolved
Comment thread test/stdlib/components/test_chat.py Resolved
Comment thread test/stdlib/components/test_chat.py Resolved
cptnm3 and others added 9 commits July 28, 2026 13:58
Co-authored-by: Alex Bozarth <[email protected]>
Signed-off-by: Vishal V <[email protected]>
Co-authored-by: Alex Bozarth <[email protected]>
Signed-off-by: Vishal V <[email protected]>
Co-authored-by: Alex Bozarth <[email protected]>
Signed-off-by: Vishal V <[email protected]>
Co-authored-by: Alex Bozarth <[email protected]>
Signed-off-by: Vishal V <[email protected]>
…reaming and non-streaming difference

Signed-off-by: Vishal V <[email protected]>
@cptnm3
cptnm3 force-pushed the unify-mot-raw-response branch from b42351c to 8815482 Compare July 28, 2026 08:38
@cptnm3

cptnm3 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@ajbozarth thanks for the review! I have applied all the suggestions. Please take a look.

@ajbozarth ajbozarth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the great work, this was exactly what I had intended to do when opening the issue(with a lot more tests than I probably would have added)

@jakelorocco if you want to take a quick sanity check that this follows what you envisioned, if you're happy feel free to add to the merge queue

@jakelorocco
jakelorocco added this pull request to the merge queue Jul 28, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jul 28, 2026
@ajbozarth
ajbozarth added this pull request to the merge queue Jul 28, 2026
Merged via the queue into generative-computing:main with commit d72a319 Jul 28, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants