Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions chatmock/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,23 +432,46 @@ def _extract_usage(evt: Dict[str, Any]) -> Dict[str, int] | None:
except Exception:
return None
try:
for raw in upstream.iter_lines(decode_unicode=False):
if not raw:
continue
line = raw.decode("utf-8", errors="ignore") if isinstance(raw, (bytes, bytearray)) else raw
try:
line_iterator = upstream.iter_lines(decode_unicode=False)
except requests.exceptions.ChunkedEncodingError as e:
if verbose and vlog:
vlog(line)
if not line.startswith("data: "):
continue
data = line[len("data: "):].strip()
if not data:
continue
if data == "[DONE]":
break
vlog(f"Failed to start stream: {e}")
yield b"data: [DONE]\n\n"
return

for raw in line_iterator:
try:
evt = json.loads(data)
except Exception:
continue
if not raw:
continue
line = (
raw.decode("utf-8", errors="ignore")
if isinstance(raw, (bytes, bytearray))
else raw
)
if verbose and vlog:
vlog(line)
if not line.startswith("data: "):
continue
data = line[len("data: ") :].strip()
if not data:
continue
if data == "[DONE]":
break
try:
evt = json.loads(data)
except (json.JSONDecodeError, UnicodeDecodeError):
continue
except (
requests.exceptions.ChunkedEncodingError,
ConnectionError,
BrokenPipeError,
) as e:
# Connection interrupted mid-stream - end gracefully
if verbose and vlog:
vlog(f"Stream interrupted: {e}")
yield b"data: [DONE]\n\n"
return
kind = evt.get("type")
if isinstance(evt.get("response"), dict) and isinstance(evt["response"].get("id"), str):
response_id = evt["response"].get("id") or response_id
Expand Down