Fixing Stream Response Compatibility: Handling Missing Termination Frames

    This article explains why a streaming response may disconnect immediately after generation finishes when a gateway-forwarded model omits the standard termination frame. It covers the root cause, how a missing finish_reason triggers a parser error that is misclassified as a transport failure, a minimal conditional fix, a configuration checklist, regression testing steps, and engineering practices for maintaining protocol tolerance across multiple upstreams.

    1. Symptom: The Highest-Quota Model Disconnects Almost Immediately

    A new model was recently added to the subscription plan of an AI coding tool. It offers the highest call quota in the plan and looks ideal as a primary model. In practice, however, the experience is very different: requests are sent successfully and the response starts streaming token by token, but the connection drops as soon as generation ends. Multi-turn conversations can barely be completed.

    This behavior is easy to misdiagnose as network jitter, insufficient quota, or a client bug. The actual root cause lies in the protocol details of the streaming response.

    To understand the issue, it helps to separate three layers:

    • Transport layer: whether the request is established and whether the byte stream keeps arriving;
    • Protocol layer: whether SSE events are properly paired and whether termination semantics are complete;
    • Application layer: whether the client parser can reconstruct structured messages from the byte stream.

    In this case, the transport layer works fine. The problem is in how the protocol layer interacts with the parser.

    2. Root Cause: A Missing Termination Frame Causes the Parser to Throw

    The new model is not native to the gateway; it is forwarded through a third-party platform gateway. During forwarding, the upstream streaming SSE response lacks standard termination semantics, with two distinct characteristics:

    1. finish_reason remains null throughout the stream and never takes a terminal value such as stop or length;
    2. There is no agreed end marker at the end of the stream (a sentinel frame such as data: [DONE]).

    The OpenAI-compatible streaming parser used by the client is designed on the assumption that “a termination reason must be readable when the stream ends.” When the byte stream closes naturally but the parser cannot read any termination reason, it throws the following error:

    1. Stream ended without finish_reason

    The critical part is how the error is classified. The upper-layer adapter categorizes this exception as a TRANSPORT error, which triggers the reconnection and disconnection logic: the connection drops immediately after each generation completes, and the user sees “disconnected right after the result appears.” The chain can be summarized as:

    1. Gateway-converted stream lacks a termination frame
    2. ↓
    3. Parser cannot read finish_reason and throws
    4. ↓
    5. Adapter misclassifies it as a transport failure
    6. ↓
    7. Session disconnects immediately after generation ends

    This is not a capability limitation of the model itself, nor a wrong API choice—the behavior is identical on both the “chat completions” and “responses” interfaces, which shows the problem is in the intermediate conversion layer. It is a compatibility defect in how the gateway converts a non-native model into a standard stream format.

    3. Fix Approach: A Minimal Conditional Tolerance Check

    The goal is not to “ignore all exceptions,” but to precisely identify the one scenario that can be safely allowed through: the stream has ended naturally, content blocks have already been collected, and only the termination reason is missing. In that case, it can be treated as a normal completion.

    An illustrative change is shown below (file paths and line numbers vary by version; refer to the actual installation directory):

    1. - if (!hasFinishReason) {
    2. + if (!hasFinishReason && blocks.length === 0) {
    3. throw new Error("Stream ended without finish_reason");
    4. }
    5. // Content received but termination frame missing: treat as normal completion
    6. + stopReason = "stop";

    This change alters only one condition but preserves the safety boundary:

    • Content received (blocks.length > 0) and the stream ended naturally: treated as normal completion, avoiding a false disconnect;
    • No content at all (blocks.length === 0): still throws, so a genuinely empty response is not silently swallowed.

    In other words, what is allowed through is a “protocol flaw,” not a “business exception.” This is a commonly overlooked point in streaming fault tolerance: tolerance must be conditional, and the condition must be clearly expressed and covered by tests.

    4. Prerequisites and Configuration Checklist

    Even after the code-level fix, the issue can still occur due to missing configuration. In practice, check the following in order:

    1. Data sharing / call authorization switch: some plans require explicitly enabling the shared call data option on the management page; otherwise requests are rejected;
    2. Quota and rate limits: confirm the plan’s quota and concurrency limit for the model to avoid mistaking rate limiting for a disconnect;
    3. Client version consistency: the parser and adapter must be the same version; mixed dependencies can easily cause path and behavior differences;
    4. Log levels: record TRANSPORT and PROTOCOL errors separately to quickly determine whether it is a network issue or a format issue.

    5. Verification and Regression Methods

    After the fix, run a minimal regression round rather than stopping after a single successful attempt:

    • Normal stream: multi-turn long-text generation, confirming the ending no longer disconnects;
    • Empty response: construct a return with no content, confirming it still errors instead of silently succeeding;
    • Interrupted stream: disconnect midway, confirming the reconnection strategy is not falsely triggered;
    • Concurrency: multiple parallel sessions, observing whether stream crossing or misalignment occurs.

    6. Engineering Takeaways: Making “Protocol Tolerance” a Maintainable Capability

    This type of problem recurs in multi-cloud, multi-model integration scenarios. A more robust approach is to extract compatibility logic from business code into a configurable adapter layer: for known upstreams with missing termination frames, treat “content present means completed”; for unknown upstreams, keep strict validation. Also retain a switchable fallback path and alert instrumentation so compatibility issues are visible and traceable in the monitoring and alerting system.

    If an enterprise wants to reduce the cost of self-maintaining such logic, it can use a general-purpose container platform from a cloud vendor to deploy a unified gateway, together with logging and monitoring services for issue localization, turning compatibility adaptation into a platform capability rather than one-off patches scattered across clients. This preserves the high-quota advantage of the new model while bringing the stability of the streaming path back under control.