Skip to content

Commit

Permalink
python[patch]: write to same experiment pytest-xdist (#1367)
Browse files Browse the repository at this point in the history
  • Loading branch information
baskaryan authored Jan 3, 2025
1 parent af0b39d commit a949edf
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions python/langsmith/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import atexit
import datetime
import functools
import importlib
import inspect
import logging
import os
import threading
import uuid
import warnings
Expand Down Expand Up @@ -270,10 +272,21 @@ def wrapper(*test_args: Any, **test_kwargs: Any):
## Private functions


def _get_experiment_name() -> str:
# TODO Make more easily configurable
prefix = ls_utils.get_tracer_project(False) or "TestSuiteResult"
name = f"{prefix}:{uuid.uuid4().hex[:8]}"
def _get_experiment_name(test_suite_name: str) -> str:
# If this is a pytest-xdist multi-process run then we need to create the same
# experiment name across processes. We can do this by accessing the
# PYTEST_XDIST_TESTRUNID env var.
if os.environ.get("PYTEST_XDIST_TESTRUNUID") and importlib.util.find_spec("xdist"):
id_name = test_suite_name + os.environ["PYTEST_XDIST_TESTRUNUID"]
id_ = str(uuid.uuid5(uuid.NAMESPACE_DNS, id_name).hex[:8])
else:
id_ = str(uuid.uuid4().hex[:8])

if os.environ.get("LANGSMITH_EXPERIMENT"):
prefix = os.environ["LANGSMITH_EXPERIMENT"]
else:
prefix = ls_utils.get_tracer_project(False) or "TestSuiteResult"
name = f"{prefix}:{id_}"
return name


Expand Down Expand Up @@ -311,7 +324,7 @@ def _start_experiment(
client: ls_client.Client,
test_suite: ls_schemas.Dataset,
) -> ls_schemas.TracerSession:
experiment_name = _get_experiment_name()
experiment_name = _get_experiment_name(test_suite.name)
try:
return client.create_project(
experiment_name,
Expand Down

0 comments on commit a949edf

Please sign in to comment.