#3388·deepeval

OpenAI stream=True calls finish their LLM span before stream consumption

Author: dafyy321-pixelCreated Sep 28, 2026Updated Sep 28, 2026

OpenAI stream=True calls finish their LLM span before the stream is consumed

DeepEval patches the OpenAI Chat Completions and Responses create() methods so an LLM call can contribute input, output, usage, and timing to a trace. That works for an ordinary completed response. With stream=True, create() returns a Stream or AsyncStream object; the answer arrives later as the caller iterates over it. The current wrapper treats the returned stream object as if it were the completed answer.

As a result, the LLM span is closed as soon as create() returns. At that point the streamed text, tool-call deltas, and final usage information have not arrived. The span can contain the request but miss the response and token counts, and its duration covers stream setup rather than the period in which the answer was produced and consumed.

Reproduction

Enable DeepEval's OpenAI instrumentation (importing deepeval.openai activates the patch), then make a normal streamed Chat Completions call and consume it:

python
import deepeval.openai
from openai import OpenAI

client = OpenAI()
with client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Say hello."}],
    stream=True,
    stream_options={"include_usage": True},
) as stream:
    for chunk in stream:
        print(chunk)

Inspect the LLM span after the loop. The output is absent, and the final usage chunk cannot populate the span because the wrapper already finished it. The same ordering applies to AsyncOpenAI and to responses.create(..., stream=True). A test can reproduce the call sequence without an API key by returning a fake iterable or async iterable of SDK-shaped chunks from the patched method.

Why this happens

Both patched create() paths call the original method, immediately run safe_extract_output_parameters() on the returned value, and return from an @observe-decorated function (sync path, async path). The output extractor expects a completed response with fields such as choices[0].message, output_text, and usage; when extraction fails on a stream object it returns empty OutputParameters (extractors.py). The decorator then closes the span when that function returns, before any chunk has been read.

Expected behavior

For a regular create(stream=True) call, each chunk should reach the caller unchanged and in order. The LLM span should remain open while the stream is consumed and finish on exhaustion, explicit close, context-manager exit, or iteration error. It should record the available text and tool calls; usage should be recorded when the provider supplies it, such as the final Chat Completions usage chunk requested with stream_options={"include_usage": True} or the completed Responses event.

The usual non-streaming path should keep its current behavior. This report concerns the SDK's regular Stream and AsyncStream returned by create(stream=True); the separate raw with_streaming_response API has a different response lifecycle.