async for line in resp.content.iter_any() will return
incomplete lines when the lines are long, and that
results in an exception being thrown by json.loads()
when it tries to parse the incomplete JSON
The default behavior of the stream reader for aiohttp
response objects is to iterate over lines, so just
removing .iter_any() fixes the bug
In the text_completion() function, it previously threw an exception at:
raw_response = response._hidden_params.get("original_response", None)
Due to response being an coroutine object to an ollama_acompletion call,
so I added an asyncio.iscoroutine() check for the response and handle it
by calling response = asyncio.run(response)
I also had to fix atext_completion(), where init_response was an instance
of TextCompletionResponse.
Since this case was not handled by the if-elif that checks if init_response
is a coroutine, a dict or a ModelResponse instance, response was unbound
which threw an exception on the "return response" line.
Note that a regular pyright based linter detects that response is possibly
unbound, and that the same code pattern is used in multiple other places
in main.py.
I would suggest that you either change these cases:
init_response = await loop.run_in_executor(...
if isinstance(init_response, ...
response = init_response
elif asyncio.iscoroutine(init_response):
response = await init_response
To either just:
response = await loop.run_in_executor(
if asyncio.iscoroutine(response):
response = await response
Or at the very least, include an else statement and set response = init_response,
so that response is never unbound when the code proceeds.