Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(sdk): reject incomplete Python generation streams

svonava8ff968c71b62
diff --git a/packages/sie_sdk/src/sie_sdk/client/_sse.py b/packages/sie_sdk/src/sie_sdk/client/_sse.py
index 9d318e2b4..cc7a51b6a 100644
--- a/packages/sie_sdk/src/sie_sdk/client/_sse.py
+++ b/packages/sie_sdk/src/sie_sdk/client/_sse.py
@@ -15,7 +15,9 @@ multi-line ``data:`` continuations are not produced and are not handled here.
from __future__ import annotations
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Any
+
+from .errors import SIEConnectionError
if TYPE_CHECKING:
from collections.abc import AsyncIterable, AsyncIterator, Iterable, Iterator
@@ -23,6 +25,52 @@ if TYPE_CHECKING:
_SSE_DONE = "[DONE]"
+class SSECompletion:
+ """Distinguish transport EOF from a completed generation.
+
+ A gateway ``[DONE]`` marker ends either surface. Without that marker,
+ native generation must have emitted ``done: true``; chat must have
+ finished every requested choice. Usage-only chat events do not complete
+ a stream and do not erase an earlier finish event.
+ """
+
+ def __init__(self, *, chat_choices: int | None = None) -> None:
+ self.done = False
+ self._native_terminal = False
+ self._chat_choices = chat_choices
+ if chat_choices is not None and (type(chat_choices) is not int or chat_choices <= 0):
+ # The gateway still validates the unchanged request. A malformed
+ # option must not make an empty successful body look complete.
+ self._chat_choices = 1
+ self._finished_choices: set[int] = set()
+
+ def observe(self, chunk: Any) -> None:
+ if not isinstance(chunk, dict):
+ return
+ if self._chat_choices is None:
+ if chunk.get("done") is True:
+ self._native_terminal = True
+ return
+ choices = chunk.get("choices")
+ if not isinstance(choices, list):
+ return
+ for choice in choices:
+ if not isinstance(choice, dict):
+ continue
+ index = choice.get("index")
+ finish = choice.get("finish_reason")
+ if type(index) is int and 0 <= index < self._chat_choices and isinstance(finish, str) and finish:
+ self._finished_choices.add(index)
+
+ def require_complete(self) -> None:
+ if self.done or self._native_terminal:
+ return
+ if self._chat_choices is not None and len(self._finished_choices) == self._chat_choices:
+ return
+ msg = "Connection lost during stream: response ended before generation completed"
+ raise SIEConnectionError(msg)
+
+
def _extract_data_payload(line: str) -> str | None:
"""Return the ``data:`` payload of one SSE line, or ``None`` to skip.
@@ -39,10 +87,12 @@ def _extract_data_payload(line: str) -> str | None:
return value
-def iter_sse_payloads(lines: Iterable[str]) -> Iterator[str]:
+def iter_sse_payloads(lines: Iterable[str], *, completion: SSECompletion | None = None) -> Iterator[str]:
"""Yield ``data:`` payloads from an iterable of SSE lines.
Stops (without yielding) on the ``[DONE]`` sentinel or a clean EOF.
+ When supplied, ``completion`` records the sentinel so the caller can
+ validate natural EOF after inspecting the semantic terminal chunks.
The caller owns the underlying stream; closing it cancels the upstream
request (the gateway forwards the disconnect so the worker stops
generating).
@@ -52,16 +102,22 @@ def iter_sse_payloads(lines: Iterable[str]) -> Iterator[str]:
if payload is None:
continue
if payload == _SSE_DONE:
+ if completion is not None:
+ completion.done = True
return
yield payload
-async def aiter_sse_payloads(lines: AsyncIterable[str]) -> AsyncIterator[str]:
+async def aiter_sse_payloads(
+ lines: AsyncIterable[str], *, completion: SSECompletion | None = None
+) -> AsyncIterator[str]:
"""Async counterpart to :func:`iter_sse_payloads`."""
async for line in lines:
payload = _extract_data_payload(line)
if payload is None:
continue
if payload == _SSE_DONE:
+ if completion is not None:
+ completion.done = True
return
yield payload
diff --git a/packages/sie_sdk/src/sie_sdk/client/async_.py b/packages/sie_sdk/src/sie_sdk/client/async_.py
index 7a3e4d866..d74568cd7 100644
--- a/packages/sie_sdk/src/sie_sdk/client/async_.py
+++ b/packages/sie_sdk/src/sie_sdk/client/async_.py
@@ -175,7 +175,7 @@ from ._shared import (
validate_generate_request_body,
websocket_matches_base_url_origin,
)
-from ._sse import aiter_sse_payloads
+from ._sse import SSECompletion, aiter_sse_payloads
from .errors import (
JobFailedError,
LoraLoadingError,
@@ -2961,6 +2961,8 @@ class SIEAsyncClient:
an HTTP 503 or as the first SSE event. Once a chunk has been yielded a
failure is terminal (non-idempotent). The ``async with`` keeps the
connection open while the caller consumes the generator.
+ A natural EOF requires an explicit or semantic completion marker;
+ an interrupted generation is never retried.
"""
self._reset_retry_count()
timeout = provision_timeout_s if provision_timeout_s is not None else DEFAULT_PROVISION_TIMEOUT_S
@@ -3018,7 +3020,13 @@ class SIEAsyncClient:
else:
self._check_server_version(_AioResponse(raw.status, b"", raw.headers))
yielded_chunk = False
- async for payload in aiter_sse_payloads(_aiter_text_lines(raw.content)):
+ chat_choices = None
+ if url == "/v1/chat/completions":
+ chat_choices = json.loads(body).get("n")
+ if chat_choices is None:
+ chat_choices = 1
+ completion = SSECompletion(chat_choices=chat_choices)
+ async for payload in aiter_sse_payloads(_aiter_text_lines(raw.content), completion=completion):
try:
chunk = json.loads(payload)
except json.JSONDecodeError as e:
@@ -3074,9 +3082,11 @@ class SIEAsyncClient:
request=parse_request_metadata(error_headers),
retry_after=float(retry_after_s) if retry_after_s is not None else None,
)
+ completion.observe(chunk)
yield chunk
yielded_chunk = True
else:
+ completion.require_complete()
return
except _CONNECT_PHASE_ERRORS as e:
if wait_for_capacity and is_transient_connect_error(e):
diff --git a/packages/sie_sdk/src/sie_sdk/client/sync.py b/packages/sie_sdk/src/sie_sdk/client/sync.py
index 3a2448f00..3c80b2215 100644
--- a/packages/sie_sdk/src/sie_sdk/client/sync.py
+++ b/packages/sie_sdk/src/sie_sdk/client/sync.py
@@ -184,7 +184,7 @@ from ._shared import (
validate_generate_request_body,
websocket_matches_base_url_origin,
)
-from ._sse import iter_sse_payloads
+from ._sse import SSECompletion, iter_sse_payloads
from .errors import (
JobFailedError,
LoraLoadingError,
@@ -3200,7 +3200,8 @@ class SIEClient:
Yields :class:`ChatCompletionChunk` events. Pass
``stream_options={"include_usage": True}`` to receive a final
usage-only chunk (``choices: []``) before the stream ends. Raises
- :class:`ServerError` if the gateway emits a mid-stream error chunk;
+ :class:`ServerError` if the gateway emits a mid-stream error chunk, or
+ :class:`SIEConnectionError` if the response ends before completion;
breaking out of the iterator early closes the stream so the worker
stops generating.
@@ -3365,6 +3366,8 @@ class SIEClient:
failure is terminal (non-idempotent). Yielding inside the ``with``
keeps the stream open while the caller consumes it; an early ``break``
tears the context down and the worker sees the disconnect.
+ A natural EOF requires an explicit or semantic completion marker;
+ an interrupted generation is never retried.
"""
self._reset_retry_count()
timeout = provision_timeout_s if provision_timeout_s is not None else DEFAULT_PROVISION_TIMEOUT_S
@@ -3401,7 +3404,13 @@ class SIEClient:
else:
self._check_server_version(response)
yielded_chunk = False
- for payload in iter_sse_payloads(response.iter_lines()):
+ chat_choices = None
+ if url == "/v1/chat/completions":
+ chat_choices = json.loads(body).get("n")
+ if chat_choices is None:
+ chat_choices = 1
+ completion = SSECompletion(chat_choices=chat_choices)
+ for payload in iter_sse_payloads(response.iter_lines(), completion=completion):
try:
chunk = json.loads(payload)
except json.JSONDecodeError as e:
@@ -3456,9 +3465,11 @@ class SIEClient:
request=parse_request_metadata(error_headers),
retry_after=float(retry_after_s) if retry_after_s is not None else None,
)
+ completion.observe(chunk)
yield chunk
yielded_chunk = True
else:
+ completion.require_complete()
return
except _CONNECT_PHASE_ERRORS as e:
if wait_for_capacity and is_transient_connect_error(e):
diff --git a/packages/sie_sdk/tests/client/test_stream_completion.py b/packages/sie_sdk/tests/client/test_stream_completion.py
new file mode 100644
index 000000000..a9f323f35
--- /dev/null
+++ b/packages/sie_sdk/tests/client/test_stream_completion.py
@@ -0,0 +1,363 @@
+"""Generation EOF must not silently turn a partial response into success."""
+
+from __future__ import annotations
+
+import asyncio
+import json
+from collections.abc import Iterator
+from typing import Any
+from unittest.mock import AsyncMock, MagicMock
+
+import aiohttp
+import httpx
+import pytest
+from sie_sdk import SIEAsyncClient, SIEClient
+from sie_sdk.client.errors import ServerError, SIEConnectionError
+
+_BASE_URL = "http://synthetic.invalid"
+
+
+def _frame(payload: dict[str, Any] | str) -> bytes:
+ value = payload if isinstance(payload, str) else json.dumps(payload)
+ return f"data: {value}\n\n".encode()
+
+
+def _delta(surface: str, *, index: int = 0) -> dict[str, Any]:
+ if surface == "native":
+ return {"request_id": "req-test", "seq": 0, "text_delta": "partial", "done": False}
+ return {
+ "id": "chat-test",
+ "object": "chat.completion.chunk",
+ "created": 0,
+ "model": "m",
+ "choices": [{"index": index, "delta": {"content": "partial"}, "finish_reason": None}],
+ }
+
+
+def _terminal(surface: str, *, index: int = 0) -> dict[str, Any]:
+ if surface == "native":
+ return {
+ "request_id": "req-test",
+ "seq": 1,
+ "text_delta": "",
+ "done": True,
+ "finish_reason": "stop",
+ "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
+ }
+ chunk = _delta(surface, index=index)
+ chunk["choices"][0].update(delta={}, finish_reason="stop")
+ return chunk
+
+
+class _SyncBody(httpx.SyncByteStream):
+ def __init__(self, frames: list[bytes], *, fail: bool = False) -> None:
+ self.frames = frames
+ self.fail = fail
+ self.closed = False
+
+ def __iter__(self) -> Iterator[bytes]:
+ yield from self.frames
+ if self.fail:
+ raise httpx.ReadError("controlled read failure")
+
+ def close(self) -> None:
+ self.closed = True
+
+
+class _AsyncBody:
+ def __init__(self, frames: list[bytes], *, fail: bool = False, block: bool = False) -> None:
+ self.frames = frames
+ self.fail = fail
+ self.block = block
+ self.closed = False
+ self.status = 200
+ self.headers = {"content-type": "text/event-stream"}
+ self.waiting = asyncio.Event()
+ self.content = self._bytes()
+
+ async def _bytes(self):
+ for frame in self.frames:
+ yield frame
+ if self.fail:
+ raise aiohttp.ServerDisconnectedError("controlled read failure")
+ if self.block:
+ self.waiting.set()
+ await asyncio.Event().wait()
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, *exc: object) -> bool:
+ self.closed = True
+ return False
+
+
+class _Harness:
+ def __init__(self, mode: str, frames: list[bytes], *, fail: bool = False, block: bool = False) -> None:
+ self.mode = mode
+ self.calls = 0
+ if mode == "sync":
+ self.body = _SyncBody(frames, fail=fail)
+ self.client = SIEClient(
+ _BASE_URL,
+ http_client=httpx.Client(base_url=_BASE_URL, transport=httpx.MockTransport(self._dispatch)),
+ )
+ else:
+ self.body = _AsyncBody(frames, fail=fail, block=block)
+ self.client = SIEAsyncClient(_BASE_URL)
+ session = MagicMock()
+ session.close = AsyncMock()
+ session.post.side_effect = self._dispatch
+ self.client._session = session
+
+ def _dispatch(self, *args, **kwargs):
+ self.calls += 1
+ assert self.calls == 1, "an interrupted generation must not be replayed"
+ if self.mode == "sync":
+ return httpx.Response(200, headers={"content-type": "text/event-stream"}, stream=self.body)
+ return self.body
+
+ def stream(self, surface: str, **kwargs):
+ if surface == "native":
+ return self.client.stream_generate("m", "test", max_new_tokens=4, **kwargs)
+ return self.client.stream_chat_completions("m", [{"role": "user", "content": "test"}], **kwargs)
+
+ async def collect(self, surface: str, output: list[Any], **kwargs) -> None:
+ if self.mode == "sync":
+ output.extend(self.stream(surface, **kwargs))
+ else:
+ async for chunk in self.stream(surface, **kwargs):
+ output.append(chunk)
+
+ async def close(self) -> None:
+ if self.mode == "sync":
+ self.client.close()
+ else:
+ await self.client.close()
+
+
[email protected]("mode", ["sync", "async"])
[email protected]("surface", ["native", "chat"])
[email protected]("kind", ["empty", "comments", "partial"])
+async def test_premature_clean_eof_raises_without_replaying(mode: str, surface: str, kind: str) -> None:
+ frames = {"empty": [], "comments": [b": keepalive\n\n"], "partial": [_frame(_delta(surface))]}[kind]
+ harness = _Harness(mode, frames)
+ output: list[Any] = []
+ try:
+ with pytest.raises(SIEConnectionError, match="before generation completed"):
+ await harness.collect(surface, output)
+ assert output == ([_delta(surface)] if kind == "partial" else [])
+ assert harness.calls == 1
+ assert harness.client.last_retry_count == 0
+ assert harness.body.closed
+ finally:
+ await harness.close()
+
+
[email protected]("mode", ["sync", "async"])
[email protected]("surface", ["native", "chat"])
[email protected]("kind", ["done_only", "delta_done", "terminal_eof", "terminal_done", "terminal_tail"])
+async def test_explicit_completion_remains_accepted(mode: str, surface: str, kind: str) -> None:
+ delta, terminal = _delta(surface), _terminal(surface)
+ frames = {
+ "done_only": [_frame("[DONE]")],
+ "delta_done": [_frame(delta), _frame("[DONE]")],
+ "terminal_eof": [_frame(delta), _frame(terminal)],
+ "terminal_done": [_frame(delta), _frame(terminal), _frame("[DONE]")],
+ "terminal_tail": [_frame(delta), _frame(terminal).rstrip(b"\n")],
+ }[kind]
+ harness = _Harness(mode, frames)
+ output: list[Any] = []
+ try:
+ await harness.collect(surface, output)
+ expected = [] if kind == "done_only" else [delta]
+ if kind.startswith("terminal"):
+ expected.append(terminal)
+ assert output == expected
+ assert harness.calls == 1
+ assert harness.body.closed
+ finally:
+ await harness.close()
+
+
[email protected]("mode", ["sync", "async"])
[email protected]("surface", ["native", "chat"])
[email protected]("before_error", ["empty", "partial", "terminal"])
+async def test_error_metadata_survives_eof_guard(mode: str, surface: str, before_error: str) -> None:
+ error = {"request_id": "req-failure", "error": {"code": "transport_failure", "message": "controlled failure"}}
+ prefix = {"empty": [], "partial": [_frame(_delta(surface))], "terminal": [_frame(_terminal(surface))]}
+ frames = prefix[before_error] + [_frame(error)]
+ harness = _Harness(mode, frames)
+ try:
+ with pytest.raises(ServerError, match="controlled failure") as caught:
+ await harness.collect(surface, [])
+ assert caught.value.code == "transport_failure"
+ assert caught.value.request == {"id": "req-failure"}
+ assert harness.calls == 1
+ assert harness.client.last_retry_count == 0
+ assert harness.body.closed
+ finally:
+ await harness.close()
+
+
[email protected]("mode", ["sync", "async"])
[email protected]("surface", ["native", "chat"])
+async def test_actual_read_error_is_not_reclassified_or_retried(mode: str, surface: str) -> None:
+ harness = _Harness(mode, [_frame(_delta(surface))], fail=True)
+ try:
+ with pytest.raises(SIEConnectionError, match="controlled read failure"):
+ await harness.collect(surface, [])
+ assert harness.calls == 1
+ assert harness.body.closed
+ finally:
+ await harness.close()
+
+
[email protected]("mode", ["sync", "async"])
[email protected]("surface", ["native", "chat"])
+async def test_caller_close_is_not_a_premature_eof(mode: str, surface: str) -> None:
+ harness = _Harness(mode, [_frame(_delta(surface)), _frame(_terminal(surface))])
+ iterator = harness.stream(surface)
+ try:
+ if mode == "sync":
+ assert next(iterator) == _delta(surface)
+ iterator.close()
+ else:
+ assert await anext(iterator) == _delta(surface)
+ await iterator.aclose()
+ # The public async wrapper's inner generator finalizes on the loop.
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+ assert harness.calls == 1
+ assert harness.body.closed
+ finally:
+ await harness.close()
+
+
[email protected]("surface", ["native", "chat"])
+async def test_caller_task_cancellation_is_not_a_premature_eof(surface: str) -> None:
+ harness = _Harness("async", [_frame(_delta(surface))], block=True)
+ iterator = harness.stream(surface)
+ pending = None
+ try:
+ assert await anext(iterator) == _delta(surface)
+ pending = asyncio.create_task(anext(iterator))
+ await asyncio.wait_for(harness.body.waiting.wait(), timeout=1)
+ pending.cancel()
+ with pytest.raises(asyncio.CancelledError):
+ await pending
+ assert harness.calls == 1
+ assert harness.body.closed
+ finally:
+ if pending is not None and not pending.done():
+ pending.cancel()
+ await iterator.aclose()
+ await harness.close()
+
+
[email protected]("mode", ["sync", "async"])
[email protected]("override", [False, True])
[email protected]("complete", [False, True])
+async def test_chat_eof_requires_every_requested_choice(mode: str, override: bool, complete: bool) -> None:
+ chunks = [_delta("chat", index=0), _terminal("chat", index=0)]
+ if complete:
+ chunks += [_terminal("chat", index=1), {"choices": [], "usage": {"total_tokens": 4}}]
+ harness = _Harness(mode, [_frame(chunk) for chunk in chunks])
+ output: list[Any] = []
+ kwargs = {"extra_body": {"n": 2}} if override else {"n": 2}
+ try:
+ if complete:
+ await harness.collect("chat", output, **kwargs)
+ else:
+ with pytest.raises(SIEConnectionError, match="before generation completed"):
+ await harness.collect("chat", output, **kwargs)
+ assert output == chunks
+ assert harness.calls == 1
+ assert harness.body.closed
+ finally:
+ await harness.close()
+
+
[email protected]("mode", ["sync", "async"])
[email protected]("native_marker", [False, True])
+async def test_chat_null_n_means_one_chat_choice(mode: str, native_marker: bool) -> None:
+ chunk = {"done": True} if native_marker else _terminal("chat")
+ harness = _Harness(mode, [_frame(chunk)])
+ output: list[Any] = []
+ try:
+ if native_marker:
+ with pytest.raises(SIEConnectionError, match="before generation completed"):
+ await harness.collect("chat", output, extra_body={"n": None})
+ else:
+ await harness.collect("chat", output, extra_body={"n": None})
+ assert output == [chunk]
+ assert harness.calls == 1
+ assert harness.body.closed
+ finally:
+ await harness.close()
+
+
[email protected]("mode", ["sync", "async"])
[email protected]("n", [0, False, -1, "2", [], {}, 1.5, True])
[email protected]("complete", [False, True])
+async def test_malformed_n_cannot_accept_empty_success_or_break_completion(mode: str, n: Any, complete: bool) -> None:
+ chunks = [_terminal("chat")] if complete else []
+ harness = _Harness(mode, [_frame(chunk) for chunk in chunks])
+ try:
+ if complete:
+ await harness.collect("chat", [], extra_body={"n": n})
+ else:
+ with pytest.raises(SIEConnectionError, match="before generation completed"):
+ await harness.collect("chat", [], extra_body={"n": n})
+ assert harness.calls == 1
+ assert harness.body.closed
+ finally:
+ await harness.close()
+
+
[email protected]("mode", ["sync", "async"])
[email protected]("indices", [[0, 0], [0, 2], [0, -1], [0, True], [0, 1.0]])
+async def test_duplicate_or_invalid_choice_indices_do_not_complete_n_two(mode: str, indices: list[Any]) -> None:
+ harness = _Harness(mode, [_frame(_terminal("chat", index=index)) for index in indices])
+ try:
+ with pytest.raises(SIEConnectionError, match="before generation completed"):
+ await harness.collect("chat", [], n=2)
+ assert harness.calls == 1
+ assert harness.body.closed
+ finally:
+ await harness.close()
+
+
[email protected]("mode", ["sync", "async"])
+ "chunk",
+ [
+ {"done": "true"},
+ {"done": 1},
+ {"choices": []},
+ {"choices": [{"index": True, "finish_reason": "stop"}]},
+ {"choices": [{"index": 1, "finish_reason": "stop"}]},
+ {"choices": [{"index": 0, "finish_reason": ""}]},
+ {"choices": [{"index": 0, "finish_reason": 1}]},
+ ],
+)
+async def test_malformed_completion_does_not_mask_eof(mode: str, chunk: dict[str, Any]) -> None:
+ surface = "native" if "done" in chunk else "chat"
+ harness = _Harness(mode, [_frame(chunk)])
+ try:
+ with pytest.raises(SIEConnectionError, match="before generation completed"):
+ await harness.collect(surface, [])
+ assert harness.calls == 1
+ finally:
+ await harness.close()