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.

fix: add OTID retry to retrieve_conversation_stream + integration test (#10229)

cthomas4c67d7f7eddf
diff --git a/letta/server/rest_api/routers/v1/conversations.py b/letta/server/rest_api/routers/v1/conversations.py
index 0f6170d51..7d3ca8e20 100644
--- a/letta/server/rest_api/routers/v1/conversations.py
+++ b/letta/server/rest_api/routers/v1/conversations.py
@@ -1,3 +1,4 @@
+import asyncio
from datetime import timedelta
from typing import Annotated, Any, Dict, List, Literal, Optional
@@ -685,8 +686,14 @@ async def retrieve_conversation_stream(
raise LettaInvalidArgumentError(f"Run {run_id} not found.")
# Priority 2: OTID provided (look up run_id from Redis)
+ # Retry with backoff: the mapping may not be stored yet if the request
+ # that created the run is still inside _create_run() (DB insert).
elif request and request.otid:
- run_id = await redis_client.get_run_id_by_otid(request.otid)
+ for _attempt in range(3):
+ run_id = await redis_client.get_run_id_by_otid(request.otid)
+ if run_id:
+ break
+ await asyncio.sleep(0.25 * (2**_attempt)) # 250ms, 500ms, 1s
if not run_id:
raise LettaInvalidArgumentError(f"No run found for otid={request.otid}. The run may have expired or never existed.")
# Fetch run to check expiration
diff --git a/tests/integration_test_conversations_sdk.py b/tests/integration_test_conversations_sdk.py
index de4471813..7c0b1b2c8 100644
--- a/tests/integration_test_conversations_sdk.py
+++ b/tests/integration_test_conversations_sdk.py
@@ -1741,3 +1741,70 @@ async def test_duplicate_request_recovery_with_same_otid(
assert len(chunks_from_stream3) > 0, "Stream 3 should have received chunks"
assert run_id_3 is not None, "Stream 3 should have a run_id"
assert run_id_3 == run_id_1, f"Stream 3 should recover the same run_id after lock released: {run_id_3} vs {run_id_1}"
+
+
+async def test_otid_recovery_via_retrieve_stream(
+ server_url: str,
+ otid_test_agent,
+ async_client: AsyncLetta,
+ disable_e2b_api_key: Any,
+) -> None:
+ """
+ Test that the retrieve_conversation_stream endpoint can recover a run via OTID.
+
+ This simulates the client flow:
+ 1. Send a background streaming request with an OTID
+ 2. While it's running, call retrieve_conversation_stream with the same OTID
+ 3. The stream endpoint should find the run via OTID lookup and return chunks
+ """
+ from letta.settings import settings
+
+ if settings.redis_host is None or settings.redis_port is None:
+ pytest.skip("Redis not configured - skipping OTID stream recovery test")
+
+ conversation = await async_client.conversations.create(agent_id=otid_test_agent.id)
+
+ shared_otid = f"test-otid-{uuid.uuid4()}"
+ messages = [
+ MessageCreateParam(
+ role="user",
+ content="Hello! Please respond briefly.",
+ otid=shared_otid,
+ )
+ ]
+
+ # Start a background streaming request
+ chunks_from_send: List[Any] = []
+ send_started = asyncio.Event()
+
+ async def send_message():
+ stream = await async_client.conversations.messages.create(
+ conversation_id=conversation.id,
+ messages=messages,
+ background=True,
+ )
+ async for chunk in stream:
+ chunks_from_send.append(chunk)
+ if not send_started.is_set():
+ send_started.set()
+ await asyncio.sleep(0.01)
+
+ # Start the send, wait for it to begin streaming, then try OTID recovery
+ send_task = asyncio.create_task(send_message())
+ await send_started.wait()
+ await asyncio.sleep(0.5) # Give time for OTID mapping to be stored
+
+ # Recover stream via OTID using the retrieve endpoint
+ recover_response = requests.post(
+ f"{server_url}/v1/conversations/{conversation.id}/stream",
+ json={"otid": shared_otid},
+ )
+ assert recover_response.status_code == 200, (
+ f"OTID recovery via retrieve_stream should succeed: {recover_response.status_code} {recover_response.text}"
+ )
+
+ # Wait for original send to finish
+ await send_task
+
+ assert len(chunks_from_send) > 0, "Original send should have received chunks"