From ee5f2a4ab159b59105ed58fa5c5f9ed6c2733856 Mon Sep 17 00:00:00 2001 From: Giancarlo-Ma <65126107+Giancarlo-Ma@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:29:23 +0800 Subject: [PATCH] fix: api streaming TypeError: Object of type generator is not JSON serializable --- g4f/api/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index 43bca2a5..ad3b0db4 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -1,4 +1,5 @@ from fastapi import FastAPI, Response, Request +from fastapi.responses import StreamingResponse from typing import List, Union, Any, Dict, AnyStr from ._tokenizer import tokenize from .. import BaseProvider @@ -151,7 +152,7 @@ class Api: except GeneratorExit: pass - return Response(content=json.dumps(streaming(), indent=4), media_type="application/json") + return StreamingResponse(streaming(), media_type="text/event-stream") @self.app.post("/v1/completions") async def completions():