From 3d570ebcf8e7fd25f22bd685a6171f5eb9daceba Mon Sep 17 00:00:00 2001 From: Hassieb Pakzad <68423100+hassiebp@users.noreply.github.com> Date: Mon, 17 Nov 2025 15:33:15 +0100 Subject: [PATCH] fix(experiments): move to sync run item creation to avoid latency issues --- langfuse/_client/client.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/langfuse/_client/client.py b/langfuse/_client/client.py index 3a432da25..52c8200a5 100644 --- a/langfuse/_client/client.py +++ b/langfuse/_client/client.py @@ -2809,18 +2809,21 @@ async def _process_experiment_item( # Link to dataset run if this is a dataset item if hasattr(item, "id") and hasattr(item, "dataset_id"): try: - # Use sync API to avoid event loop issues when run_async_safely - # creates multiple event loops across different threads - dataset_run_item = await asyncio.to_thread( - self.api.dataset_run_items.create, - request=CreateDatasetRunItemRequest( + # Use sync API instead of async API due to httpx.AsyncClient performance issues + # at high concurrency (100+ concurrent requests). The async API exhibits cumulative + # performance degradation where each subsequent request takes progressively longer + # (~100ms more per request), causing items to complete in 800ms, 1000ms, 1200ms, etc., + # even though the server processes each request in ~300ms. Using the sync API blocks + # during creation but avoids the httpx async bottleneck, ensuring correct timing. + dataset_run_item = self.api.dataset_run_items.create( + CreateDatasetRunItemRequest( runName=experiment_run_name, runDescription=experiment_description, metadata=experiment_metadata, datasetItemId=item.id, # type: ignore traceId=trace_id, observationId=span.id, - ), + ) ) dataset_run_id = dataset_run_item.dataset_run_id