From 1837692a66418eff52f184b980b8cb37e0b1a71e Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Thu, 26 Sep 2024 17:40:27 +0800 Subject: [PATCH] fix: sse error message --- api/core/plugin/backwards_invocation/base.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/api/core/plugin/backwards_invocation/base.py b/api/core/plugin/backwards_invocation/base.py index 2ec71fdc5b..1ce126b893 100644 --- a/api/core/plugin/backwards_invocation/base.py +++ b/api/core/plugin/backwards_invocation/base.py @@ -11,13 +11,10 @@ class BaseBackwardsInvocation: if isinstance(response, Generator): try: for chunk in response: - if isinstance(chunk, BaseModel): + if isinstance(chunk, BaseModel | dict): yield BaseBackwardsInvocationResponse(data=chunk).model_dump_json().encode() + b"\n\n" - elif isinstance(chunk, str): yield f"event: {chunk}\n\n".encode() - else: - yield json.dumps(chunk).encode() + b"\n\n" except Exception as e: error_message = BaseBackwardsInvocationResponse(error=str(e)).model_dump_json() yield f"{error_message}\n\n".encode() @@ -28,7 +25,7 @@ class BaseBackwardsInvocation: yield json.dumps(response).encode() + b"\n\n" -T = TypeVar("T", bound=BaseModel | dict | str | bool | int) +T = TypeVar("T", bound=dict | str | bool | int | BaseModel) class BaseBackwardsInvocationResponse(BaseModel, Generic[T]):