Skip to content

Commit 328fcb5

Browse files
committed
push
1 parent 0dcc4fd commit 328fcb5

File tree

5 files changed

+69
-54
lines changed

5 files changed

+69
-54
lines changed

langfuse/_client/span.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ def start_observation(
861861
self,
862862
*,
863863
name: str,
864-
as_type: ObservationTypeLiteral,
864+
as_type: ObservationTypeLiteral = "span",
865865
input: Optional[Any] = None,
866866
output: Optional[Any] = None,
867867
metadata: Optional[Any] = None,
@@ -1109,7 +1109,7 @@ def start_as_current_observation( # type: ignore[misc]
11091109
self,
11101110
*,
11111111
name: str,
1112-
as_type: ObservationTypeLiteralNoEvent,
1112+
as_type: ObservationTypeLiteralNoEvent = "span",
11131113
input: Optional[Any] = None,
11141114
output: Optional[Any] = None,
11151115
metadata: Optional[Any] = None,

tests/test_core_sdk.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,15 @@ def test_create_boolean_score():
211211
# Retrieve and verify
212212
trace = api_wrapper.get_trace(trace_id)
213213

214-
assert trace["scores"][0]["id"] == score_id
215-
assert trace["scores"][0]["dataType"] == "BOOLEAN"
216-
assert trace["scores"][0]["value"] == 1
217-
assert trace["scores"][0]["stringValue"] == "True"
214+
# Find the score we created by name
215+
created_score = next(
216+
(s for s in trace["scores"] if s["name"] == "this-is-a-score"), None
217+
)
218+
assert created_score is not None, "Score not found in trace"
219+
assert created_score["id"] == score_id
220+
assert created_score["dataType"] == "BOOLEAN"
221+
assert created_score["value"] == 1
222+
assert created_score["stringValue"] == "True"
218223

219224

220225
def test_create_categorical_score():
@@ -260,10 +265,15 @@ def test_create_categorical_score():
260265
# Retrieve and verify
261266
trace = api_wrapper.get_trace(trace_id)
262267

263-
assert trace["scores"][0]["id"] == score_id
264-
assert trace["scores"][0]["dataType"] == "CATEGORICAL"
265-
assert trace["scores"][0]["value"] == 0
266-
assert trace["scores"][0]["stringValue"] == "high score"
268+
# Find the score we created by name
269+
created_score = next(
270+
(s for s in trace["scores"] if s["name"] == "this-is-a-score"), None
271+
)
272+
assert created_score is not None, "Score not found in trace"
273+
assert created_score["id"] == score_id
274+
assert created_score["dataType"] == "CATEGORICAL"
275+
assert created_score["value"] == 0
276+
assert created_score["stringValue"] == "high score"
267277

268278

269279
def test_create_score_with_custom_timestamp():
@@ -302,14 +312,19 @@ def test_create_score_with_custom_timestamp():
302312
# Retrieve and verify
303313
trace = api_wrapper.get_trace(trace_id)
304314

305-
assert trace["scores"][0]["id"] == score_id
306-
assert trace["scores"][0]["dataType"] == "NUMERIC"
307-
assert trace["scores"][0]["value"] == 0.85
315+
# Find the score we created by name
316+
created_score = next(
317+
(s for s in trace["scores"] if s["name"] == "custom-timestamp-score"), None
318+
)
319+
assert created_score is not None, "Score not found in trace"
320+
assert created_score["id"] == score_id
321+
assert created_score["dataType"] == "NUMERIC"
322+
assert created_score["value"] == 0.85
308323

309324
# Verify timestamp is close to our custom timestamp
310325
# Parse the timestamp from the API response
311326
response_timestamp = datetime.fromisoformat(
312-
trace["scores"][0]["timestamp"].replace("Z", "+00:00")
327+
created_score["timestamp"].replace("Z", "+00:00")
313328
)
314329

315330
# Check that the timestamps are within 1 second of each other
@@ -1995,7 +2010,7 @@ def test_generate_trace_id():
19952010
def test_generate_trace_url_client_disabled():
19962011
langfuse = Langfuse(tracing_enabled=False)
19972012

1998-
with langfuse.start_as_current_span(
2013+
with langfuse.start_as_current_observation(
19992014
name="test-span",
20002015
):
20012016
# The trace URL should be None because the client is disabled

tests/test_langchain.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_callback_simple_openai():
176176
llm.invoke(text, config={"callbacks": [handler], "run_name": test_name})
177177

178178
# Ensure data is flushed to API
179-
handler.client.flush()
179+
handler._langfuse_client.flush()
180180
sleep(2)
181181

182182
# Retrieve trace
@@ -222,7 +222,7 @@ def test_callback_multiple_invocations_on_different_traces():
222222
handler2 = CallbackHandler()
223223
llm.invoke(text, config={"callbacks": [handler2], "run_name": test_name_2})
224224

225-
handler1.client.flush()
225+
handler1._langfuse_client.flush()
226226

227227
# Ensure data is flushed to API
228228
sleep(2)
@@ -281,7 +281,7 @@ def test_openai_instruct_usage():
281281
]
282282
runnable_chain.batch(input_list)
283283

284-
lf_handler.client.flush()
284+
lf_handler._langfuse_client.flush()
285285

286286
observations = get_api().trace.get(trace_id).observations
287287

@@ -451,7 +451,7 @@ def test_link_langfuse_prompts_invoke():
451451
},
452452
)
453453

454-
langfuse_handler.client.flush()
454+
langfuse_handler._langfuse_client.flush()
455455
sleep(2)
456456

457457
trace = get_api().trace.get(trace_id=trace_id)
@@ -538,7 +538,7 @@ def test_link_langfuse_prompts_stream():
538538
for chunk in stream:
539539
output += chunk
540540

541-
langfuse_handler.client.flush()
541+
langfuse_handler._langfuse_client.flush()
542542
sleep(2)
543543

544544
trace = get_api().trace.get(trace_id=trace_id)
@@ -624,7 +624,7 @@ def test_link_langfuse_prompts_batch():
624624
},
625625
)
626626

627-
langfuse_handler.client.flush()
627+
langfuse_handler._langfuse_client.flush()
628628

629629
traces = get_api().trace.list(name=trace_name).data
630630

@@ -747,13 +747,13 @@ class GetWeather(BaseModel):
747747
}
748748
]
749749

750-
with handler.client.start_as_current_observation(
750+
with handler._langfuse_client.start_as_current_observation(
751751
name="test_callback_openai_functions_with_tools"
752752
) as span:
753753
trace_id = span.trace_id
754754
llm.bind_tools([address_tool, weather_tool]).invoke(messages)
755755

756-
handler.client.flush()
756+
handler._langfuse_client.flush()
757757

758758
trace = get_api().trace.get(trace_id=trace_id)
759759

@@ -846,11 +846,13 @@ def test_multimodal():
846846
],
847847
)
848848

849-
with handler.client.start_as_current_observation(name="test_multimodal") as span:
849+
with handler._langfuse_client.start_as_current_observation(
850+
name="test_multimodal"
851+
) as span:
850852
trace_id = span.trace_id
851853
model.invoke([message], config={"callbacks": [handler]})
852854

853-
handler.client.flush()
855+
handler._langfuse_client.flush()
854856

855857
trace = get_api().trace.get(trace_id=trace_id)
856858

@@ -935,14 +937,16 @@ def call_model(state: MessagesState):
935937
handler = CallbackHandler()
936938

937939
# Use the Runnable
938-
with handler.client.start_as_current_observation(name="test_langgraph") as span:
940+
with handler._langfuse_client.start_as_current_observation(
941+
name="test_langgraph"
942+
) as span:
939943
trace_id = span.trace_id
940944
final_state = app.invoke(
941945
{"messages": [HumanMessage(content="what is the weather in sf")]},
942946
config={"configurable": {"thread_id": 42}, "callbacks": [handler]},
943947
)
944948
print(final_state["messages"][-1].content)
945-
handler.client.flush()
949+
handler._langfuse_client.flush()
946950

947951
trace = get_api().trace.get(trace_id=trace_id)
948952

@@ -975,7 +979,7 @@ def test_cached_token_usage():
975979
# invoke again to force cached token usage
976980
chain.invoke({"test_param": "in a funny way"}, config)
977981

978-
handler.client.flush()
982+
handler._langfuse_client.flush()
979983

980984
trace = get_api().trace.get(handler.get_trace_id())
981985

tests/test_otel.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,16 +2452,16 @@ def test_blocked_instrumentation_scopes_export_filtering(
24522452
allowed_tracer = tracer_provider.get_tracer("allowed-library")
24532453

24542454
# Create spans from each tracer
2455-
langfuse_span = langfuse_tracer.start_observation("langfuse-span")
2455+
langfuse_span = langfuse_tracer.start_span("langfuse-span")
24562456
langfuse_span.end()
24572457

2458-
openai_span = openai_tracer.start_observation("openai-span")
2458+
openai_span = openai_tracer.start_span("openai-span")
24592459
openai_span.end()
24602460

2461-
anthropic_span = anthropic_tracer.start_observation("anthropic-span")
2461+
anthropic_span = anthropic_tracer.start_span("anthropic-span")
24622462
anthropic_span.end()
24632463

2464-
allowed_span = allowed_tracer.start_observation("allowed-span")
2464+
allowed_span = allowed_tracer.start_span("allowed-span")
24652465
allowed_span.end()
24662466

24672467
# Force flush to ensure all spans are processed
@@ -2515,13 +2515,13 @@ def test_no_blocked_scopes_allows_all_exports(
25152515
anthropic_tracer = tracer_provider.get_tracer("anthropic")
25162516

25172517
# Create spans from each tracer
2518-
langfuse_span = langfuse_tracer.start_observation("langfuse-span")
2518+
langfuse_span = langfuse_tracer.start_span("langfuse-span")
25192519
langfuse_span.end()
25202520

2521-
openai_span = openai_tracer.start_observation("openai-span")
2521+
openai_span = openai_tracer.start_span("openai-span")
25222522
openai_span.end()
25232523

2524-
anthropic_span = anthropic_tracer.start_observation("anthropic-span")
2524+
anthropic_span = anthropic_tracer.start_span("anthropic-span")
25252525
anthropic_span.end()
25262526

25272527
# Force flush
@@ -2559,10 +2559,10 @@ def test_none_blocked_scopes_allows_all_exports(
25592559
openai_tracer = tracer_provider.get_tracer("openai")
25602560

25612561
# Create spans from each tracer
2562-
langfuse_span = langfuse_tracer.start_observation("langfuse-span")
2562+
langfuse_span = langfuse_tracer.start_span("langfuse-span")
25632563
langfuse_span.end()
25642564

2565-
openai_span = openai_tracer.start_observation("openai-span")
2565+
openai_span = openai_tracer.start_span("openai-span")
25662566
openai_span.end()
25672567

25682568
# Force flush
@@ -2597,10 +2597,10 @@ def test_blocking_langfuse_sdk_scope_export(self, instrumentation_filtering_setu
25972597
other_tracer = tracer_provider.get_tracer("other-library")
25982598

25992599
# Create spans
2600-
langfuse_span = langfuse_tracer.start_observation("langfuse-span")
2600+
langfuse_span = langfuse_tracer.start_span("langfuse-span")
26012601
langfuse_span.end()
26022602

2603-
other_span = other_tracer.start_observation("other-span")
2603+
other_span = other_tracer.start_span("other-span")
26042604
other_span.end()
26052605

26062606
# Force flush

tests/test_propagate_attributes.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,9 +1180,7 @@ def test_different_tracer_spans_get_attributes(
11801180
langfuse_span.end()
11811181

11821182
# Create span with different tracer
1183-
with other_tracer.start_as_current_observation(
1184-
name="other-library-span"
1185-
):
1183+
with other_tracer.start_as_current_span(name="other-library-span"):
11861184
pass
11871185

11881186
# Verify both spans have the propagated attributes
@@ -1222,8 +1220,8 @@ def test_nested_spans_from_multiple_tracers(
12221220
user_id="user_123", metadata={"experiment": "cross_tracer"}
12231221
):
12241222
# Create nested spans from different tracers
1225-
with tracer_a.start_as_current_observation(name="library-a-span"):
1226-
with tracer_b.start_as_current_observation(name="library-b-span"):
1223+
with tracer_a.start_as_current_span(name="library-a-span"):
1224+
with tracer_b.start_as_current_span(name="library-b-span"):
12271225
langfuse_leaf = langfuse_client.start_observation(
12281226
name="langfuse-leaf"
12291227
)
@@ -1251,13 +1249,13 @@ def test_other_tracer_span_before_propagate_context(
12511249

12521250
with langfuse_client.start_as_current_observation(name="root"):
12531251
# Create span BEFORE propagate_attributes
1254-
with other_tracer.start_as_current_observation(name="span-before"):
1252+
with other_tracer.start_as_current_span(name="span-before"):
12551253
pass
12561254

12571255
# NOW set attributes
12581256
with propagate_attributes(user_id="user_123"):
12591257
# Create span AFTER propagate_attributes
1260-
with other_tracer.start_as_current_observation(name="span-after"):
1258+
with other_tracer.start_as_current_span(name="span-after"):
12611259
pass
12621260

12631261
# Verify: span-before does NOT have user_id, span-after DOES
@@ -1291,9 +1289,7 @@ def test_mixed_tracers_with_metadata(
12911289
)
12921290
langfuse_span.end()
12931291

1294-
with other_tracer.start_as_current_observation(
1295-
name="library-operation"
1296-
):
1292+
with other_tracer.start_as_current_span(name="library-operation"):
12971293
pass
12981294

12991295
# Verify both spans have all metadata
@@ -1322,10 +1318,10 @@ def test_propagate_without_langfuse_parent(
13221318
other_tracer = tracer_provider.get_tracer("other-library", "1.0.0")
13231319

13241320
# Parent span is from different tracer
1325-
with other_tracer.start_as_current_observation(name="other-parent"):
1321+
with other_tracer.start_as_current_span(name="other-parent"):
13261322
with propagate_attributes(user_id="user_123", session_id="session_xyz"):
13271323
# Create children from both tracers
1328-
with other_tracer.start_as_current_observation(name="other-child"):
1324+
with other_tracer.start_as_current_span(name="other-child"):
13291325
pass
13301326

13311327
langfuse_child = langfuse_client.start_observation(
@@ -1358,11 +1354,11 @@ def test_attributes_persist_across_tracer_changes(
13581354
with langfuse_client.start_as_current_observation(name="root"):
13591355
with propagate_attributes(user_id="persistent_user"):
13601356
# Bounce between different tracers
1361-
with tracer_1.start_as_current_observation(name="step-1"):
1357+
with tracer_1.start_as_current_span(name="step-1"):
13621358
pass
13631359

1364-
with tracer_2.start_as_current_observation(name="step-2"):
1365-
with tracer_3.start_as_current_observation(name="step-3"):
1360+
with tracer_2.start_as_current_span(name="step-2"):
1361+
with tracer_3.start_as_current_span(name="step-3"):
13661362
pass
13671363

13681364
langfuse_span = langfuse_client.start_observation(name="step-4")

0 commit comments

Comments
 (0)