@@ -1878,3 +1878,53 @@ def test_generate_trace_id():
18781878 project_id = langfuse ._get_project_id ()
18791879 trace_url = langfuse .get_trace_url (trace_id = trace_id )
18801880 assert trace_url == f"http://localhost:3000/project/{ project_id } /traces/{ trace_id } "
1881+
1882+
1883+ def test_start_as_current_observation_types ():
1884+ """Test creating different observation types using start_as_current_observation."""
1885+ langfuse = Langfuse ()
1886+
1887+ observation_types = [
1888+ "span" ,
1889+ "generation" ,
1890+ "agent" ,
1891+ "tool" ,
1892+ "chain" ,
1893+ "retriever" ,
1894+ "evaluator" ,
1895+ "embedding" ,
1896+ "guardrail" ,
1897+ ]
1898+
1899+ with langfuse .start_as_current_span (name = "parent" ) as parent_span :
1900+ parent_span .update_trace (name = "observation-types-test" )
1901+ trace_id = parent_span .trace_id
1902+
1903+ for obs_type in observation_types :
1904+ with parent_span .start_as_current_observation (
1905+ name = f"test-{ obs_type } " , as_type = obs_type
1906+ ):
1907+ pass
1908+
1909+ langfuse .flush ()
1910+
1911+ api = get_api ()
1912+ trace = api .trace .get (trace_id )
1913+
1914+ # Check we have all expected observation types
1915+ found_types = {obs .type for obs in trace .observations }
1916+ expected_types = {obs_type .upper () for obs_type in observation_types } | {
1917+ "SPAN"
1918+ } # includes parent span
1919+ assert expected_types .issubset (
1920+ found_types
1921+ ), f"Missing types: { expected_types - found_types } "
1922+
1923+ # Verify each specific observation exists
1924+ for obs_type in observation_types :
1925+ observations = [
1926+ obs
1927+ for obs in trace .observations
1928+ if obs .name == f"test-{ obs_type } " and obs .type == obs_type .upper ()
1929+ ]
1930+ assert len (observations ) == 1 , f"Expected one { obs_type .upper ()} observation"
0 commit comments