@@ -2295,6 +2295,36 @@ def test_tags_attribute_key_format(self, langfuse_client, memory_exporter):
22952295class TestPropagateAttributesExperiment (TestPropagateAttributesBase ):
22962296 """Tests for experiment attribute propagation."""
22972297
2298+ @pytest .mark .asyncio
2299+ async def test_experiment_propagates_user_id_in_async_context (
2300+ self , langfuse_client , memory_exporter
2301+ ):
2302+ """Verify run_experiment keeps propagated attributes when called from async code."""
2303+ import asyncio
2304+
2305+ local_data = [{"input" : "test input" , "expected_output" : "expected output" }]
2306+
2307+ async def async_task (* , item , ** kwargs ):
2308+ await asyncio .sleep (0.01 )
2309+ return f"processed: { item ['input' ]} "
2310+
2311+ with propagate_attributes (user_id = "async-experiment-user" ):
2312+ langfuse_client .run_experiment (
2313+ name = "Async Experiment" ,
2314+ data = local_data ,
2315+ task = async_task ,
2316+ )
2317+
2318+ langfuse_client .flush ()
2319+ time .sleep (0.1 )
2320+
2321+ root_span = self .get_span_by_name (memory_exporter , "experiment-item-run" )
2322+ self .verify_span_attribute (
2323+ root_span ,
2324+ LangfuseOtelSpanAttributes .TRACE_USER_ID ,
2325+ "async-experiment-user" ,
2326+ )
2327+
22982328 def test_experiment_attributes_propagate_without_dataset (
22992329 self , langfuse_client , memory_exporter
23002330 ):
0 commit comments