From a13c6c78bb534840b6f7b5063db1081e076bfdf3 Mon Sep 17 00:00:00 2001 From: Charles Packer Date: Wed, 30 Jul 2025 16:49:45 -0700 Subject: [PATCH] feat: move keepalive to an arg (#3652) --- letta/schemas/letta_request.py | 4 ++++ letta/server/rest_api/routers/v1/agents.py | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/letta/schemas/letta_request.py b/letta/schemas/letta_request.py index cdaf27c1..5f16380a 100644 --- a/letta/schemas/letta_request.py +++ b/letta/schemas/letta_request.py @@ -42,6 +42,10 @@ class LettaStreamingRequest(LettaRequest): default=False, description="Flag to determine if individual tokens should be streamed. Set to True for token streaming (requires stream_steps = True).", ) + include_pings: bool = Field( + default=False, + description="Whether to include periodic keepalive ping messages in the stream to prevent connection timeouts.", + ) class LettaAsyncRequest(LettaRequest): diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.py index 387732cf..b146585d 100644 --- a/letta/server/rest_api/routers/v1/agents.py +++ b/letta/server/rest_api/routers/v1/agents.py @@ -1071,8 +1071,8 @@ async def send_message_streaming( request_start_timestamp_ns=request_start_timestamp_ns, include_return_message_types=request.include_return_message_types, ) - # Conditionally wrap with keepalive based on settings - if settings.enable_keepalive: + # Conditionally wrap with keepalive based on request parameter + if request.include_pings and settings.enable_keepalive: stream = add_keepalive_to_stream(raw_stream, keepalive_interval=settings.keepalive_interval) else: stream = raw_stream @@ -1089,8 +1089,8 @@ async def send_message_streaming( request_start_timestamp_ns=request_start_timestamp_ns, include_return_message_types=request.include_return_message_types, ) - # Conditionally wrap with keepalive based on settings - if settings.enable_keepalive: + # Conditionally wrap with keepalive based on request parameter + if request.include_pings and settings.enable_keepalive: stream = add_keepalive_to_stream(raw_stream, keepalive_interval=settings.keepalive_interval) else: stream = raw_stream