Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: add resource closed errors throughout stream (#4021)

cthomasb2a68a467c57
diff --git a/letta/server/rest_api/streaming_response.py b/letta/server/rest_api/streaming_response.py
index 361facffa..a8d0f2454 100644
--- a/letta/server/rest_api/streaming_response.py
+++ b/letta/server/rest_api/streaming_response.py
@@ -273,13 +273,17 @@ class StreamingResponseWithStatusCode(StreamingResponse):
}
)
raise
- await send(
- {
- "type": "http.response.body",
- "body": cancellation_event,
- "more_body": more_body,
- }
- )
+ if self._client_connected:
+ try:
+ await send(
+ {
+ "type": "http.response.body",
+ "body": cancellation_event,
+ "more_body": more_body,
+ }
+ )
+ except anyio.ClosedResourceError:
+ self._client_connected = False
return
# Handle client timeouts (should throw error to inform user)
@@ -298,13 +302,17 @@ class StreamingResponseWithStatusCode(StreamingResponse):
}
)
raise
- await send(
- {
- "type": "http.response.body",
- "body": error_event,
- "more_body": more_body,
- }
- )
+ if self._client_connected:
+ try:
+ await send(
+ {
+ "type": "http.response.body",
+ "body": error_event,
+ "more_body": more_body,
+ }
+ )
+ except anyio.ClosedResourceError:
+ self._client_connected = False
capture_sentry_exception(exc)
return
@@ -323,14 +331,22 @@ class StreamingResponseWithStatusCode(StreamingResponse):
}
)
raise
- await send(
- {
- "type": "http.response.body",
- "body": error_event,
- "more_body": more_body,
- }
- )
+ if self._client_connected:
+ try:
+ await send(
+ {
+ "type": "http.response.body",
+ "body": error_event,
+ "more_body": more_body,
+ }
+ )
+ except anyio.ClosedResourceError:
+ self._client_connected = False
+
capture_sentry_exception(exc)
return
if more_body and self._client_connected:
- await send({"type": "http.response.body", "body": b"", "more_body": False})
+ try:
+ await send({"type": "http.response.body", "body": b"", "more_body": False})
+ except anyio.ClosedResourceError:
+ self._client_connected = False