diff --git a/langfuse/_client/client.py b/langfuse/_client/client.py index fd4928316..bb55e04b7 100644 --- a/langfuse/_client/client.py +++ b/langfuse/_client/client.py @@ -1612,7 +1612,18 @@ def update_current_generation( current_otel_span = self._get_current_otel_span() if current_otel_span is not None: - generation = LangfuseGeneration( + # Keep a generation-like type such as "embedding"; other types + # become "generation" so that model and usage are recorded. + observation_type = self._get_observation_type_from_otel_span( + current_otel_span + ) + generation_class = ( + self._get_span_class(observation_type) + if observation_type + in get_observation_types_list(ObservationTypeGenerationLike) + else LangfuseGeneration + ) + generation = generation_class( otel_span=current_otel_span, langfuse_client=self ) diff --git a/tests/unit/test_observe.py b/tests/unit/test_observe.py index 21ff1418d..b210a3955 100644 --- a/tests/unit/test_observe.py +++ b/tests/unit/test_observe.py @@ -55,6 +55,30 @@ async def guardrail_check() -> bool: } +def test_capture_output_false_preserves_embedding_type_when_current_generation_is_updated( + langfuse_memory_client: Any, memory_exporter: Any +) -> None: + @observe(name="embed_text", as_type="embedding", capture_output=False) + def embed_text() -> list[float]: + langfuse_memory_client.update_current_generation( + model="text-embedding-3-small", usage_details={"input": 5} + ) + return [0.1, 0.2] + + embed_text() + + langfuse_memory_client.flush() + + embedding_span = _finished_spans_by_name(memory_exporter, "embed_text")[0] + attributes = embedding_span.attributes + + assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_TYPE] == "embedding" + assert ( + attributes[LangfuseOtelSpanAttributes.OBSERVATION_MODEL] + == "text-embedding-3-small" + ) + + def test_sync_generator_preserves_context_without_output_capture( langfuse_memory_client: Any, memory_exporter: Any ) -> None: