fix: sse error message

This commit is contained in:
Yeuoly 2024-09-26 17:40:27 +08:00
parent 5dcd25a613
commit 1837692a66
No known key found for this signature in database
GPG Key ID: A66E7E320FB19F61

View File

@ -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]):