ChatCompletionStream.get_final_completion() can return an incomplete result after an early EOF
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 53/100
Hướng nghiên cứu
Read src/openai/_streaming.py and src/openai/lib/streaming/chat/_completions.py, then trace how ChatCompletionStreamState.get_final_completion() handles a snapshot without a terminal finish_reason. Start by adapting the proposed offline reproduction and the repository’s streaming test harness for both sync and async paths. Done means early EOF cannot yield a successful final completion, while normal terminal chunks still work.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This report is about the SDK's handling of a synthetic, prematurely terminated stream, not about why a live API connection might close.
- Checked the current default-branch implementation and searched for an equivalent Chat Completions streaming report.
- Reproduced against a fully installed SDK — not run yet (source-traced behavior below).
Summary
In the synchronous and asynchronous Chat Completions streaming helpers, a normally ending underlying iterator is treated as completion even when no chunk supplied a finish_reason. get_final_completion() then parses the accumulated snapshot and can return partially generated content or tool-call arguments as a successful final completion.
This is different from response.completed-based Responses API streaming: this report concerns client.chat.completions.stream(...).get_final_completion().
Minimal regression test to confirm (offline; no API key)
This isolates the parsing state after a last chunk with finish_reason=None. It is proposed reproduction code, not a test claimed to have passed:
from openai._types import omit
from openai.types.chat import ChatCompletionChunk
from openai.lib.streaming.chat._completions import ChatCompletionStreamState
state = ChatCompletionStreamState(response_format=omit, input_tools=omit)
chunk = ChatCompletionChunk.model_validate({
"id": "chatcmpl_test",
"object": "chat.completion.chunk",
"created": 0,
"model": "gpt-4o-mini",
"choices": [{
"index": 0,
"delta": {"role": "assistant", "content": "incomplete answer"},
"finish_reason": None,
}],
})
list(state.handle_chunk(chunk))
result = state.get_final_completion()
print(result.choices[0].message.content, result.choices[0].finish_reason)
# Source path suggests: "incomplete answer", None, without raising.
For the public .stream() path, feed that one chunk via a scripted/mock Stream[ChatCompletionChunk] that then ends cleanly, without a terminal choice chunk and [DONE]. Verify that get_final_completion() either raises an incomplete-stream exception or explicitly exposes its incompleteness; the current code does neither.
Why this happens
src/openai/_streaming.py(~lines 62–117 / 177–232): the baseStream/AsyncStreamloops until[DONE]or iterator exhaustion, without recording which condition ended the loop.src/openai/lib/streaming/chat/_completions.py(~lines 93–119 and 216–242):get_final_completion()consumes that iterator withuntil_done(), then unconditionally callsChatCompletionStreamState.get_final_completion().- The state helper (~lines 326–336) calls
parse_chat_completionon the current snapshot and explicitly notes that the helper itself does not guarantee the stream finished. src/openai/lib/_parsing/_completions.py(~lines 96–166) only checks forfinish_reason == 'length'and'content_filter'; a missing / null finish reason is not rejected.
A clean early EOF can therefore turn a partially received answer into an apparently successful result. This is particularly risky for agent callers that consume tool-call JSON or treat get_final_completion() as an integrity boundary.
Expected
- The public Chat Completion streaming helper should not report a successful final completion when a requested choice has not delivered a terminal
finish_reason(or another documented end-of-message indicator). - Keep successful parsing for streams that finish normally, including valid truncated-by-token-limit terminal reasons, which already have dedicated error semantics for parsed outputs.
- Preserve behavior when users intentionally stop iteration early and inspect
current_completion_snapshotrather than request a final result. - Consider capturing whether the underlying stream saw
[DONE], to distinguish a clean EOF from an explicit protocol terminator where appropriate; not every endpoint using the shared baseStreamnecessarily follows the same convention.
Regression test suggestions
- Sync/async: a chunk with
finish_reason=None, then transport EOF -> no successful final completion. - Sync/async: normal terminal
finish_reason='stop'(and standard stream terminator) -> final completion works. - Partial function-call arguments before EOF -> must not silently be accepted as a completed tool call.
Related but not a duplicate
- #2486 / #3263 concern Responses API structured-output parsing/incomplete responses. This is specific to Chat Completions
get_final_completion()and its missing terminal choice guard.
I can follow up with a complete integration test based on the repository's streaming test harness if maintainers agree this behavior should be considered a bug.
- Ngôn ngữ chính
- Python
- Star
- 32.2k
- Fork
- 5.8k
- Merge trung bình
- 12 giờ 29 phút
- Pull request đã merge (30 ngày)
- 112
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của openai/openai-python
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
openai/openai-python#4338 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
openai/openai-python#4337 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersCó thể đã có người làm @Rewaskc02-lang đã nhận 1 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
openai/openai-python#4311 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
openai/openai-python#4310 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
openai/openai-python#4309 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của openai/openai-python
Issue tương tự
-
triage
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
kornia/kornia-rs#1416 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
ui
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
PolicyEngine/ai-inequality#64 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over timeCó thể đã có người làm @PabloRoque đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
pymc-devs/pymc-extras#839 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày