fix duplicate [DONE] and suppress httpcore RuntimeError on Python 3.13 asyncgen cleanup

This commit is contained in:
Roland Tannous 2026-03-30 13:02:55 +00:00
commit cc757b7800

View file

@ -965,13 +965,20 @@ async def _proxy_to_external_provider(
stream = payload.stream,
)
try:
sent_done = False
async for line in gen:
yield f"{line}\n\n"
yield "data: [DONE]\n\n"
if "[DONE]" in line:
sent_done = True
if not sent_done:
yield "data: [DONE]\n\n"
except Exception as exc:
logger.error("external_provider.stream_error", error = str(exc))
finally:
await gen.aclose()
try:
await gen.aclose()
except RuntimeError:
pass # suppress httpcore asyncgen cleanup error (Python 3.13 + httpcore 1.0.x)
await client.close()
return StreamingResponse(