From 1b83bd4062e22c74aee4261dc35893e02b26edbd Mon Sep 17 00:00:00 2001 From: printlndarling Date: Sat, 22 Nov 2025 22:49:27 +0800 Subject: [PATCH] Fix handle empty stream chunks and enforce UTF-8 cache writes --- sgpt/cache.py | 2 +- sgpt/handlers/handler.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/sgpt/cache.py b/sgpt/cache.py index 5d63db8f..c176fe24 100644 --- a/sgpt/cache.py +++ b/sgpt/cache.py @@ -38,7 +38,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Generator[str, None, None]: result += i yield i if "@FunctionCall" not in result: - file.write_text(result) + file.write_text(result, encoding="utf-8") self._delete_oldest_files(self.length) # type: ignore return wrapper diff --git a/sgpt/handlers/handler.py b/sgpt/handlers/handler.py index a17d8024..2f4df95c 100644 --- a/sgpt/handlers/handler.py +++ b/sgpt/handlers/handler.py @@ -114,6 +114,8 @@ def get_completion( try: for chunk in response: + if not chunk.choices: + continue delta = chunk.choices[0].delta # LiteLLM uses dict instead of Pydantic object like OpenAI does.