From b7fd563b9beb3144bfb489b95375a40b594274e8 Mon Sep 17 00:00:00 2001 From: "Gareth.Y" <91742523+linjiaye929@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:43:43 +0800 Subject: [PATCH] Allow trace thread retrieval to retry after a transient failure --- py/src/braintrust/test_trace.py | 31 +++++++++++++++++++++++++++++++ py/src/braintrust/trace.py | 9 ++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/py/src/braintrust/test_trace.py b/py/src/braintrust/test_trace.py index a16ae0420..080c88ada 100644 --- a/py/src/braintrust/test_trace.py +++ b/py/src/braintrust/test_trace.py @@ -1,5 +1,6 @@ """Tests for Trace functionality.""" +import asyncio import os import braintrust @@ -551,6 +552,36 @@ def fake_invoke(**kwargs): assert calls[0]["global_function"] == "custom_preprocessor" assert calls[0]["function_type"] == "preprocessor" + @pytest.mark.asyncio + @pytest.mark.parametrize("failure", ["flush", "invoke"]) + async def test_retries_failed_thread_fetch(self, monkeypatch, failure): + flushes = 0 + invokes = 0 + messages = [{"role": "user", "content": "retry succeeded"}] + + async def flush(): + nonlocal flushes + flushes += 1 + if failure == "flush" and flushes == 1: + raise RuntimeError("transient flush failure") + + def fake_invoke(**kwargs): + nonlocal invokes + invokes += 1 + if failure == "invoke" and invokes == 1: + raise RuntimeError("transient invoke failure") + return messages + + monkeypatch.setattr("braintrust.trace.invoke", fake_invoke) + trace = LocalTrace("experiment", "exp-123", "root-456", flush, _DummyState()) + with pytest.raises(RuntimeError, match=f"transient {failure} failure"): + await trace.get_thread() + + assert await asyncio.gather(trace.get_thread(), trace.get_thread()) == [messages, messages] + assert await trace.get_thread() == messages + assert flushes == (2 if failure == "flush" else 1) + assert invokes == (2 if failure == "invoke" else 1) + @pytest.mark.asyncio async def test_caches_by_preprocessor(self, monkeypatch): call_count = 0 diff --git a/py/src/braintrust/trace.py b/py/src/braintrust/trace.py index cb3b7d258..3fd32997d 100644 --- a/py/src/braintrust/trace.py +++ b/py/src/braintrust/trace.py @@ -594,9 +594,12 @@ async def get_thread(self, options: GetThreadOptions | None = None) -> list[Any] """ preprocessor = options.get("preprocessor") if options and options.get("preprocessor") else None cache_key = preprocessor or "project_default" - if cache_key not in self._thread_cache: - self._thread_cache[cache_key] = asyncio.create_task(self._fetch_thread(options)) - return await self._thread_cache[cache_key] + task = self._thread_cache.get(cache_key) + # Share in-flight work and successful results, not a previous failed attempt. + if task is None or (task.done() and (task.cancelled() or task.exception() is not None)): + task = asyncio.create_task(self._fetch_thread(options)) + self._thread_cache[cache_key] = task + return await task async def _fetch_thread(self, options: GetThreadOptions | None = None) -> list[Any]: """Fetch thread messages via preprocessor invocation."""