@@ -315,16 +315,56 @@ def test_create_update_trace():
315315 langfuse .flush ()
316316 sleep (2 )
317317
318- # Ensure trace_id is a string before passing to the API
319- if trace_id is not None :
320- # Retrieve and verify trace
321- trace = get_api ().trace .get (trace_id )
318+ assert isinstance (trace_id , str )
319+ # Retrieve and verify trace
320+ trace = get_api ().trace .get (trace_id )
321+
322+ assert trace .name == trace_name
323+ assert trace .user_id == "test"
324+ assert trace .metadata ["key" ] == "value"
325+ assert trace .metadata ["key2" ] == "value2"
326+ assert trace .public is False
327+
328+
329+ def test_create_update_current_trace ():
330+ langfuse = Langfuse ()
331+
332+ trace_name = create_uuid ()
333+
334+ # Create initial span with trace properties using update_current_trace
335+ with langfuse .start_as_current_span (name = "test-span-current" ) as span :
336+ langfuse .update_current_trace (
337+ name = trace_name ,
338+ user_id = "test" ,
339+ metadata = {"key" : "value" },
340+ public = True ,
341+ input = "test_input"
342+ )
343+ # Get trace ID for later reference
344+ trace_id = span .trace_id
322345
323- assert trace .name == trace_name
324- assert trace .user_id == "test"
325- assert trace .metadata ["key" ] == "value"
326- assert trace .metadata ["key2" ] == "value2"
327- assert trace .public is False
346+ # Allow a small delay before updating
347+ sleep (1 )
348+
349+ # Update trace properties using update_current_trace
350+ langfuse .update_current_trace (metadata = {"key2" : "value2" }, public = False , version = "1.0" )
351+
352+ # Ensure data is sent to the API
353+ langfuse .flush ()
354+ sleep (2 )
355+
356+ assert isinstance (trace_id , str )
357+ # Retrieve and verify trace
358+ trace = get_api ().trace .get (trace_id )
359+
360+ # The 2nd update to the trace must not erase previously set attributes
361+ assert trace .name == trace_name
362+ assert trace .user_id == "test"
363+ assert trace .metadata ["key" ] == "value"
364+ assert trace .metadata ["key2" ] == "value2"
365+ assert trace .public is False
366+ assert trace .version == "1.0"
367+ assert trace .input == "test_input"
328368
329369
330370def test_create_generation ():
@@ -1917,9 +1957,9 @@ def test_start_as_current_observation_types():
19171957 expected_types = {obs_type .upper () for obs_type in observation_types } | {
19181958 "SPAN"
19191959 } # includes parent span
1920- assert expected_types .issubset (
1921- found_types
1922- ), f"Missing types: { expected_types - found_types } "
1960+ assert expected_types .issubset (found_types ), (
1961+ f"Missing types: { expected_types - found_types } "
1962+ )
19231963
19241964 # Verify each specific observation exists
19251965 for obs_type in observation_types :
@@ -1963,25 +2003,25 @@ def test_that_generation_like_properties_are_actually_created():
19632003 ) as obs :
19642004 # Verify the properties are accessible on the observation object
19652005 if hasattr (obs , "model" ):
1966- assert (
1967- obs . model == test_model
1968- ), f" { obs_type } should have model property"
2006+ assert obs . model == test_model , (
2007+ f" { obs_type } should have model property"
2008+ )
19692009 if hasattr (obs , "completion_start_time" ):
1970- assert (
1971- obs . completion_start_time == test_completion_start_time
1972- ), f" { obs_type } should have completion_start_time property"
2010+ assert obs . completion_start_time == test_completion_start_time , (
2011+ f" { obs_type } should have completion_start_time property"
2012+ )
19732013 if hasattr (obs , "model_parameters" ):
1974- assert (
1975- obs . model_parameters == test_model_parameters
1976- ), f" { obs_type } should have model_parameters property"
2014+ assert obs . model_parameters == test_model_parameters , (
2015+ f" { obs_type } should have model_parameters property"
2016+ )
19772017 if hasattr (obs , "usage_details" ):
1978- assert (
1979- obs . usage_details == test_usage_details
1980- ), f" { obs_type } should have usage_details property"
2018+ assert obs . usage_details == test_usage_details , (
2019+ f" { obs_type } should have usage_details property"
2020+ )
19812021 if hasattr (obs , "cost_details" ):
1982- assert (
1983- obs . cost_details == test_cost_details
1984- ), f" { obs_type } should have cost_details property"
2022+ assert obs . cost_details == test_cost_details , (
2023+ f" { obs_type } should have cost_details property"
2024+ )
19852025
19862026 langfuse .flush ()
19872027
@@ -1995,28 +2035,28 @@ def test_that_generation_like_properties_are_actually_created():
19952035 for obs in trace .observations
19962036 if obs .name == f"test-{ obs_type } " and obs .type == obs_type .upper ()
19972037 ]
1998- assert (
1999- len ( observations ) == 1
2000- ), f"Expected one { obs_type . upper () } observation, but found { len ( observations ) } "
2038+ assert len ( observations ) == 1 , (
2039+ f"Expected one { obs_type . upper () } observation, but found { len ( observations ) } "
2040+ )
20012041
20022042 obs = observations [0 ]
20032043
20042044 assert obs .model == test_model , f"{ obs_type } should have model property"
2005- assert (
2006- obs . model_parameters == test_model_parameters
2007- ), f" { obs_type } should have model_parameters property"
2045+ assert obs . model_parameters == test_model_parameters , (
2046+ f" { obs_type } should have model_parameters property"
2047+ )
20082048
20092049 # usage_details
20102050 assert hasattr (obs , "usage_details" ), f"{ obs_type } should have usage_details"
2011- assert obs .usage_details == dict (
2012- test_usage_details , total = 30
2013- ), f" { obs_type } should persist usage_details" # API adds total
2051+ assert obs .usage_details == dict (test_usage_details , total = 30 ), (
2052+ f" { obs_type } should persist usage_details"
2053+ ) # API adds total
20142054
2015- assert (
2016- obs . cost_details == test_cost_details
2017- ), f" { obs_type } should persist cost_details"
2055+ assert obs . cost_details == test_cost_details , (
2056+ f" { obs_type } should persist cost_details"
2057+ )
20182058
20192059 # completion_start_time, because of time skew not asserting time
2020- assert (
2021- obs . completion_start_time is not None
2022- ), f" { obs_type } should persist completion_start_time property"
2060+ assert obs . completion_start_time is not None , (
2061+ f" { obs_type } should persist completion_start_time property"
2062+ )
0 commit comments