Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: add retrieve stream testing (#4168)
cthomas6c59bc8dea96
diff --git a/letta/server/rest_api/redis_stream_manager.py b/letta/server/rest_api/redis_stream_manager.pyindex 091304f19..0eb4e5194 100644--- a/letta/server/rest_api/redis_stream_manager.py+++ b/letta/server/rest_api/redis_stream_manager.py@@ -50,7 +50,7 @@ class RedisSSEStreamWriter:# Buffer for batching: run_id -> list of chunksself.buffer: Dict[str, List[Dict]] = defaultdict(list)# Track sequence IDs per run- self.seq_counters: Dict[str, int] = defaultdict(int)+ self.seq_counters: Dict[str, int] = defaultdict(lambda: 1)# Track last flush time per runself.last_flush: Dict[str, float] = defaultdict(float)@@ -237,7 +237,7 @@ async def create_background_stream_processor(async def redis_sse_stream_generator(redis_client: AsyncRedisClient,run_id: str,- start_cursor: Optional[int] = None,+ starting_after: Optional[int] = None,poll_interval: float = 0.1,batch_size: int = 100,) -> AsyncIterator[str]:@@ -250,7 +250,7 @@ async def redis_sse_stream_generator(Args:redis_client: Redis client instancerun_id: The run ID to read chunks for- start_cursor: Sequential ID (integer) to start reading from (default: 0 for beginning)+ starting_after: Sequential ID (integer) to start reading from (default: None for beginning)poll_interval: Seconds to wait between polls when no new data (default: 0.1)batch_size: Number of entries to read per batch (default: 100)@@ -259,7 +259,7 @@ async def redis_sse_stream_generator("""stream_key = f"sse:run:{run_id}"last_redis_id = "-"- cursor_seq_id = start_cursor or 0+ cursor_seq_id = starting_after or 0logger.debug(f"Starting redis_sse_stream_generator for run_id={run_id}, stream_key={stream_key}")@@ -273,7 +273,7 @@ async def redis_sse_stream_generator(continuechunk_seq_id = int(fields.get("seq_id", 0))- if chunk_seq_id >= cursor_seq_id:+ if chunk_seq_id > cursor_seq_id:data = fields.get("data", "")if not data:logger.debug(f"No data found for chunk {chunk_seq_id} in run {run_id}")diff --git a/letta/server/rest_api/routers/v1/runs.py b/letta/server/rest_api/routers/v1/runs.pyindex 72ef6d8ba..d9dcf84ad 100644--- a/letta/server/rest_api/routers/v1/runs.py+++ b/letta/server/rest_api/routers/v1/runs.py@@ -235,7 +235,7 @@ async def delete_run()async def retrieve_stream(run_id: str,- request: RetrieveStreamRequest = Body(...),+ request: RetrieveStreamRequest = Body(None),actor_id: Optional[str] = Header(None, alias="user_id"),server: "SyncServer" = Depends(get_letta_server),):@@ -244,7 +244,7 @@ async def retrieve_stream(stream = redis_sse_stream_generator(redis_client=redis_client,run_id=run_id,- start_cursor=request.starting_after,+ starting_after=request.starting_after,poll_interval=request.poll_interval,batch_size=request.batch_size,)diff --git a/poetry.lock b/poetry.lockindex 13edaea09..7b6b0f45b 100644--- a/poetry.lock+++ b/poetry.lock@@ -3708,14 +3708,14 @@ vcr = ["vcrpy (>=7.0.0)"][[package]]name = "letta-client"-version = "0.1.276"+version = "0.1.277"description = ""optional = falsepython-versions = "<4.0,>=3.8"groups = ["main"]files = [- {file = "letta_client-0.1.276-py3-none-any.whl", hash = "sha256:6b5472bc9090265ee4ec832a6002eadf3e5dfd4fd2d229393a8e77930f3821fd"},- {file = "letta_client-0.1.276.tar.gz", hash = "sha256:23f992dc8714cfec857d5ae209d495a5a5911da285d06bdc6f4d1c744e27ebf1"},+ {file = "letta_client-0.1.277-py3-none-any.whl", hash = "sha256:2428ee84ff3cf582c935623dd7678845bfe46e79c75cb9a1a1b6efe284c5bc16"},+ {file = "letta_client-0.1.277.tar.gz", hash = "sha256:112837705c43c7c2c9a6baa99c38a0ad390f8bcb7ad0de5e3b9556990b898427"},][package.dependencies]@@ -9231,4 +9231,4 @@ tests = ["wikipedia"][metadata]lock-version = "2.1"python-versions = "<3.14,>=3.11"-content-hash = "7cfed00763e8850c5b92a42969920b6793aedba88d4dfe268d85c6f529de7060"+content-hash = "db92e7562fa04425478033ae7f63ac7d8741192015ea0d4d970d2f1d47881c76"diff --git a/pyproject.toml b/pyproject.tomlindex d2317e96f..b585282e5 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -72,7 +72,7 @@ llama-index = "^0.12.2"llama-index-embeddings-openai = "^0.3.1"e2b-code-interpreter = {version = "^1.0.3", optional = true}anthropic = "^0.49.0"-letta_client = "^0.1.276"+letta_client = "^0.1.277"openai = "^1.99.9"opentelemetry-api = "1.30.0"opentelemetry-sdk = "1.30.0"diff --git a/tests/integration_test_send_message.py b/tests/integration_test_send_message.pyindex 47731fd1e..344863fee 100644--- a/tests/integration_test_send_message.py+++ b/tests/integration_test_send_message.py@@ -1167,6 +1167,25 @@ def test_background_token_streaming_greeting_with_assistant_message(messages_from_db = client.agents.messages.list(agent_id=agent_state.id, after=last_message[0].id)assert_greeting_with_assistant_message_response(messages_from_db, from_db=True, llm_config=llm_config)+ run_id = messages[0].run_id+ assert run_id is not None++ response = client.runs.stream(run_id=run_id, starting_after=0)+ messages = accumulate_chunks(+ list(response), verify_token_streaming=(llm_config.model_endpoint_type in ["anthropic", "openai", "bedrock"])+ )+ assert_greeting_with_assistant_message_response(messages, streaming=True, token_streaming=True, llm_config=llm_config)++ last_message_cursor = messages[-3].seq_id - 1+ response = client.runs.stream(run_id=run_id, starting_after=last_message_cursor)+ messages = accumulate_chunks(+ list(response), verify_token_streaming=(llm_config.model_endpoint_type in ["anthropic", "openai", "bedrock"])+ )+ assert len(messages) == 3+ assert messages[0].message_type == "assistant_message" and messages[0].seq_id == last_message_cursor + 1+ assert messages[1].message_type == "stop_reason"+ assert messages[2].message_type == "usage_statistics"+@pytest.mark.parametrize("llm_config",