Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 453dd17

Browse files
committed
fix: do not assume streaming for ollama
Aider is sending a mix of requests that need stream or sync processing and we were hardcoding stream to True. Instead, pick if from the request and also fix the formatting of the json response Closes: #624
1 parent dceeef8 commit 453dd17

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

src/codegate/providers/ollama/adapter.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ def normalize(self, data: Dict) -> ChatCompletionRequest:
2525
{"content": normalized_data.pop("prompt"), "role": "user"}
2626
]
2727

28-
# In Ollama force the stream to be True. Continue is not setting this parameter and
29-
# most of our functionality is for streaming completions.
30-
normalized_data["stream"] = True
31-
28+
# if we have the stream flag in data we set it, otherwise defaults to true
29+
normalized_data["stream"] = data.get("stream", True)
3230
return ChatCompletionRequest(**normalized_data)
3331

3432
def denormalize(self, data: ChatCompletionRequest) -> Dict:

src/codegate/providers/ollama/completion_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _create_streaming_response(self, stream: AsyncIterator[ChatResponse]) -> Str
6060
"""
6161
return StreamingResponse(
6262
ollama_stream_generator(stream),
63-
media_type="application/x-ndjson",
63+
media_type="application/x-ndjson; charset=utf-8",
6464
headers={
6565
"Cache-Control": "no-cache",
6666
"Connection": "keep-alive",
@@ -70,4 +70,4 @@ def _create_streaming_response(self, stream: AsyncIterator[ChatResponse]) -> Str
7070
def _create_json_response(
7171
self, response: Union[GenerateResponse, ChatResponse]
7272
) -> JSONResponse:
73-
return JSONResponse(content=response.model_dump_json(), status_code=200)
73+
return JSONResponse(status_code=200, content=response.model_dump())

src/codegate/providers/ollama/provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async def show_model(request: Request):
6565
response = await client.post(
6666
f"{self.base_url}/api/show",
6767
content=body,
68-
headers={"Content-Type": "application/json"},
68+
headers={"Content-Type": "application/json; charset=utf-8"},
6969
)
7070
return response.json()
7171

@@ -99,4 +99,5 @@ async def create_completion(request: Request):
9999
else:
100100
# just continue raising the exception
101101
raise e
102+
102103
return self._completion_handler.create_response(stream)

0 commit comments

Comments
 (0)