Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion floatchat/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ def chat_stream(prompt: str, system: str | None = None, temperature: float = 0.2
return
try:
resp = _post(_payload(prompt, system, temperature, max_tokens, json_mode=False, stream=True), stream=True)
for line in resp.iter_lines(decode_unicode=True):
# Read bytes and decode as UTF-8: event streams often have no charset, and requests would then fall back to
# ISO-8859-1 and garble characters such as "µ" or narrow spaces
for raw in resp.iter_lines():
line = raw.decode("utf-8", errors="replace") if isinstance(raw, bytes) else raw
if not line or not line.startswith("data: "):
continue
data = line[len("data: "):]
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def test_stream_yields_tokens(monkeypatch):
assert sent[0]["stream"] is True


def test_stream_decodes_utf8_bytes(monkeypatch):
# Event streams usually carry no charset; the bytes must still be read as UTF-8
text = "O₂ minimum 0.8 µmol/kg"
line = ("data: " + json.dumps({"choices": [{"delta": {"content": text}}]}, ensure_ascii=False)).encode("utf-8")
install(monkeypatch, [FakeResponse(lines=[line, b"data: [DONE]"])])
assert "".join(llm.chat_stream("q")) == text


def test_stream_yields_nothing_without_an_llm(monkeypatch):
monkeypatch.setattr(config, "LLM_BASE_URL", "")
assert list(llm.chat_stream("q")) == []
Loading