Skip to content

: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.

fix(langsmith): scope runs/query to the workspace's tracing projects (#72783)

Tom Owersca7e27a6ff36
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.py
index f2c464c1afab..e24d730c8323 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
@@ -60,6 +60,13 @@ MAX_CURSOR_BYTES = 8 * 1024
# make hitting this in one attempt take days.
MAX_PAGES_PER_RUN = 50_000
+# Cap the total bytes of session ids accumulated while scoping the runs query. `host` is
+# user-controlled and returns arbitrary `id` strings on every project page, so without a cumulative
+# cap a host could return unboundedly many (or oversized) ids across thousands of pages — well
+# before MAX_PAGES_PER_RUN trips — and exhaust a shared import worker's memory. ~58k real UUIDs'
+# worth, far beyond any legitimate workspace's tracing-project count.
+MAX_SESSION_IDS_BYTES = 2 * 1024 * 1024
+
class LangSmithRetryableError(Exception):
pass
@@ -323,6 +330,53 @@ def _fetch_page(
return json.loads(raw) if raw else None
+def _list_session_ids(
+ session: requests.Session,
+ headers: dict[str, str],
+ base_url: str,
+ logger: FilteringBoundLogger,
+) -> list[str]:
+ """Collect every tracing-project (session) id in the workspace.
+
+ runs/query rejects a request that doesn't scope to at least one of session/id/parent_run/
+ trace/reference_example; this sync pulls runs across every project, so it scopes by every
+ session id in the workspace instead of narrowing to one.
+ """
+ config = LANGSMITH_ENDPOINTS["projects"]
+ 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_SESSION_IDS_BYTES:
+ raise LangSmithResponseTooLargeError(
+ f"LangSmith returned an oversized set of tracing-project ids "
+ f"(> {MAX_SESSION_IDS_BYTES} bytes) while scoping the runs query"
+ )
+ if len(rows) < config.page_size:
+ break
+ offset += config.page_size
+ # Same hostile-host guard as the other paginators: a host returning a full page forever
+ # must not hold the worker until the activity timeout.
+ pages += 1
+ if pages >= MAX_PAGES_PER_RUN:
+ raise LangSmithPageLimitError(
+ f"LangSmith session listing hit the {MAX_PAGES_PER_RUN}-page limit while scoping the runs query"
+ )
+ return ids
+
+
def _get_runs_rows(
session: requests.Session,
headers: dict[str, str],
@@ -350,6 +404,13 @@ def _get_runs_rows(
start = _resolve_window_start(config, should_use_incremental_field, db_incremental_field_last_value)
window_start = _format_datetime(start) if start is not None else None
+ # runs/query requires at least one of session/id/parent_run/trace/reference_example in the
+ # body (a 400 otherwise) — there's no such thing as an unscoped query across a workspace.
+ session_ids = _list_session_ids(session, headers, base_url, logger)
+ if not session_ids:
+ logger.debug("LangSmith: no tracing projects in workspace, nothing to sync for runs")
+ return
+
body: dict[str, Any] = {
"limit": config.page_size,
"select": RUNS_SELECT_FIELDS,
@@ -357,6 +418,7 @@ def _get_runs_rows(
# window bound. The watermark still only persists at job end (sort_mode="desc") since we
# can't verify the ordering guarantee across every LangSmith deployment.
"order": "asc",
+ "session": session_ids,
}
if window_start:
body["start_time"] = window_start
diff --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.py
index 5ac8a8e06b5a..12c7bb075118 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
@@ -66,6 +66,21 @@ def _run(run_id: str) -> dict[str, Any]:
return {"id": run_id, "start_time": "2026-06-01T00:00:00Z"}
+SESSION_IDS_PAGE = [{"id": "session-1"}]
+
+
+def _with_session_page(fake_fetch):
+ """Wrap a runs/query-only fake_fetch so the preceding sessions-listing GET (json_body=None)
+ is answered separately from the runs/query POSTs the wrapped function expects to see."""
+
+ def wrapped(session, url, headers, log, json_body=None):
+ if json_body is None:
+ return SESSION_IDS_PAGE
+ return fake_fetch(session, url, headers, log, json_body=json_body)
+
+ return wrapped
+
+
class TestNormalizeBaseUrl:
@pytest.mark.parametrize(
"raw,expected",
@@ -146,7 +161,7 @@ class TestRunsPagination:
bodies.append(json_body)
return pages[len(bodies) - 1]
- with mock.patch(_FETCH_PAGE, side_effect=fake_fetch):
+ with mock.patch(_FETCH_PAGE, side_effect=_with_session_page(fake_fetch)):
rows = _collect(
get_rows("key", BASE_URL, "runs", logger, manager, 1, True, watermark) # type: ignore[arg-type]
)
@@ -164,12 +179,45 @@ class TestRunsPagination:
def test_terminates_on_empty_page_and_missing_cursors(self):
manager = FakeManager()
- with mock.patch(_FETCH_PAGE, return_value={"runs": [], "cursors": {"next": "dangling"}}) as fetch:
+ def fake_fetch(session, url, headers, log, json_body=None):
+ return {"runs": [], "cursors": {"next": "dangling"}}
+
+ with mock.patch(_FETCH_PAGE, side_effect=_with_session_page(fake_fetch)) as fetch:
+ rows = _collect(get_rows("key", BASE_URL, "runs", logger, manager, 1)) # type: ignore[arg-type]
+
+ assert rows == []
+ # One call to list sessions, one to runs/query.
+ assert fetch.call_count == 2
+
+ def test_no_sessions_in_workspace_skips_runs_query_entirely(self):
+ # runs/query requires a session/id/parent_run/trace/reference_example scope; with no
+ # tracing projects there's nothing to scope to, so the sync must not call it at all
+ # (an empty `session: []` would 400 the same way the unscoped call used to).
+ manager = FakeManager()
+
+ with mock.patch(_FETCH_PAGE, return_value=[]) as fetch:
rows = _collect(get_rows("key", BASE_URL, "runs", logger, manager, 1)) # type: ignore[arg-type]
assert rows == []
assert fetch.call_count == 1
+ def test_runs_query_is_always_scoped_to_a_session(self):
+ # Regression test: runs/query used to send limit/select/order (and start_time) with no
+ # session/id/parent_run/trace/reference_example, which LangSmith rejects with a 400
+ # ("At least one of 'session', 'id', 'parent_run', 'trace' or 'reference_example' must be
+ # specified"). Every request must carry the workspace's session ids.
+ manager = FakeManager()
+ bodies: list[dict[str, Any]] = []
+
+ def fake_fetch(session, url, headers, log, json_body=None):
+ bodies.append(json_body)
+ return {"runs": [_run("a")], "cursors": {"next": None}}
+
+ with mock.patch(_FETCH_PAGE, side_effect=_with_session_page(fake_fetch)):
+ _collect(get_rows("key", BASE_URL, "runs", logger, manager, 1)) # type: ignore[arg-type]
+
+ assert bodies[0]["session"] == [row["id"] for row in SESSION_IDS_PAGE]
+
def test_resume_pins_cursor_and_window(self):
manager = FakeManager(resume=LangSmithResumeConfig(cursor="c9", window_start="2026-01-01T00:00:00.000000Z"))
bodies: list[dict[str, Any]] = []
@@ -178,7 +226,7 @@ class TestRunsPagination:
bodies.append(json_body)
return {"runs": [_run("x")], "cursors": {"next": None}}
- with mock.patch(_FETCH_PAGE, side_effect=fake_fetch):
+ with mock.patch(_FETCH_PAGE, side_effect=_with_session_page(fake_fetch)):
_collect(get_rows("key", BASE_URL, "runs", logger, manager, 1, True, datetime.now(UTC))) # type: ignore[arg-type]
# The interrupted run's window is replayed, not recomputed from the new watermark — a
@@ -193,7 +241,7 @@ class TestRunsPagination:
big_page = {"runs": [_run(f"r{i}") for i in range(2500)], "cursors": {"next": "c2"}}
last_page = {"runs": [_run("tail")], "cursors": {"next": None}}
- with mock.patch(_FETCH_PAGE, side_effect=[big_page, last_page]):
+ with mock.patch(_FETCH_PAGE, side_effect=[SESSION_IDS_PAGE, big_page, last_page]):
rows = _collect(get_rows("key", BASE_URL, "runs", logger, manager, 1)) # type: ignore[arg-type]
assert len(rows) == 2501
@@ -248,6 +296,18 @@ class TestOffsetPagination:
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
+ # query; without a cumulative cap, accumulating every id would let a hostile host exhaust a
+ # shared worker's memory well before MAX_PAGES_PER_RUN ever trips.
+ manager = FakeManager()
+ huge_id = "x" * 50_000
+ page_size = LANGSMITH_ENDPOINTS["projects"].page_size
+
+ with mock.patch(_FETCH_PAGE, return_value=[{"id": huge_id} for _ in range(page_size)]):
+ with pytest.raises(LangSmithResponseTooLargeError):
+ _collect(get_rows("key", BASE_URL, "runs", logger, manager, 1)) # type: ignore[arg-type]
+
def test_repeated_runs_cursor_raises(self):
# A host that hands back a cursor it already gave us would loop forever; the walk must bail
# instead of spinning until the week-long activity timeout.
@@ -256,7 +316,7 @@ class TestPaginationAbuseGuards:
def fake_fetch(session, url, headers, log, json_body=None):
return {"runs": [_run("a")], "cursors": {"next": "loop"}}
- with mock.patch(_FETCH_PAGE, side_effect=fake_fetch):
+ with mock.patch(_FETCH_PAGE, side_effect=_with_session_page(fake_fetch)):
with pytest.raises(LangSmithRepeatedCursorError):
_collect(get_rows("key", BASE_URL, "runs", logger, manager, 1)) # type: ignore[arg-type]
@@ -269,7 +329,7 @@ class TestPaginationAbuseGuards:
def fake_fetch(session, url, headers, log, json_body=None):
return {"runs": [_run("a")], "cursors": {"next": big_cursor}}
- with mock.patch(_FETCH_PAGE, side_effect=fake_fetch):
+ with mock.patch(_FETCH_PAGE, side_effect=_with_session_page(fake_fetch)):
with pytest.raises(LangSmithResponseTooLargeError):
_collect(get_rows("key", BASE_URL, "runs", logger, manager, 1)) # type: ignore[arg-type]
@@ -286,7 +346,7 @@ class TestPaginationAbuseGuards:
return {"runs": [_run(f"r{counter['n']}")], "cursors": {"next": f"c{counter['n']}"}}
rows: list[dict[str, Any]] = []
- with mock.patch(_MAX_PAGES, 3), mock.patch(_FETCH_PAGE, side_effect=fake_fetch):
+ with mock.patch(_MAX_PAGES, 3), mock.patch(_FETCH_PAGE, side_effect=_with_session_page(fake_fetch)):
with pytest.raises(LangSmithPageLimitError):
for table in get_rows("key", BASE_URL, "runs", logger, manager, 1): # type: ignore[arg-type]
rows.extend(table.to_pylist())