diff --git a/templates/types/streaming/fastapi/app/api/routers/vercel_response.py b/templates/types/streaming/fastapi/app/api/routers/vercel_response.py index abb0185feb40285899d72488868d69757781d333..7ed19c5a12274f8165f880cc6f85ab4537ec321a 100644 --- a/templates/types/streaming/fastapi/app/api/routers/vercel_response.py +++ b/templates/types/streaming/fastapi/app/api/routers/vercel_response.py @@ -13,9 +13,9 @@ class VercelStreamResponse(StreamingResponse): @classmethod def convert_text(cls, token: str): - # Escape newlines to avoid breaking the stream - token = token.replace("\n", "\\n") - return f'{cls.TEXT_PREFIX}"{token}"\n' + # Escape newlines and double quotes to avoid breaking the stream + token = json.dumps(token) + return f"{cls.TEXT_PREFIX}{token}\n" @classmethod def convert_data(cls, data: dict):