Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions langfuse/_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
from langfuse._client.constants import (
LANGFUSE_SDK_EXPERIMENT_ENVIRONMENT,
ObservationTypeGenerationLike,
ObservationTypeLiteral,
ObservationTypeLiteralNoEvent,
ObservationTypeSpanLike,
get_observation_types_list,
Expand Down Expand Up @@ -1069,7 +1068,7 @@

def _get_span_class(
self,
as_type: ObservationTypeLiteral,
as_type: str,
) -> Union[
Type[LangfuseAgent],
Type[LangfuseTool],
Expand Down Expand Up @@ -1108,6 +1107,21 @@
else:
return LangfuseSpan

@staticmethod
def _get_observation_type_from_otel_span(otel_span: otel_trace_api.Span) -> str:
if not otel_span.is_recording():
return "span"

attributes = getattr(otel_span, "attributes", None)
if attributes is None or not hasattr(attributes, "get"):
return "span"

observation_type = attributes.get(
LangfuseOtelSpanAttributes.OBSERVATION_TYPE, "span"
)

return observation_type if isinstance(observation_type, str) else "span"

@_agnosticcontextmanager
def _create_span_with_parent_context(
self,
Expand Down Expand Up @@ -1378,10 +1392,13 @@
current_otel_span = self._get_current_otel_span()

if current_otel_span is not None:
span = LangfuseSpan(
span_class = self._get_span_class(
self._get_observation_type_from_otel_span(current_otel_span)
)
span = span_class(
otel_span=current_otel_span,
langfuse_client=self,
environment=self._environment,

Check notice on line 1401 in langfuse/_client/client.py

View check run for this annotation

Claude / Claude Code Review

update_current_generation has the same observation type downgrade

Pre-existing parallel bug — the symmetric `update_current_generation` (langfuse/_client/client.py:1319-1342, untouched by this PR) still unconditionally instantiates `LangfuseGeneration(...)`, which hardcodes `OBSERVATION_TYPE="generation"` via the base wrapper. So `@observe(as_type="embedding", capture_output=False)` calling `update_current_generation(...)` in the body silently downgrades the type from `embedding` to `generation` — the same failure mode this PR fixes for span-like types. The fi
Comment thread
hassiebp marked this conversation as resolved.
release=self._release,
)

Expand Down Expand Up @@ -1431,11 +1448,9 @@
current_otel_span = self._get_current_otel_span()

if current_otel_span is not None and current_otel_span.is_recording():
existing_observation_type = current_otel_span.attributes.get( # type: ignore[attr-defined]
LangfuseOtelSpanAttributes.OBSERVATION_TYPE, "span"
span_class = self._get_span_class(
self._get_observation_type_from_otel_span(current_otel_span)
)
# We need to preserve the class to keep the correct observation type
span_class = self._get_span_class(existing_observation_type)
span = span_class(
otel_span=current_otel_span,
langfuse_client=self,
Expand Down Expand Up @@ -1468,11 +1483,9 @@
current_otel_span = self._get_current_otel_span()

if current_otel_span is not None and current_otel_span.is_recording():
existing_observation_type = current_otel_span.attributes.get( # type: ignore[attr-defined]
LangfuseOtelSpanAttributes.OBSERVATION_TYPE, "span"
span_class = self._get_span_class(
self._get_observation_type_from_otel_span(current_otel_span)
)
# We need to preserve the class to keep the correct observation type
span_class = self._get_span_class(existing_observation_type)
span = span_class(
otel_span=current_otel_span,
langfuse_client=self,
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_observe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import contextvars
import gc
import json
import sys
from typing import Any, AsyncGenerator, Generator, cast

Expand Down Expand Up @@ -32,6 +33,28 @@ def _finished_spans_by_name(memory_exporter: Any, name: str) -> list[Any]:
return [span for span in memory_exporter.get_finished_spans() if span.name == name]


@pytest.mark.asyncio
async def test_capture_output_false_preserves_type_when_current_span_is_updated(
langfuse_memory_client: Any, memory_exporter: Any
) -> None:
@observe(name="guardrail_check", as_type="guardrail", capture_output=False)
async def guardrail_check() -> bool:
langfuse_memory_client.update_current_span(output={"verdict": "manually set"})
return True

assert await guardrail_check() is True

langfuse_memory_client.flush()

guardrail_span = _finished_spans_by_name(memory_exporter, "guardrail_check")[0]
attributes = guardrail_span.attributes

assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_TYPE] == "guardrail"
assert json.loads(attributes[LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT]) == {
"verdict": "manually set"
}


def test_sync_generator_preserves_context_without_output_capture(
langfuse_memory_client: Any, memory_exporter: Any
) -> None:
Expand Down
Loading