fix: handle HTTP redirects in event stream and improve JWT expiration handling

This commit is contained in:
Mabe
2026-06-21 17:42:16 +02:00
parent 39e2aaecf2
commit 98219a6a53
2 changed files with 31 additions and 1 deletions
+18 -1
View File
@@ -47,8 +47,25 @@ async def event_stream(request: ChatRequest) -> AsyncGenerator[str, None]:
"Accept": "text/event-stream",
}
async with httpx.AsyncClient(timeout=None, follow_redirects=True) as client:
async with httpx.AsyncClient(timeout=None, follow_redirects=False) as client:
async with client.stream("POST", OLLAMA_API_URL, json=payload, headers=headers) as response:
if response.status_code in (301, 302, 307, 308) and response.headers.get("location"):
location = response.headers["location"]
await response.aclose()
logger.debug("Following Ollama redirect to %s", location)
async with client.stream("POST", location, json=payload, headers=headers, follow_redirects=False) as response:
if response.status_code >= 400:
text = await response.aread()
raise HTTPException(status_code=502, detail=f"Ollama Cloud error: {text.decode('utf-8', 'ignore')}" )
async for chunk in response.aiter_text():
if not chunk:
continue
for line in chunk.splitlines():
if line.strip():
yield f"data: {line}\n\n"
return
if response.status_code >= 400:
text = await response.aread()
raise HTTPException(status_code=502, detail=f"Ollama Cloud error: {text.decode('utf-8', 'ignore')}" )