Skip to content

fix: avoid duplicate spans when a full fetch follows a typed fetch#546

Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit into
braintrustdata:mainfrom
NishchayMahor:fix/span-cache-duplicate-on-full-fetch
Jul 6, 2026
Merged

fix: avoid duplicate spans when a full fetch follows a typed fetch#546
Abhijeet Prasad (AbhiPrasad) merged 1 commit into
braintrustdata:mainfrom
NishchayMahor:fix/span-cache-duplicate-on-full-fetch

Conversation

@NishchayMahor

Copy link
Copy Markdown
Contributor

What

CachedSpanFetcher.get_spans() returns duplicate spans when a type-filtered fetch is followed by a full (unfiltered) fetch:

await fetcher.get_spans(["llm"])   # caches llm-1, llm-2
await fetcher.get_spans()          # -> fn-1, llm-1, llm-1, llm-2, llm-2  ❌

The no-filter branch calls _fetch_spans(None), which re-fetches every span, and _fetch_spans always appends into self._span_cache[type]. Since llm was already cached from the first call, its spans get appended a second time. Scorers that call trace.get_spans([...]) and then trace.get_spans() get doubled results.

Fix

A full fetch returns the authoritative complete set, so reset the per-type cache before it:

if not span_type or len(span_type) == 0:
    self._span_cache = {}
    await self._fetch_spans(None)
    ...

The typed-fetch path is unchanged (it only fetches types not already cached, so it never double-appends). An alternative would be to make _fetch_spans replace per-type lists instead of appending; I went with the smaller, more localized change, but happy to switch if you'd prefer that.

Testing

Added test_fetch_all_after_typed_fetch_has_no_duplicates to TestCachedSpanFetcher using the existing offline fetch_fn= injection (no API key/network). pytest -k CachedSpanFetcher17 passed; the new test fails on main and passes with the fix.

CachedSpanFetcher.get_spans() with no filter re-fetches every span and
_fetch_spans appends into the per-type cache, so a prior typed fetch's spans
(e.g. get_spans(["llm"]) then get_spans()) were duplicated. Treat a full
fetch as authoritative and reset the cache before it. Adds a regression test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) enabled auto-merge (squash) July 6, 2026 15:30
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) merged commit 888f3f6 into braintrustdata:main Jul 6, 2026
82 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants