litellm/tests/unit/integrations/test_langsmith_init.py
yuneng-jiang cf491d1df9
test: move tests/test_litellm integrations and secret_managers into tests/unit (#43194)
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: move tests/test_litellm/llms into tests/unit/llms

Rename-only. Moves the provider tests and the fine-tuning fixtures they
load, mirroring the old paths. Follow-up commits merge, split and wire them.

* test: merge, split and prune the moved llms tests

Merges the Databricks chat transformation tests into the existing unit
file, keeps the tests that need real keys or the network in
tests/test_litellm, deletes the audited tests a stronger unit test
already covers, and points imports at tests.unit.llms.

* ci: run the moved llms tests under their legacy flags

The Vertex AI and All Other Providers shards keep their legacy test-path
for the retained files and add the llm-vertex-ai and llm-other-providers
unit selections. CircleCI gets matching unit jobs.

* test: make the tests/unit/llms directories packages

Adds __init__.py to the moved dirs and drops the legacy ones whose
directories no longer hold tests.

* test: drop script runners and path hacks the llms split left dangling

The __main__ runners in the split openai_like files and the Databricks e2e
runner called tests that now live in the other half of the split or were
deleted. The retained legacy halves also no longer need sys.path edits.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

* test: move tests/test_litellm integrations and secret_managers into tests/unit

Rename-only. Mirrors the old paths, including the directory conftests
and the prompt and JSON fixtures. Follow-up commits prune and wire them.

* test: prune and repoint the moved integrations tests

Deletes the 7 audited tests a stronger test in the same tree already
covers, imports the TLS sink helpers from their new conftest path, and
restores os.environ after each integrations test. Some presets write
OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the
legacy tree's test ordering that header leaked into the AgentOps tests.

* ci: run the moved integrations tests under their legacy flag

The integrations GHA shard and a new CircleCI job run the integrations
unit selection. secret_managers joins the misc selection.

* docs: point integrations and secret_managers references at tests/unit

* test: make the moved integrations directories packages

* test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path

The Databricks e2e file is a manual script whose main() calls the tests
that were pruned, so pruning them broke the documented run. It is back to
its main version. The SageMaker Nova docstring now points at the file's
real location in tests/local_testing.

* test: keep the job's UNIT_FLAG out of the shard-script tests

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 12:57:07 -07:00

693 lines
27 KiB
Python

import asyncio
import json
import os
from datetime import datetime, timezone
from decimal import Decimal
from typing import Final
from unittest.mock import AsyncMock, MagicMock, patch
import httpx
import pytest
import litellm
from litellm.integrations.langsmith import LangsmithLogger
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
from litellm.types.integrations.langsmith import LangsmithQueueObject
@pytest.fixture
def reset_redact_flag():
"""Reset redact_user_api_key_info between tests so global state doesn't leak."""
original = litellm.redact_user_api_key_info
yield
litellm.redact_user_api_key_info = original
class TestLangsmithLoggerInit:
"""Test cases for LangSmith logger initialization, particularly sampling rate handling.
These tests verify that the sampling_rate attribute is set during initialization.
Note: The current implementation has some edge cases in the sampling rate logic.
"""
@patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "1"}, clear=False)
def test_langsmith_sampling_rate_parameter_respected_with_valid_env(self):
"""Test that langsmith_sampling_rate parameter is properly set when env var condition is met."""
sampling_rate = 0.5
logger = LangsmithLogger(
langsmith_api_key="test-key",
langsmith_project="test-project",
langsmith_sampling_rate=sampling_rate,
)
assert (
logger.sampling_rate == sampling_rate
), f"Expected sampling_rate to be {sampling_rate}, got {logger.sampling_rate}"
@patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "1"}, clear=False)
def test_langsmith_sampling_rate_zero_parameter_falls_back_to_env(self):
"""Test that 0.0 parameter falls back to env var due to falsy value."""
logger = LangsmithLogger(
langsmith_api_key="test-key",
langsmith_project="test-project",
langsmith_sampling_rate=0.0,
)
assert (
logger.sampling_rate == 1.0
), f"Expected sampling_rate to fall back to 1.0 from env, got {logger.sampling_rate}"
@patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "1"}, clear=False)
def test_langsmith_sampling_rate_from_integer_env_var(self):
"""Test that sampling rate uses environment variable when parameter not provided and env var is integer."""
logger = LangsmithLogger(
langsmith_api_key="test-key", langsmith_project="test-project"
)
assert (
logger.sampling_rate == 1.0
), f"Expected sampling_rate to be 1.0 from env var, got {logger.sampling_rate}"
@patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "0.8"}, clear=False)
def test_langsmith_sampling_rate_decimal_env_var_ignored(self):
"""Test that decimal environment variables are ignored due to isdigit() check."""
logger = LangsmithLogger(
langsmith_api_key="test-key", langsmith_project="test-project"
)
assert (
logger.sampling_rate == 1.0
), f"Expected sampling_rate to default to 1.0 (decimal env ignored), got {logger.sampling_rate}"
@patch.dict(os.environ, {}, clear=True)
def test_langsmith_sampling_rate_default_value(self):
"""Test that sampling rate defaults to 1.0 when no parameter or env var provided."""
logger = LangsmithLogger(
langsmith_api_key="test-key", langsmith_project="test-project"
)
assert (
logger.sampling_rate == 1.0
), f"Expected default sampling_rate to be 1.0, got {logger.sampling_rate}"
@patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "invalid"}, clear=False)
def test_langsmith_sampling_rate_invalid_env_var_defaults(self):
"""Test that invalid environment variable falls back to default value."""
logger = LangsmithLogger(
langsmith_api_key="test-key", langsmith_project="test-project"
)
assert (
logger.sampling_rate == 1.0
), f"Expected sampling_rate to default to 1.0 with invalid env var, got {logger.sampling_rate}"
@patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": ""}, clear=False)
def test_langsmith_sampling_rate_empty_env_var_defaults(self):
"""Test that empty environment variable falls back to default value."""
logger = LangsmithLogger(
langsmith_api_key="test-key", langsmith_project="test-project"
)
assert (
logger.sampling_rate == 1.0
), f"Expected sampling_rate to default to 1.0 with empty env var, got {logger.sampling_rate}"
def test_langsmith_sampling_rate_attribute_exists(self):
"""Test that the sampling_rate attribute is always set on the logger instance."""
logger = LangsmithLogger(
langsmith_api_key="test-key", langsmith_project="test-project"
)
assert hasattr(
logger, "sampling_rate"
), "LangsmithLogger should have sampling_rate attribute"
assert isinstance(
logger.sampling_rate, float
), f"sampling_rate should be a float, got {type(logger.sampling_rate)}"
assert (
logger.sampling_rate >= 0.0
), f"sampling_rate should be non-negative, got {logger.sampling_rate}"
@patch.object(LangsmithLogger, "_start_periodic_flush_task", return_value=None)
def test_langsmith_init_skips_periodic_flush_without_running_loop(
self, mock_start_periodic_flush_task
):
"""Test that sync initialization leaves the periodic flush task unset."""
logger = LangsmithLogger(
langsmith_api_key="test-key", langsmith_project="test-project"
)
assert logger is not None
mock_start_periodic_flush_task.assert_called_once()
assert logger._flush_task is None
@patch(
"asyncio.get_running_loop", side_effect=RuntimeError("no running event loop")
)
def test_start_periodic_flush_task_returns_none_without_running_loop(
self, mock_get_running_loop
):
"""Test that helper returns None when no running event loop exists."""
with patch.object(
LangsmithLogger, "_start_periodic_flush_task", return_value=None
):
logger = LangsmithLogger(
langsmith_api_key="test-key",
langsmith_project="test-project",
)
mock_get_running_loop.reset_mock()
assert logger._start_periodic_flush_task() is None
mock_get_running_loop.assert_called_once()
@pytest.mark.asyncio
async def test_langsmith_init_starts_periodic_flush_with_running_loop(self):
"""Test that init schedules periodic flush when a running loop exists."""
logger = LangsmithLogger(
langsmith_api_key="test-key", langsmith_project="test-project", flush_interval=0.01
)
batch_sent = asyncio.Event()
logger.async_send_batch = AsyncMock(side_effect=batch_sent.set)
logger.log_queue.append({"id": "run-id"})
flush_task = logger._flush_task
assert isinstance(flush_task, asyncio.Task)
await asyncio.wait_for(batch_sent.wait(), timeout=5)
flush_task.cancel()
with pytest.raises(asyncio.CancelledError):
await flush_task
@pytest.mark.asyncio
async def test_async_log_success_event_lazily_starts_periodic_flush(self):
"""Test that async logging lazily starts periodic flush after sync init."""
with patch.object(
LangsmithLogger, "_start_periodic_flush_task", return_value=None
):
logger = LangsmithLogger(
langsmith_api_key="test-key",
langsmith_project="test-project",
)
logger._get_sampling_rate_to_use_for_request = MagicMock(return_value=1.0)
logger._get_credentials_to_use_for_request = MagicMock(
return_value=logger.default_credentials
)
logger._prepare_log_data = MagicMock(return_value={"id": "run-id"})
logger._start_periodic_flush_task = MagicMock(return_value=MagicMock())
await logger.async_log_success_event({}, {}, None, None)
logger._start_periodic_flush_task.assert_called_once()
assert len(logger.log_queue) == 1
@pytest.mark.asyncio
async def test_async_log_failure_event_lazily_starts_periodic_flush(self):
"""Test that async failure logging lazily starts periodic flush after sync init."""
with patch.object(
LangsmithLogger, "_start_periodic_flush_task", return_value=None
):
logger = LangsmithLogger(
langsmith_api_key="test-key",
langsmith_project="test-project",
)
logger._get_sampling_rate_to_use_for_request = MagicMock(return_value=1.0)
logger._get_credentials_to_use_for_request = MagicMock(
return_value=logger.default_credentials
)
logger._prepare_log_data = MagicMock(return_value={"id": "run-id"})
logger._start_periodic_flush_task = MagicMock(return_value=MagicMock())
await logger.async_log_failure_event({}, {}, None, None)
logger._start_periodic_flush_task.assert_called_once()
assert len(logger.log_queue) == 1
class TestLangsmithBatchSerialization:
async def _logger(self, transport_handler, tenant_id=None):
logger = LangsmithLogger(
langsmith_api_key="test-key",
langsmith_project="test-project",
langsmith_base_url="https://api.smith.langchain.com",
langsmith_tenant_id=tenant_id,
)
if logger._flush_task is not None:
logger._flush_task.cancel()
handler = AsyncHTTPHandler()
await handler.client.aclose()
handler.client = httpx.AsyncClient(
transport=httpx.MockTransport(transport_handler)
)
logger.async_httpx_client = handler
return logger
@staticmethod
def _capturing_transport(captured):
async def handle(request: httpx.Request) -> httpx.Response:
captured.append(request)
return httpx.Response(200, request=request, json={"ok": True})
return handle
def _queue(self, logger, extra):
return [
LangsmithQueueObject(
data={"id": "run-1", "name": "LLMRun", "extra": extra},
credentials=logger.default_credentials,
)
]
@pytest.mark.asyncio
async def test_datetime_and_decimal_metadata_reach_langsmith_as_strings(self):
captured: list[httpx.Request] = [] # mutable-ok: transport capture buffer
logger = await self._logger(self._capturing_transport(captured))
logger.log_queue = self._queue(
logger,
{
"created_at": datetime(2026, 1, 2, 3, 4, 5, tzinfo=timezone.utc),
"spend": Decimal("0.0042"),
},
)
await logger.async_send_batch()
assert len(captured) == 1, "batch was dropped instead of being sent"
body = json.loads(captured[0].content)
assert body["post"][0]["extra"] == {
"created_at": "2026-01-02 03:04:05+00:00",
"spend": "0.0042",
}
await logger.async_httpx_client.client.aclose()
@pytest.mark.asyncio
async def test_nan_metadata_is_dropped_instead_of_shipping_invalid_json(self):
captured: list[httpx.Request] = [] # mutable-ok: transport capture buffer
logger = await self._logger(self._capturing_transport(captured))
logger.log_queue = self._queue(logger, {"score": float("nan")})
await logger.async_send_batch()
assert captured == [], (
"nan metadata must abort the batch: a bare NaN token is invalid JSON and LangSmith rejects it"
)
await logger.async_httpx_client.client.aclose()
@pytest.mark.asyncio
async def test_batch_declares_json_content_type(self):
captured: list[httpx.Request] = [] # mutable-ok: transport capture buffer
logger = await self._logger(self._capturing_transport(captured))
logger.log_queue = self._queue(logger, {"model": "gpt-4.1-mini"})
await logger.async_send_batch()
assert captured[0].headers["content-type"] == "application/json", (
"a content= body carries no implicit content type; LangSmith refuses it without this header"
)
assert captured[0].url.path.endswith("/api/v1/runs/batch")
assert captured[0].headers["x-api-key"] == "test-key"
assert "x-tenant-id" not in captured[0].headers
await logger.async_httpx_client.client.aclose()
@pytest.mark.asyncio
async def test_tenant_id_is_forwarded_on_the_batch_request(self):
captured: list[httpx.Request] = [] # mutable-ok: transport capture buffer
logger = await self._logger(
self._capturing_transport(captured), tenant_id="tenant-1"
)
logger.log_queue = self._queue(logger, {"model": "gpt-4.1-mini"})
await logger.async_send_batch()
assert captured[0].headers["x-tenant-id"] == "tenant-1"
await logger.async_httpx_client.client.aclose()
@pytest.mark.asyncio
async def test_langsmith_error_response_does_not_propagate(self):
captured: list[httpx.Request] = [] # mutable-ok: transport capture buffer
async def reject(request: httpx.Request) -> httpx.Response:
captured.append(request)
return httpx.Response(422, request=request, text="bad run")
logger = await self._logger(reject)
logger.log_queue = self._queue(logger, {"model": "gpt-4.1-mini"})
await logger.async_send_batch()
assert len(captured) == 1, "the batch never left the process"
await logger.async_httpx_client.client.aclose()
class TestLangsmithPrepareLogData:
"""Regression test for #24001: _prepare_log_data must inject
usage_metadata into outputs so LangSmith's Cost column is populated."""
@patch("asyncio.create_task")
@patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "1"}, clear=False)
def test_outputs_contain_usage_metadata(self, mock_create_task):
logger = LangsmithLogger(
langsmith_api_key="test-key",
langsmith_project="test-project",
)
payload = {
"id": "test-id",
"response": {"choices": [{"message": {"content": "hi"}}]},
"metadata": {},
"startTime": 1.0,
"endTime": 2.0,
"request_tags": [],
"error_str": None,
"status": "success",
"response_cost": 0.0042,
"prompt_tokens": 100,
"completion_tokens": 50,
"total_tokens": 150,
}
kwargs = {
"litellm_params": {"metadata": {}},
"standard_logging_object": payload,
}
credentials = {
"LANGSMITH_API_KEY": "test-key",
"LANGSMITH_PROJECT": "test-project",
"LANGSMITH_BASE_URL": "https://api.smith.langchain.com",
}
data = logger._prepare_log_data(
kwargs=kwargs,
response_obj=None,
start_time=1.0,
end_time=2.0,
credentials=credentials,
)
assert "usage_metadata" in data["outputs"]
um = data["outputs"]["usage_metadata"]
assert um["total_cost"] == 0.0042
assert um["input_tokens"] == 100
assert um["output_tokens"] == 50
assert um["total_tokens"] == 150
class TestLangsmithRedactUserApiKeyInfo:
"""Verify litellm.redact_user_api_key_info is honored for LangSmith."""
def _logger(self):
return LangsmithLogger(
langsmith_api_key="test-key",
langsmith_project="test-project",
)
def _metadata_with_user_api_key_fields(self):
return {
"user_api_key_hash": "abc123",
"user_api_key_alias": "engineer-key",
"user_api_key_user_id": "default_user_id",
"user_api_key_team_id": "team-uuid",
"user_api_key_team_alias": "GNT",
"user_api_key_request_route": "/chat/completions",
"user_api_key_spend": 1.64,
"model": "gpt-4",
"requester_metadata": {
"user_api_key_team_id": "team-uuid",
"user_api_key_user_id": "default_user_id",
"session_id": "sess-1",
},
}
def test_redact_disabled_keeps_user_api_key_fields(self, reset_redact_flag):
"""Flag off: user_api_key_* fields are preserved (no behavior change)."""
litellm.redact_user_api_key_info = False
logger = self._logger()
metadata = self._metadata_with_user_api_key_fields()
extra = logger._build_extra_metadata(metadata)
assert extra["user_api_key_hash"] == "abc123"
assert extra["user_api_key_team_id"] == "team-uuid"
assert extra["requester_metadata"]["user_api_key_user_id"] == "default_user_id"
def test_redact_enabled_strips_top_level_user_api_key_fields(
self, reset_redact_flag
):
"""Flag on: top-level user_api_key_* keys removed; other keys preserved."""
litellm.redact_user_api_key_info = True
logger = self._logger()
metadata = self._metadata_with_user_api_key_fields()
extra = logger._build_extra_metadata(metadata)
for key in (
"user_api_key_hash",
"user_api_key_alias",
"user_api_key_user_id",
"user_api_key_team_id",
"user_api_key_team_alias",
"user_api_key_request_route",
"user_api_key_spend",
):
assert key not in extra, f"{key} should be redacted at top level"
assert extra["model"] == "gpt-4"
def test_redact_enabled_strips_nested_requester_metadata(self, reset_redact_flag):
"""Flag on: nested requester_metadata.user_api_key_* removed; session_id still lifted."""
litellm.redact_user_api_key_info = True
logger = self._logger()
metadata = self._metadata_with_user_api_key_fields()
extra = logger._build_extra_metadata(metadata)
nested = extra["requester_metadata"]
assert "user_api_key_team_id" not in nested
assert "user_api_key_user_id" not in nested
assert nested["session_id"] == "sess-1"
assert extra["session_id"] == "sess-1"
def test_redact_enabled_strips_user_api_key_info_from_inputs(self, reset_redact_flag):
"""
Regression (LIT-4306): `inputs` is the whole StandardLoggingPayload, so
`redact_user_api_key_info` has to cover `inputs.metadata` the same way it
covers `extra` - including the nested `requester_metadata` copy. Before
the fix `extra` was redacted and `inputs` shipped every user_api_key_*
field verbatim.
"""
litellm.redact_user_api_key_info = True
logger = self._logger()
metadata = self._metadata_with_user_api_key_fields()
metadata["user_api_key_auth_metadata"] = {"priority": "high"}
payload = {
"id": "run-1",
"response": {"choices": []},
"metadata": metadata,
"startTime": 1.0,
"endTime": 2.0,
"request_tags": [],
"error_str": None,
"status": "success",
"response_cost": 0.0,
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2,
}
credentials = {
"LANGSMITH_API_KEY": "test-key",
"LANGSMITH_PROJECT": "test-project",
"LANGSMITH_BASE_URL": "https://api.smith.langchain.com",
}
data = logger._prepare_log_data(
kwargs={"litellm_params": {"metadata": metadata}, "standard_logging_object": payload},
response_obj=None,
start_time=1.0,
end_time=2.0,
credentials=credentials,
)
inputs_metadata = data["inputs"]["metadata"]
assert [k for k in inputs_metadata if k.startswith("user_api_key")] == []
assert [k for k in inputs_metadata["requester_metadata"] if k.startswith("user_api_key")] == []
# inputs and extra must agree - they go through the same redaction now
assert [k for k in data["extra"] if k.startswith("user_api_key")] == []
# non-identity payload is untouched
assert inputs_metadata["model"] == "gpt-4"
assert inputs_metadata["requester_metadata"]["session_id"] == "sess-1"
assert data["inputs"]["total_tokens"] == 2
# the shared standard_logging_object other loggers read is not mutated
assert "user_api_key_hash" in payload["metadata"]
assert "user_api_key_user_id" in payload["metadata"]["requester_metadata"]
def test_redact_disabled_keeps_user_api_key_info_in_inputs(self, reset_redact_flag):
"""Flag off: the identity fields stay. The flag governs them, not this fix."""
litellm.redact_user_api_key_info = False
logger = self._logger()
metadata = self._metadata_with_user_api_key_fields()
payload = {
"id": "run-1",
"response": {"choices": []},
"metadata": metadata,
"startTime": 1.0,
"endTime": 2.0,
"request_tags": [],
"error_str": None,
"status": "success",
"response_cost": 0.0,
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2,
}
data = logger._prepare_log_data(
kwargs={"litellm_params": {"metadata": metadata}, "standard_logging_object": payload},
response_obj=None,
start_time=1.0,
end_time=2.0,
credentials={
"LANGSMITH_API_KEY": "test-key",
"LANGSMITH_PROJECT": "test-project",
"LANGSMITH_BASE_URL": "https://api.smith.langchain.com",
},
)
assert data["inputs"]["metadata"]["user_api_key_hash"] == "abc123"
class TestLangsmithRootRunIdConsistency:
"""Regression tests for LIT-5878 / #37269.
A request that carries a session/trace header (e.g. x-claude-code-session-id)
fans the header value out into litellm metadata as both trace_id and
session_id. LangSmith then rejected the whole ingest batch twice over:
a root run whose trace_id does not match the run id embedded in dotted_order
(400), and a run-body session_id that does not reference an existing tracer
session (404, or 422 for non-UUID values).
"""
def _prepare(self, request_metadata):
payload = {
"id": "slp-1",
"response": {"choices": []},
"metadata": {},
"startTime": 1.0,
"endTime": 2.0,
"request_tags": [],
"error_str": None,
"status": "success",
"response_cost": 0.0,
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2,
}
logger = LangsmithLogger(
langsmith_api_key="test-key",
langsmith_project="test-project",
)
return logger._prepare_log_data(
kwargs={
"litellm_params": {"metadata": request_metadata},
"standard_logging_object": payload,
},
response_obj=None,
start_time=1.0,
end_time=2.0,
credentials={
"LANGSMITH_API_KEY": "test-key",
"LANGSMITH_PROJECT": "test-project",
"LANGSMITH_BASE_URL": "https://api.smith.langchain.com",
},
)
def test_header_derived_ids_yield_self_consistent_root_run(self):
header_value = "ed29c3bb-44fa-4eec-9b7b-fecaa3e82d64"
data = self._prepare({"trace_id": header_value, "session_id": header_value})
assert data["trace_id"] == data["id"]
assert data["trace_id"] != header_value
assert data["dotted_order"].endswith(data["id"])
assert len(data["dotted_order"]) == 22 + len(data["id"])
assert "session_id" not in data
def test_distinct_session_id_is_still_forwarded(self):
data = self._prepare({"session_id": "11111111-2222-3333-4444-555555555555"})
assert data["session_id"] == "11111111-2222-3333-4444-555555555555"
def test_trace_id_only_root_run_is_overridden(self):
data = self._prepare({"trace_id": "ed29c3bb-44fa-4eec-9b7b-fecaa3e82d64"})
assert data["trace_id"] == data["id"]
assert data["trace_id"] != "ed29c3bb-44fa-4eec-9b7b-fecaa3e82d64"
assert data["dotted_order"].endswith(data["id"])
def test_root_run_without_caller_ids_is_self_consistent(self):
data = self._prepare({})
assert data["trace_id"] == data["id"]
assert data["dotted_order"].endswith(data["id"])
def test_child_run_keeps_caller_trace_id(self):
data = self._prepare(
{
"trace_id": "trace-1",
"parent_run_id": "parent-1",
"run_id": "child-1",
}
)
assert data["trace_id"] == "trace-1"
assert data["id"] == "child-1"
assert data["parent_run_id"] == "parent-1"
def test_caller_supplied_dotted_order_and_trace_id_are_untouched(self):
dotted = "20260820T000000000000Ztrace-1.20260820T000001000000Zrun-1"
data = self._prepare(
{
"trace_id": "trace-1",
"run_id": "run-1",
"dotted_order": dotted,
}
)
assert data["trace_id"] == "trace-1"
assert data["dotted_order"] == dotted
@pytest.mark.asyncio
async def test_events_appended_during_flush_are_not_dropped():
logger = LangsmithLogger(langsmith_api_key="test-key", langsmith_project="test-project")
try:
sent_batches: Final[list[list[dict[str, str]]]] = []
late_event: Final = LangsmithQueueObject(
credentials=logger.default_credentials, data={"id": "late"}
)
async def fake_post(url: str, content: str, headers: dict[str, str]) -> MagicMock:
if not sent_batches:
logger.log_queue.append(late_event)
sent_batches.append(json.loads(content)["post"])
response = MagicMock()
response.status_code = 200
response.raise_for_status = MagicMock()
return response
logger.async_httpx_client = MagicMock(post=AsyncMock(side_effect=fake_post))
logger.log_queue = [
LangsmithQueueObject(credentials=logger.default_credentials, data={"id": "a"}),
LangsmithQueueObject(credentials=logger.default_credentials, data={"id": "b"}),
]
await logger.flush_queue()
assert [e["id"] for e in sent_batches[0]] == ["a", "b"]
assert logger.log_queue == [late_event]
await logger.flush_queue()
assert [e["id"] for e in sent_batches[1]] == ["late"]
assert logger.log_queue == []
finally:
if logger._flush_task is not None:
logger._flush_task.cancel()
await asyncio.gather(logger._flush_task, return_exceptions=True)