Skip to content

Commit 7e76f15

Browse files
committed
fix(observe): preserve current span observation type
1 parent 5c9efae commit 7e76f15

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

langfuse/_client/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,13 @@ def update_current_span(
13781378
current_otel_span = self._get_current_otel_span()
13791379

13801380
if current_otel_span is not None:
1381-
span = LangfuseSpan(
1381+
existing_observation_type = current_otel_span.attributes.get( # type: ignore[attr-defined]
1382+
LangfuseOtelSpanAttributes.OBSERVATION_TYPE, "span"
1383+
)
1384+
span_class = self._get_span_class(
1385+
cast(ObservationTypeLiteral, existing_observation_type)
1386+
)
1387+
span = span_class(
13821388
otel_span=current_otel_span,
13831389
langfuse_client=self,
13841390
environment=self._environment,

tests/unit/test_observe.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,34 @@ async def endpoint() -> StreamingResponse:
114114
assert LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT not in endpoint_span.attributes
115115

116116

117+
def test_observe_capture_output_false_preserves_as_type_after_current_span_update(
118+
langfuse_memory_client: Any, memory_exporter: Any
119+
) -> None:
120+
@observe(
121+
name="probe.with_capture_output_false",
122+
as_type="guardrail",
123+
capture_output=False,
124+
)
125+
def probe() -> dict[str, bool]:
126+
langfuse_memory_client.update_current_span(output={"verdict": "manually set"})
127+
128+
return {"decorator_output": True}
129+
130+
assert probe() == {"decorator_output": True}
131+
132+
langfuse_memory_client.flush()
133+
134+
probe_span = _finished_spans_by_name(
135+
memory_exporter, "probe.with_capture_output_false"
136+
)[0]
137+
138+
assert (
139+
probe_span.attributes[LangfuseOtelSpanAttributes.OBSERVATION_TYPE]
140+
== "guardrail"
141+
)
142+
assert LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT in probe_span.attributes
143+
144+
117145
def test_sync_generator_wrapper_close_ends_span_without_exhaustion() -> None:
118146
def generator() -> Generator[str, None, None]:
119147
yield "item_0"

0 commit comments

Comments
 (0)