fix: avoid JSONDecodeError for empty line stream response #393
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
r.iter_lines()may yield empty string, which results in JSONDecodeError and terminates the stream response.the stackstrace is as follows:
File "/root/.pyenv/versions/3.10.16/lib/python3.10/concurrent/futures/_base.py", line 451, in result return self.__get_result() File "/root/.pyenv/versions/3.10.16/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result raise self._exception File "/root/Developer/github/LightRAG/.venv/lib/python3.10/site-packages/tenacity/asyncio/__init__.py", line 114, in __call__ result = await fn(*args, **kwargs) File "/root/Developer/github/LightRAG/lightrag/llm.py", line 335, in ollama_model_if_cache for chunk in response: File "/root/Developer/github/LightRAG/.venv/lib/python3.10/site-packages/ollama/_client.py", line 171, in inner part = json.loads(line) File "/root/.pyenv/versions/3.10.16/lib/python3.10/json/__init__.py", line 346, in loads return _default_decoder.decode(s) File "/root/.pyenv/versions/3.10.16/lib/python3.10/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/root/.pyenv/versions/3.10.16/lib/python3.10/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)the fix: check line before json.loads, continue to next line if is empty
Client
AsyncClient