diff --git a/src/services/kiaai/chat.py b/src/services/kiaai/chat.py index 8442803..b83e067 100644 --- a/src/services/kiaai/chat.py +++ b/src/services/kiaai/chat.py @@ -1,4 +1,5 @@ import json +import codecs import asyncio from typing import AsyncGenerator import httpx @@ -145,9 +146,12 @@ class KiaAIService: line_count = 0 buf = "" done = False + # Incremental decoder holds partial multi-byte chars (Persian = 2 bytes each) + # until the remaining bytes arrive — prevents '?' corruption with chunk_size=1 + _decoder = codecs.getincrementaldecoder("utf-8")(errors="replace") async for raw in resp.aiter_bytes(chunk_size=1): - buf += raw.decode("utf-8", errors="replace") + buf += _decoder.decode(raw) while "\n" in buf: line, buf = buf.split("\n", 1)