From 988217b34cae7d1ca8681c93165e50084034fadc Mon Sep 17 00:00:00 2001 From: William FH <13333726+hinthornw@users.noreply.github.com> Date: Tue, 3 Sep 2024 09:01:46 -0700 Subject: [PATCH] Filter child runs where trace has been filtered (#964) --- js/package.json | 2 +- js/src/client.ts | 21 +++++++++++---------- js/src/index.ts | 2 +- python/langsmith/client.py | 19 ++++++++++++------- python/pyproject.toml | 2 +- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/js/package.json b/js/package.json index e9681e5ae..c3ff5e32d 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "langsmith", - "version": "0.1.49", + "version": "0.1.50", "description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.", "packageManager": "yarn@1.22.19", "files": [ diff --git a/js/src/client.ts b/js/src/client.ts index 484cd91d5..4d7538bda 100644 --- a/js/src/client.ts +++ b/js/src/client.ts @@ -400,7 +400,7 @@ export class Client { private tracingSampleRate?: number; - private sampledPostUuids = new Set(); + private filteredPostUuids = new Set(); private autoBatchTracing = true; @@ -648,23 +648,24 @@ export class Client { if (patch) { const sampled = []; for (const run of runs) { - if (this.sampledPostUuids.has(run.id)) { + if (!this.filteredPostUuids.has(run.id)) { sampled.push(run); - this.sampledPostUuids.delete(run.id); + } else { + this.filteredPostUuids.delete(run.id); } } return sampled; } else { const sampled = []; for (const run of runs) { - if (run.id !== run.trace_id) { - sampled.push(run); - this.sampledPostUuids.add(run.id); - continue; - } - if (Math.random() < this.tracingSampleRate) { + if ( + (run.id !== run.trace_id && + !this.filteredPostUuids.has(run.trace_id)) || + Math.random() < this.tracingSampleRate + ) { sampled.push(run); - this.sampledPostUuids.add(run.id); + } else { + this.filteredPostUuids.add(run.id); } } return sampled; diff --git a/js/src/index.ts b/js/src/index.ts index 6e930d3df..bccaa0015 100644 --- a/js/src/index.ts +++ b/js/src/index.ts @@ -12,4 +12,4 @@ export type { export { RunTree, type RunTreeConfig } from "./run_trees.js"; // Update using yarn bump-version -export const __version__ = "0.1.49"; +export const __version__ = "0.1.50"; diff --git a/python/langsmith/client.py b/python/langsmith/client.py index 077a1cfca..4c90ced1c 100644 --- a/python/langsmith/client.py +++ b/python/langsmith/client.py @@ -439,7 +439,7 @@ class Client: "_web_url", "_tenant_id", "tracing_sample_rate", - "_sampled_post_uuids", + "_filtered_post_uuids", "tracing_queue", "_anonymizer", "_hide_inputs", @@ -524,7 +524,7 @@ def __init__( ) self.tracing_sample_rate = _get_tracing_sampling_rate() - self._sampled_post_uuids: set[uuid.UUID] = set() + self._filtered_post_uuids: set[uuid.UUID] = set() self._write_api_urls: Mapping[str, Optional[str]] = _get_write_api_urls( api_urls ) @@ -1175,19 +1175,24 @@ def _filter_for_sampling( sampled = [] for run in runs: run_id = _as_uuid(run["id"]) - if run_id in self._sampled_post_uuids: + if run_id not in self._filtered_post_uuids: sampled.append(run) - self._sampled_post_uuids.remove(run_id) + else: + self._filtered_post_uuids.remove(run_id) return sampled else: sampled = [] for run in runs: if ( + # Child run run["id"] != run.get("trace_id") - or random.random() < self.tracing_sample_rate - ): + # Whose trace is included + and run.get("trace_id") not in self._filtered_post_uuids + # Or a root that's randomly sampled + ) or random.random() < self.tracing_sample_rate: sampled.append(run) - self._sampled_post_uuids.add(_as_uuid(run["id"])) + else: + self._filtered_post_uuids.add(_as_uuid(run["id"])) return sampled def create_run( diff --git a/python/pyproject.toml b/python/pyproject.toml index 9b7dd9e26..1aeb0c944 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "langsmith" -version = "0.1.108" +version = "0.1.109" description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform." authors = ["LangChain "] license = "MIT"