OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: Gateway rejects Responses streams whose terminal snapshot has an empty output array

Open Terryli246 opened this issue 4 weeks ago • 0 comments

Issue Origin

Observed or reproduced in a real environment

Bug Description

The OpenViking Gateway's Responses protocol rejects any upstream stream whose terminal response.completed snapshot carries an empty output array, even when every streamed output item was closed with a matching response.output_item.done event. The gateway turns the round into a response.failed event, and Codex clients surface it as stream disconnected before completion: Responses output items are missing.

Upstreams that emit a sparse lifecycle snapshot — the terminal object without the output array, with the items only in the event stream — are the normal shape for OpenAI's ChatGPT Codex backend (chatgpt.com/backend-api/codex), which the Codex CLI itself accepts. The gateway currently treats the terminal object as authoritative and rejects the reply, so every request routed to such an upstream fails, no matter how well-formed the stream was.

The strict check lives in ResponsesProtocol.end():

# openviking_gateway/tool_protocols/responses.py:230-234 (main @ 5760af6ff41fdc7b74056a7b408807ee7cdbc9b1)
if self.body.get("stream"):
    # The terminal object is authoritative, including opaque reasoning
    # data that need not appear in deltas. Never reconstruct signatures.
    if set(self.items) != set(range(len(self.output))) or self.closed != set(self.items):
        raise ToolLoopError("Responses output items are missing")

self.items holds the indexes seen in response.output_item.added / response.output_item.done events and self.output is the terminal object's output array. With a sparse terminal (output: []) and one streamed item, set(self.items) is {0} while set(range(len(self.output))) is {}, so the check raises even though self.closed == set(self.items) — i.e. nothing was truncated, every item was closed properly.

This is not a regression: the check is present in the file since it was introduced in #5795 (2026-10-09).

Steps to Reproduce

  1. Run the OpenViking Gateway with an upstream that speaks the Responses API and returns a sparse terminal snapshot (OpenAI's ChatGPT Codex backend is one such upstream).
  2. Send a POST /v1/responses request with stream: true through the gateway (/gateway/api/compatible/v1/responses).
  3. The upstream streams response.output_item.added / response.output_item.done for the reply, then a response.completed whose output array is [].
  4. The gateway replies with a response.failed event whose error.message is Responses output items are missing, and the client reports stream disconnected before completion: Responses output items are missing.

Reproduced against the installed package directly (no server involved):

import sys
sys.path.insert(0, "/path/to/site-packages")
from openviking_gateway.tool_protocols.responses import ResponsesProtocol

adapter = ResponsesProtocol({"stream": True, "model": "m"})
adapter.begin()
adapter.accumulate({"type": "response.output_item.added", "output_index": 0,
                    "item": {"type": "message", "id": "msg_1"}})
adapter.accumulate({"type": "response.output_item.done", "output_index": 0,
                    "item": {"type": "message", "id": "msg_1"}})
adapter.accumulate({"type": "response.completed",
                    "response": {"status": "completed", "output": [], "usage": {}}})
adapter.end()   # ToolLoopError: Responses output items are missing

Expected Behavior

The gateway should accept the round. When the terminal snapshot carries no output array but the stream closed every item it announced, the streamed items are the complete record of the reply and should be used as the output — the same items the client already received. The reply's function_call items must still reach the client so the tool round can continue.

Actual Behavior

ToolLoopError("Responses output items are missing") is raised, the gateway answers with a response.failed event, and the client aborts the turn with stream disconnected before completion: Responses output items are missing.

Measured on OpenViking 0.5.3:

  • 7/7 recorded upstream streams from an OpenAI-route provider failed the check (plain text and function_call replies, two model ids). The one recorded stream from a provider that returns a full terminal output array passed.
  • Replaying the same recorded streams after treating the closed streamed items as the output when the terminal array is empty: 0/8 failures, and the function_call items are still extracted for the client.
  • Two real Codex client turns failed with exactly this error (2026-11-04 06:01:49 and 06:37:03 UTC+8); between 2026-11-04 00:00 and 07:00 UTC+8, 198 gateway requests were routed through that upstream.

No data is lost or corrupted: the reply text was already streamed to the client before the failure. The turn aborts before the round is committed, so the client sees a failed turn instead of a finished reply.

Minimal Reproducible Example

import sys
sys.path.insert(0, "/path/to/site-packages")
from openviking_gateway.tool_protocols.responses import ResponsesProtocol


def adapter_for(events):
    adapter = ResponsesProtocol({"stream": True, "model": "m"})
    adapter.begin()
    for event in events:
        adapter.accumulate(event)
    return adapter


message = {"type": "message", "id": "msg_1"}
sparse = adapter_for([
    {"type": "response.output_item.added", "output_index": 0, "item": message},
    {"type": "response.output_item.done", "output_index": 0, "item": message},
    {"type": "response.completed",
     "response": {"status": "completed", "output": [], "usage": {}}},
])
sparse.end()  # raises ToolLoopError("Responses output items are missing")

full = adapter_for([
    {"type": "response.output_item.added", "output_index": 0, "item": message},
    {"type": "response.output_item.done", "output_index": 0, "item": message},
    {"type": "response.completed",
     "response": {"status": "completed", "output": [message], "usage": {}}},
])
full.end()   # ok

Error Logs

client (Codex): stream disconnected before completion: Responses output items are missing
gateway SSE tail: {"type": "response.failed", "response": {"status": "failed",
  "error": {"code": "server_error", "message": "Responses output items are missing"}, ...}}

OpenViking Version

0.5.3

Python Version

3.13.15

Operating System

macOS

Model Backend

OpenAI

Additional Context

The same sparse-snapshot shape is tracked in lidge-jun/opencodex#893 ("[Bug]: Responses-compatible gateways can return sparse lifecycle snapshots that Codex clients do not commit", closed) and handled there by an opt-in provider flag responsesSnapshotRepair. That repair backfills missing canonical fields and closing events, but it deliberately leaves an explicit output: [] authoritative, and the streaming path does not reconstruct the terminal array — so the snapshot still reaches the gateway in this shape.

A local workaround is to use the closed streamed items as the output when the terminal output array is empty, keeping the existing strictness for every other mismatch (an item without response.output_item.done, a gap in the item indexes, or a terminal array that does not match the streamed items still raises). The reproduction above passes with that change, and the function_call items remain available to the client.

Environment: OpenViking 0.5.3, Python 3.13.15, macOS 27.2 (arm64), gateway embedded in the OpenViking server on 127.0.0.1:1933/gateway, upstream is an OpenAI Responses-compatible endpoint (ChatGPT Codex backend) reached through a local proxy that relays the upstream stream unchanged.

Terryli246 avatar Nov 03 '26 23:11 Terryli246