Skip to content

fix(relay): reopen reasoning items after mid-stream finish_reason #432

Closed

Description

@LIghtJUNction

Upstream source

Why this applies to api.lmm.best

apps/api-go/relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go has the same lifecycle bug as the upstream report.

Current LMM behavior:

  1. appendReasoningDelta opens a reasoning item only when reasoningStarted == false.
  2. A non-empty finish_reason calls doneDeltaEvents(), which sets reasoningDone = true and emits response.output_item.done.
  3. If the same upstream stream later emits more reasoning_content, reasoningStarted is still true, so LMM emits another response.reasoning_summary_text.delta for the already-closed resp_*_reasoning_0 item.
  4. The final response also uses a single reasoning builder/item, so multiple reasoning segments are collapsed together.

This is a protocol/lifecycle bug for Chat Completions -> Responses streaming conversion. It does not depend on pricing, groups, routing or provider-specific business logic.

Why not cherry-pick the upstream PR yet

The upstream PR is still open and its first-contributor CI has not executed yet. More importantly, LMM's relaykit has diverged from the current upstream converter:

  • LMM does not have the newer EmitSequenceNumber stream state used by the upstream regression test.
  • LMM's reasoning output is represented through ResponsesOutput.Content, while current upstream uses the newer reasoning Summary representation.
  • Current upstream stream state also contains newer annotation/hosted-tool lifecycle code that is absent from this fork.

So the bug is confirmed, but copying the upstream patch as-is would mix protocol-shape changes with the actual lifecycle fix.

Suggested minimal LMM adaptation

Keep the existing LMM converter shape and change only reasoning-segment lifecycle:

  1. Track a reasoning segment ordinal and persisted text per completed segment.
  2. If a new reasoning delta arrives after reasoningDone, reset the current builder/state and open a new reasoning segment.
  3. Allocate a fresh output index and item ID (resp_*_reasoning_1, resp_*_reasoning_2, ...), never reuse a closed item.
  4. Emit the required response.output_item.added before any delta for the new segment. If LMM adds response.reasoning_summary_part.added, do so consistently for every reasoning segment rather than only resumed segments.
  5. Store each segment's text when it is closed so the final response.completed output contains separate reasoning items in output order instead of concatenating them.
  6. Do not change text/tool item behavior in the same patch unless a separate regression demonstrates the same bug there.

Acceptance criteria

  • Regression sequence reasoning -> tool call -> finish_reason=tool_calls -> reasoning -> finish_reason=stop produces two distinct reasoning output items.
  • No response.reasoning_summary_text.delta references an item after that item has emitted response.output_item.done.
  • The second reasoning segment gets a new output_index and a new item ID.
  • Final completed response preserves round 1 and round 2 as independent reasoning outputs in order; they are not concatenated or overwritten.
  • Existing single-segment reasoning behavior remains unchanged apart from any intentionally added protocol-required reasoning_summary_part.added event.
  • Tests cover both the emitted stream lifecycle and final completed response.
  • go test for the relaykit conversion package and repository CI/release qualification pass.
  • No changes to pricing, group ratios, model price locks, /fast, OAuth restrictions, routing policy, or administrator AI assistant behavior.

Activity

  1. LIghtJUNction commented on Sep 21, 2026

    @LIghtJUNction
    MemberAuthor

    Upstream #7512 has now merged as QuantumNous/new-api@4eb3b91 and provides the separate regression evidence this issue was waiting for: the same post-finish_reason lifecycle bug affects normal message/text items as well as reasoning items.

    Please expand the LMM-native fix/acceptance criteria accordingly:

    • if reasoning resumes after its item was closed, open a new reasoning item with a new ID/output index;
    • if normal content resumes after its message item was closed, open a new message item with a new ID/output index;
    • never append a delta to an already-done item;
    • retain every closed segment in order in the final response.completed.output;
    • keep single-segment IDs/behavior stable;
    • add a regression sequence covering reasoning -> text -> tool_call -> finish_reason=tool_calls -> reasoning -> text -> stop, asserting separate reasoning and message segments before/after the tool call.

    Upstream #7512 also adds reasoning_summary_part.added/done lifecycle events. Treat those as protocol-parity work only if LMM's current Responses event model supports them cleanly; the critical correctness requirement here is reopening both reasoning and message items instead of writing into closed items. No pricing/routing behavior should be coupled to this change.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions