fix(langsmith): keep events appended during an in-flight flush instead of clearing them

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
mrinal 2026-09-15 20:38:35 +00:00
parent 7d3917dbf4
commit 4a8986dd72
2 changed files with 34 additions and 0 deletions

View file

@ -39,6 +39,8 @@ def is_serializable(value):
class LangsmithLogger(CustomBatchLogger):
preserve_events_added_during_flush = True
def __init__(
self,
langsmith_api_key: str | None = None,

View file

@ -531,3 +531,35 @@ class TestLangsmithRootRunIdConsistency:
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")
sent_batches: list[list[dict]] = []
late_event = {"credentials": logger.default_credentials, "data": {"id": "late"}}
async def fake_post(url, json, headers):
if not sent_batches:
logger.log_queue.append(late_event)
sent_batches.append(json["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 = [
{"credentials": logger.default_credentials, "data": {"id": "a"}},
{"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 == []