:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(langsmith): scope the examples fetch to a dataset (#83111)
Tom Owersf52ef2700f2a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/langsmith.py b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/langsmith.pyindex d4b9880320e7..87b24748192d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/langsmith.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/langsmith.py@@ -67,6 +67,10 @@ MAX_PAGES_PER_RUN = 50_000# worth, far beyond any legitimate workspace's tracing-project count.MAX_SESSION_IDS_BYTES = 2 * 1024 * 1024+# Same cumulative-memory guard as MAX_SESSION_IDS_BYTES, applied to the dataset ids collected to+# scope the examples query. A user-controlled host could otherwise stream unbounded dataset ids.+MAX_DATASET_IDS_BYTES = 2 * 1024 * 1024+class LangSmithRetryableError(Exception):pass@@ -113,7 +117,7 @@ def _read_capped_body(response: requests.Response, cap: int = MAX_RESPONSE_BYTESreturn bytes(buffer)[email protected](frozen=False) # mutability is unused (always constructed fresh); explicit per house conventionclass LangSmithResumeConfig:# runs/query body cursor for the page to fetch next; None for offset-paginated endpoints.cursor: str | None = None@@ -123,6 +127,9 @@ class LangSmithResumeConfig:# resumed run keeps paging the same window (a recomputed bound would shift what each# cursor/offset points at).window_start: str | None = None+ # Dataset being paged when an examples run was interrupted; examples are fetched per dataset,+ # so the resume needs both which dataset and the offset within it. None for every other endpoint.+ dataset_id: str | None = Nonedef normalize_base_url(raw: str) -> str:@@ -377,6 +384,50 @@ def _list_session_ids(return ids+def _list_dataset_ids(+ session: requests.Session,+ headers: dict[str, str],+ base_url: str,+ logger: FilteringBoundLogger,+) -> list[str]:+ """Collect every dataset id in the workspace.++ GET /examples requires a `dataset` filter (a 400 otherwise); this sync pulls examples across+ every dataset, so it lists dataset ids and pages each dataset's examples in turn.+ """+ config = LANGSMITH_ENDPOINTS["datasets"]+ ids: list[str] = []+ ids_bytes = 0+ offset = 0+ pages = 0+ while True:+ url = f"{base_url}{config.path}?{urlencode({'limit': config.page_size, 'offset': offset})}"+ data = _fetch_page(session, url, headers, logger)+ rows = data if isinstance(data, list) else []+ if not rows:+ break+ for row in rows:+ row_id = row.get("id")+ if not row_id:+ continue+ ids.append(row_id)+ ids_bytes += len(row_id.encode())+ if ids_bytes > MAX_DATASET_IDS_BYTES:+ raise LangSmithResponseTooLargeError(+ f"LangSmith returned an oversized set of dataset ids "+ f"(> {MAX_DATASET_IDS_BYTES} bytes) while scoping the examples query"+ )+ if len(rows) < config.page_size:+ break+ offset += config.page_size+ pages += 1+ if pages >= MAX_PAGES_PER_RUN:+ raise LangSmithPageLimitError(+ f"LangSmith dataset listing hit the {MAX_PAGES_PER_RUN}-page limit while scoping the examples query"+ )+ return ids++def _get_runs_rows(session: requests.Session,headers: dict[str, str],@@ -550,6 +601,97 @@ def _get_offset_rows()+def _get_examples_rows(+ session: requests.Session,+ headers: dict[str, str],+ base_url: str,+ config: LangSmithEndpointConfig,+ batcher: Batcher,+ resumable_source_manager: ResumableSourceManager[LangSmithResumeConfig],+ logger: FilteringBoundLogger,+ should_use_incremental_field: bool,+ db_incremental_field_last_value: Any,+) -> Iterator[Any]:+ """Page GET /examples for every dataset in the workspace.++ Examples belong to a dataset, and GET /examples rejects a request that doesn't scope to one+ (a 400 otherwise), so this walks each dataset id and pages that dataset's examples with a+ `dataset` filter. Examples are full-refresh only (no server-side window), so there's no+ incremental watermark to pin — the resume tracks which dataset and offset an interrupted run+ was on."""+ dataset_ids = _list_dataset_ids(session, headers, base_url, logger)+ if not dataset_ids:+ logger.debug("LangSmith: no datasets in workspace, nothing to sync for examples")+ return++ resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None+ # Only resume into a dataset that still exists; a deleted one restarts the sweep from the top+ # so no dataset is silently skipped.+ resume_into_dataset = resume.dataset_id if resume is not None and resume.dataset_id in dataset_ids else None+ resume_offset = resume.offset if resume is not None else None+ start_index = dataset_ids.index(resume_into_dataset) if resume_into_dataset is not None else 0+ if resume_into_dataset is not None:+ logger.debug(f"LangSmith: resuming examples from dataset={resume_into_dataset} offset={resume_offset}")++ pages = 0+ for index in range(start_index, len(dataset_ids)):+ dataset_id = dataset_ids[index]+ is_last_dataset = index == len(dataset_ids) - 1+ if index == start_index and resume_into_dataset is not None:+ offset = resume_offset or 0+ else:+ offset = 0+ # Checkpoint the dataset boundary before reading it, so a crash resumes at this dataset+ # rather than re-reading the previous one.+ resumable_source_manager.save_state(LangSmithResumeConfig(dataset_id=dataset_id, offset=0))++ while True:+ page_offset = offset+ url = f"{base_url}{config.path}?{urlencode({'dataset': dataset_id, 'limit': config.page_size, 'offset': page_offset})}"+ data = _fetch_page(session, url, headers, logger)+ # Count every request, including a short first page, against the per-run limit. A+ # dataset whose examples fit on one page must still cost one request — otherwise a+ # host serving many datasets (bounded only by MAX_DATASET_IDS_BYTES) each with a+ # single short page pages forever without ever tripping MAX_PAGES_PER_RUN.+ pages += 1++ rows = data if isinstance(data, list) else []+ is_last_page = not rows or len(rows) < config.page_size++ for item in rows:+ batcher.batch(item)+ if batcher.should_yield():+ yield batcher.get_table()+ # Save AFTER yielding so a crash re-reads this page rather than skipping it.+ # Skip only on the final page of the final dataset — the run is finishing.+ if not (is_last_page and is_last_dataset):+ resumable_source_manager.save_state(+ LangSmithResumeConfig(dataset_id=dataset_id, offset=page_offset)+ )++ run_is_finished = is_last_page and is_last_dataset+ if pages >= MAX_PAGES_PER_RUN and not run_is_finished:+ # Flush any batched-but-unyielded items before checkpointing where to resume —+ # resuming skips this page, so an unflushed partial batch is lost.+ if batcher.should_yield(include_incomplete_chunk=True):+ yield batcher.get_table()+ if is_last_page:+ # This dataset is exhausted; resume picks up at the start of the next one.+ next_dataset_id = dataset_ids[index + 1]+ resumable_source_manager.save_state(LangSmithResumeConfig(dataset_id=next_dataset_id, offset=0))+ else:+ resumable_source_manager.save_state(+ LangSmithResumeConfig(dataset_id=dataset_id, offset=page_offset + config.page_size)+ )+ raise LangSmithPageLimitError(+ f"LangSmith examples import hit the {MAX_PAGES_PER_RUN}-page per-attempt limit; resuming from checkpoint"+ )++ if is_last_page:+ break+ offset = page_offset + config.page_size++def get_rows(api_key: str,base_url: str,@@ -573,7 +715,12 @@ def get_rows(# carry secrets or personal data the name-based scrubber won't recognize.session = make_tracked_session(redact_values=(api_key,), allow_redirects=False, capture=False)- pager = _get_runs_rows if config.pagination == "cursor" else _get_offset_rows+ if config.scoped_by_dataset:+ pager = _get_examples_rows+ elif config.pagination == "cursor":+ pager = _get_runs_rows+ else:+ pager = _get_offset_rowsyield from pager(session,headers,diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/settings.pyindex e26b641dfc02..61b0396c4153 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/settings.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/settings.py@@ -40,7 +40,7 @@ RUNS_SELECT_FIELDS = []-@dataclass+@dataclass(frozen=False) # mutability is unused (always constructed fresh); explicit per house conventionclass LangSmithEndpointConfig:name: strpath: str@@ -69,6 +69,9 @@ class LangSmithEndpointConfig:# successful job end instead of checkpointing per batch, which is correct under any actual# ordering. Mid-run recovery comes from the resumable cursor/offset state instead.sort_mode: Literal["asc", "desc"] = "asc"+ # GET /examples rejects an unscoped request — examples belong to a dataset — so it's paged per+ # dataset with a `dataset` filter instead of the plain offset paginator. Only examples sets this.+ scoped_by_dataset: bool = False_START_TIME_INCREMENTAL_FIELDS: list[IncrementalField] = [@@ -118,12 +121,14 @@ LANGSMITH_ENDPOINTS: dict[str, LangSmithEndpointConfig] = {path="/api/v1/datasets",incremental_fields=[],),- # Dataset examples across all datasets. The API versions examples via `as_of` snapshots+ # Dataset examples across all datasets. GET /examples must be scoped to a dataset, so this is+ # paged per dataset (see `scoped_by_dataset`). The API versions examples via `as_of` snapshots# rather than a created/modified filter, so full refresh only."examples": LangSmithEndpointConfig(name="examples",path="/api/v1/examples",incremental_fields=[],+ scoped_by_dataset=True,),# Human and programmatic feedback scores attached to runs. `min_created_at` is a genuine# server-side filter; feedback edits move modified_at (not filterable), so the lookbackdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/tests/test_langsmith.py b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/tests/test_langsmith.pyindex 12c7bb075118..ca82a968f8f8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/tests/test_langsmith.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/tests/test_langsmith.py@@ -295,6 +295,89 @@ class TestOffsetPagination:assert "min_created_at=2026-01-01T00%3A00%3A00.000000Z" in urls[0]+class TestExamplesPagination:+ def test_examples_are_scoped_per_dataset(self):+ # GET /examples rejects an unscoped request (a 400), so examples must be paged per dataset+ # with a `dataset` filter — the regression that failed every examples sync.+ manager = FakeManager()+ dataset_ids = ["ds-1", "ds-2"]+ urls: list[str] = []++ def fake_fetch(session, url, headers, log, json_body=None):+ urls.append(url)+ if "/api/v1/examples" in url:+ return [{"id": "ex-1"}]+ return [{"id": d} for d in dataset_ids]++ with mock.patch(_FETCH_PAGE, side_effect=fake_fetch):+ rows = _collect(get_rows("key", BASE_URL, "examples", logger, manager, 1)) # type: ignore[arg-type]++ example_urls = [u for u in urls if "/api/v1/examples" in u]+ assert len(example_urls) == 2+ assert "dataset=ds-1" in example_urls[0]+ assert "dataset=ds-2" in example_urls[1]+ assert len(rows) == 2++ def test_no_datasets_in_workspace_skips_examples_entirely(self):+ # With no datasets there is nothing to scope to, so no examples request should be issued.+ manager = FakeManager()+ urls: list[str] = []++ def fake_fetch(session, url, headers, log, json_body=None):+ urls.append(url)+ return []++ with mock.patch(_FETCH_PAGE, side_effect=fake_fetch):+ rows = _collect(get_rows("key", BASE_URL, "examples", logger, manager, 1)) # type: ignore[arg-type]++ assert rows == []+ assert all("/api/v1/examples" not in u for u in urls)++ def test_resume_continues_from_saved_dataset_and_offset(self):+ # A resumed run skips datasets already fully read, picks the interrupted dataset up at its+ # saved offset, and reads the remaining datasets from the start.+ manager = FakeManager(resume=LangSmithResumeConfig(dataset_id="ds-2", offset=100))+ dataset_ids = ["ds-1", "ds-2", "ds-3"]+ example_urls: list[str] = []++ def fake_fetch(session, url, headers, log, json_body=None):+ if "/api/v1/examples" in url:+ example_urls.append(url)+ return [{"id": "ex"}]+ return [{"id": d} for d in dataset_ids]++ with mock.patch(_FETCH_PAGE, side_effect=fake_fetch):+ _collect(get_rows("key", BASE_URL, "examples", logger, manager, 1)) # type: ignore[arg-type]++ assert len(example_urls) == 2+ assert "dataset=ds-2" in example_urls[0] and "offset=100" in example_urls[0]+ assert "dataset=ds-3" in example_urls[1] and "offset=0" in example_urls[1]+ assert all("dataset=ds-1" not in u for u in example_urls)++ def test_many_short_pages_still_hit_the_page_limit(self):+ # A dataset whose examples fit on a single short page must still cost one request against+ # MAX_PAGES_PER_RUN. Otherwise a host serving many datasets (bounded only by+ # MAX_DATASET_IDS_BYTES), each with one short page, could page forever without ever+ # tripping the per-run limit.+ manager = FakeManager()+ dataset_ids = [f"ds-{i}" for i in range(10)]+ example_urls: list[str] = []++ def fake_fetch(session, url, headers, log, json_body=None):+ if "/api/v1/examples" in url:+ example_urls.append(url)+ return [{"id": "ex"}] # a single-item, short page — never the "full page" branch+ return [{"id": d} for d in dataset_ids]++ with mock.patch(_MAX_PAGES, 3), mock.patch(_FETCH_PAGE, side_effect=fake_fetch):+ with pytest.raises(LangSmithPageLimitError):+ _collect(get_rows("key", BASE_URL, "examples", logger, manager, 1)) # type: ignore[arg-type]++ assert len(example_urls) == 3+ assert manager.saved[-1].dataset_id == "ds-3"+ assert manager.saved[-1].offset == 0++class TestPaginationAbuseGuards:def test_oversized_session_id_accumulation_raises(self):# A host controls both the count and size of session ids returned while scoping the runs