From 7d33254f5f3fa11945ed732e5abcf0588a044dce Mon Sep 17 00:00:00 2001 From: cthomas Date: Tue, 5 Aug 2025 16:07:30 -0700 Subject: [PATCH] feat: log stream cancellation to sentry (#3759) --- .../anthropic_streaming_interface.py | 21 ++++++++++++++----- .../interfaces/openai_streaming_interface.py | 21 ++++++++++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/letta/interfaces/anthropic_streaming_interface.py b/letta/interfaces/anthropic_streaming_interface.py index 9e1328fa..3022a11c 100644 --- a/letta/interfaces/anthropic_streaming_interface.py +++ b/letta/interfaces/anthropic_streaming_interface.py @@ -389,13 +389,24 @@ class AnthropicStreamingInterface: self.anthropic_mode = None except asyncio.CancelledError as e: - logger.info("Cancelled stream %s", e) - yield LettaStopReason(stop_reason=StopReasonType.cancelled) - raise + import traceback + + logger.error("Cancelled stream %s: %s", e, traceback.format_exc()) + ttft_span.add_event( + name="stop_reason", + attributes={"stop_reason": StopReasonType.cancelled.value, "error": str(e), "stacktrace": traceback.format_exc()}, + ) + raise e except Exception as e: - logger.error("Error processing stream: %s", e) + import traceback + + logger.error("Error processing stream: %s", e, traceback.format_exc()) + ttft_span.add_event( + name="stop_reason", + attributes={"stop_reason": StopReasonType.error.value, "error": str(e), "stacktrace": traceback.format_exc()}, + ) yield LettaStopReason(stop_reason=StopReasonType.error) - raise + raise e finally: logger.info("AnthropicStreamingInterface: Stream processing complete.") diff --git a/letta/interfaces/openai_streaming_interface.py b/letta/interfaces/openai_streaming_interface.py index aee3584d..710c3ba0 100644 --- a/letta/interfaces/openai_streaming_interface.py +++ b/letta/interfaces/openai_streaming_interface.py @@ -389,12 +389,23 @@ class OpenAIStreamingInterface: yield tool_call_msg self.function_id_buffer = None except asyncio.CancelledError as e: - logger.info("Cancelled stream %s", e) - yield LettaStopReason(stop_reason=StopReasonType.cancelled) - raise + import traceback + + logger.error("Cancelled stream %s: %s", e, traceback.format_exc()) + ttft_span.add_event( + name="stop_reason", + attributes={"stop_reason": StopReasonType.cancelled.value, "error": str(e), "stacktrace": traceback.format_exc()}, + ) + raise e except Exception as e: - logger.error("Error processing stream: %s", e) + import traceback + + logger.error("Error processing stream: %s", e, traceback.format_exc()) + ttft_span.add_event( + name="stop_reason", + attributes={"stop_reason": StopReasonType.error.value, "error": str(e), "stacktrace": traceback.format_exc()}, + ) yield LettaStopReason(stop_reason=StopReasonType.error) - raise + raise e finally: logger.info("OpenAIStreamingInterface: Stream processing complete.")