8000 A stream that ends before its terminal event is reported as a normal stop (Codex Responses and Chat Completions) · Issue #818 · huggingface/tau · GitHub
[go: up one dir, main page]

Skip to content

A stream that ends before its terminal event is reported as a normal stop (Codex Responses and Chat Completions) #818

Backlog

Description

@MainSequenceDev

Summary

When the body of a streamed response ends before the protocol's terminal event, OpenAICodexProvider and OpenAICompatibleProvider finish with AssistantDoneEvent(reason="stop"). The message holds whatever text arrived and zero usage, and no error is raised.

canonicalize_provider_stream already turns a stream without a terminal event into AssistantErrorEvent ("Provider stream ended without a terminal event"). That check never applies here, because the parsers always emit ProviderResponseEndEvent once the body ends.

Versions

tau-ai 0.4.2 (httpx 0.28.1) and 0.4.12 (httpx 0.29.0), Python 3.13.

Reproduction

An httpx.MockTransport serves a 200 text/event-stream body, and the last event stream_response yields is printed:

  • Codex Responses: response.created, then one response.output_text.delta, with no response.completed.
  • Chat Completions: one chunk with delta.content, with no chunk carrying a finish_reason and no data: [DONE].

Each case is also run once with the terminal event in place, as a control.

import asyncio, json, httpx
from tau_agent.messages import UserMessage
from tau_ai.env import OpenAICompatibleConfig
from tau_ai.openai_codex import OpenAICodexConfig, OpenAICodexCredentials, OpenAICodexProvider
from tau_ai.openai_compatible import OpenAICompatibleProvider

def sse(*events):
    return "".join(f"data: {json.dumps(e)}\n\n" for e in events).encode()

def client(body):
    return httpx.AsyncClient(transport=httpx.MockTransport(lambda request: httpx.Response(
        200, headers={"content-type": "text/event-stream"}, content=body)))

async def credentials():
    return OpenAICodexCredentials(access_token="test-token", account_id="test-account")

async def last(provider):
    events = [e async for e in provider.stream_response(
        model="test-model", system="", messages=[UserMessage(content="hi")], tools=[])]
    m = getattr(events[-1], "message", None) or events[-1].error
    return type(events[-1]).__name__, events[-1].reason, m.text, m.usage.input, m.usage.output

async def main():
    codex = sse({"type": "response.created", "response": {"id": "r1", "status": "in_progress"}},
                {"type": "response.output_text.delta", "item_id": "m1", "output_index": 0,
                 "content_index": 0, "delta": "Hel"})
    print(await last(OpenAICodexProvider(
        OpenAICodexConfig(credential_resolver=credentials, max_retries=0), client=client(codex))))
    chat = sse({"id": "c1", "choices": [{"index": 0, "delta": {"role": "assistant", "content": "Hel"}}]})
    print(await last(OpenAICompatibleProvider(
        OpenAICompatibleConfig(api_key="test-key", base_url="http://chat.test/v1", max_retries=0),
        client=client(chat))))

asyncio.run(main())

Result

The same on 0.4.2 and 0.4.12 (the usage figures are the test bodies' own):

Body Last event Text Usage in / out Error
Codex, with response.completed AssistantDoneEvent, stop Hel 5 / 2 none
Codex, without response.completed AssistantDoneEvent, stop Hel 0 / 0 none
Chat, with finish_reason and [DONE] AssistantDoneEvent, stop Hel 5 / 2 none
Chat, without either AssistantDoneEvent, stop Hel 0 / 0 none

Where it happens

  • openai_codex._codex_provider_events yields ProviderResponseEndEvent(finish_reason=None) after its SSE loop, whether or not it saw response.completed, response.done or response.incomplete.
  • In openai_compatible, the stream parsers' finalize() always appends a ProviderResponseEndEvent.
  • stream._finish_reason(None) returns "stop".

The other providers were not checked.

Expected

A body that ends before its terminal event should end in AssistantErrorEvent, as canonicalize_provider_stream already does for a stream with no end event. The terminal event is response.completed, response.incomplete or response.done for Responses, and a finish_reason or [DONE] for Chat Completions. Ideally the error would also say what did arrive, such as the number of events parsed, so that a truncated stream can be told apart from an empty answer.

How this showed up

A proxy between the client and the provider truncated streams to their first chunk. The answers were recorded as successful turns with partial or no text and zero usage, with no error at the model-call level.

Activity

  1. four1er commented on Nov 27, 2026

    @four1er

    Hi, I am reproducing this with the MockTransport cases from the report and preparing a fix so a stream that ends before its terminal event surfaces as an error instead of a normal stop. Will open a PR shortly.

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

      0