Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/langsmith/run_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,11 +1798,17 @@ def _setup_run(
run_tree_kwargs["id"] = ls_client._ensure_uuid(id_)
new_run = run_trees.RunTree(**cast(Any, run_tree_kwargs))
# Post run if enabled=True (force) or if tracing is enabled globally
if enabled is True or utils.tracing_is_enabled() is True:
tracing_enabled = utils.tracing_is_enabled()
if enabled is True or tracing_enabled is True:
try:
new_run.post()
except BaseException as e:
LOGGER.error(f"Failed to post run {new_run.id}: {e}")
elif _context._TRACING_ENABLED.get() is None:
# The run is not sent but is still the current run for its children,
# which would send themselves under a parent that never arrives.
# Give them the answer this run got.
context.run(_context._TRACING_ENABLED.set, tracing_enabled)
response_container = _TraceableContainer(
new_run=new_run,
project_name=selected_project,
Expand Down
20 changes: 20 additions & 0 deletions tests/unit_tests/test_run_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,26 @@ def my_run(foo: str):
assert len(mock_calls) == 0


@pytest.mark.parametrize("parent_kind", ["run_tree", "headers"])
def test_children_not_sent_when_traceable_parent(parent_kind: str):
mock_client = _get_mock_client()
rt = RunTree(name="foo", client=mock_client)
parent = rt if parent_kind == "run_tree" else rt.to_headers()

@traceable(client=mock_client)
def child():
return "baz"

@traceable(client=mock_client)
def my_run():
return child()

my_run(langsmith_extra={"parent": parent})
mock_client.flush()
# Neither run is sent: `child` would point at a `my_run` that never was.
assert len(_get_calls(mock_client)) == 0


def test_client_passed_when_trace_parent():
mock_client = _get_mock_client()
rt = RunTree(name="foo", client=mock_client)
Expand Down