fix: avoid duplicate spans when a full fetch follows a typed fetch#546
Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit intoJul 6, 2026
Conversation
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.
888f3f6
into
braintrustdata:main
82 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
CachedSpanFetcher.get_spans()returns duplicate spans when a type-filtered fetch is followed by a full (unfiltered) fetch:The no-filter branch calls
_fetch_spans(None), which re-fetches every span, and_fetch_spansalways appends intoself._span_cache[type]. Sincellmwas already cached from the first call, its spans get appended a second time. Scorers that calltrace.get_spans([...])and thentrace.get_spans()get doubled results.Fix
A full fetch returns the authoritative complete set, so reset the per-type cache before it:
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_spansreplace 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_duplicatestoTestCachedSpanFetcherusing the existing offlinefetch_fn=injection (no API key/network).pytest -k CachedSpanFetcher→ 17 passed; the new test fails onmainand passes with the fix.