From 83adf0b9d243e4e749e2c1fbbb727c096f9d6892 Mon Sep 17 00:00:00 2001 From: Charles Packer Date: Mon, 16 Dec 2024 16:38:05 -0800 Subject: [PATCH] fix: add error logging on stream fails (#2261) --- letta/server/rest_api/utils.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/letta/server/rest_api/utils.py b/letta/server/rest_api/utils.py index a87400dc..da8d472c 100644 --- a/letta/server/rest_api/utils.py +++ b/letta/server/rest_api/utils.py @@ -1,5 +1,6 @@ import asyncio import json +import os import warnings from enum import Enum from typing import AsyncGenerator, Optional, Union @@ -64,13 +65,31 @@ async def sse_async_generator( import traceback traceback.print_exc() - warnings.warn(f"Error getting usage data: {e}") - yield sse_formatter({"error": "Failed to get usage data"}) + warnings.warn(f"SSE stream generator failed: {e}") + + # Log the error, since the exception handler upstack (in FastAPI) won't catch it, because this may be a 200 response + # Print the stack trace + if (os.getenv("SENTRY_DSN") is not None) and (os.getenv("SENTRY_DSN") != ""): + import sentry_sdk + + sentry_sdk.capture_exception(e) + + yield sse_formatter({"error": f"Stream failed (internal error occured)"}) except Exception as e: - print("stream decoder hit error:", e) - print(traceback.print_stack()) - yield sse_formatter({"error": "stream decoder encountered an error"}) + import traceback + + traceback.print_exc() + warnings.warn(f"SSE stream generator failed: {e}") + + # Log the error, since the exception handler upstack (in FastAPI) won't catch it, because this may be a 200 response + # Print the stack trace + if (os.getenv("SENTRY_DSN") is not None) and (os.getenv("SENTRY_DSN") != ""): + import sentry_sdk + + sentry_sdk.capture_exception(e) + + yield sse_formatter({"error": "Stream failed (decoder encountered an error)"}) finally: if finish_message: