fix(f173c903-1467-4c00-aa7f-bcf48007d079): attach CI LangSmith metadata before pytest runs

This commit is contained in:
issues-agent
2026-06-16 17:56:49 +00:00
parent 847312e0aa
commit 733eb92b06
2 changed files with 24 additions and 10 deletions

View File

@@ -23,7 +23,6 @@ import sys
import warnings
from typing import TYPE_CHECKING, Any
import pytest
from langsmith.run_helpers import tracing_context
if TYPE_CHECKING:
@@ -84,8 +83,22 @@ def _langsmith_ci_cm() -> Iterator[None]:
yield
@pytest.fixture(scope="session", autouse=True)
def _langsmith_ci_context() -> Iterator[None]:
"""Apply `LANGSMITH_TAGS`/`LANGSMITH_METADATA` to traces in this session."""
with _langsmith_ci_cm():
yield
_langsmith_ci_context: contextlib.ExitStack | None = None
def pytest_configure() -> None:
"""Apply `LANGSMITH_TAGS`/`LANGSMITH_METADATA` to traces."""
global _langsmith_ci_context # noqa: PLW0603
if _langsmith_ci_context is None:
_langsmith_ci_context = contextlib.ExitStack()
_langsmith_ci_context.enter_context(_langsmith_ci_cm())
def pytest_unconfigure() -> None:
"""Restore the previous LangSmith tracing context."""
global _langsmith_ci_context # noqa: PLW0603
if _langsmith_ci_context is not None:
_langsmith_ci_context.close()
_langsmith_ci_context = None

View File

@@ -129,22 +129,23 @@ class TestContextManager:
class TestPluginDiscovery:
"""End-to-end: the `pytest11` entry point wires up the autouse fixture."""
"""End-to-end: the `pytest11` entry point wires up the plugin hooks."""
def test_autouse_fixture_applies_env_in_subprocess(
def test_plugin_hooks_apply_env_in_subprocess(
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("GITHUB_ACTIONS", "true")
monkeypatch.setenv("LANGSMITH_TAGS", "discovered,from-entrypoint")
monkeypatch.delenv("LANGSMITH_METADATA", raising=False)
pytester.makepyfile(
pytester.makeconftest(
dedent("""
from langsmith.run_helpers import get_tracing_context
def test_tags_visible_via_autouse_fixture():
def pytest_runtest_setup(item):
ctx = get_tracing_context()
assert ctx["tags"] == ["discovered", "from-entrypoint"]
"""),
)
pytester.makepyfile("def test_tags_visible_before_test_setup(): pass")
result = pytester.runpytest_subprocess("-q")
result.assert_outcomes(passed=1)