Skip to content

Commit 6c9f9ea

Browse files
committed
improve typing
1 parent c52de84 commit 6c9f9ea

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

langfuse/_client/client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
from opentelemetry import trace
3030
from opentelemetry import trace as otel_trace_api
3131
from opentelemetry.sdk.trace.id_generator import RandomIdGenerator
32-
from opentelemetry.util._decorator import _agnosticcontextmanager
32+
from opentelemetry.util._decorator import (
33+
_AgnosticContextManager,
34+
_agnosticcontextmanager,
35+
)
3336

3437
from langfuse._client.attributes import (
3538
LangfuseOtelSpanAttributes,
@@ -310,7 +313,6 @@ def start_span(
310313
metadata=metadata,
311314
)
312315

313-
# TODO: add return typing
314316
def start_as_current_span(
315317
self,
316318
*,
@@ -323,7 +325,7 @@ def start_as_current_span(
323325
level: Optional[SpanLevel] = None,
324326
status_message: Optional[str] = None,
325327
end_on_exit: Optional[bool] = None,
326-
):
328+
) -> _AgnosticContextManager[LangfuseSpan]:
327329
"""Create a new span and set it as the current span in a context manager.
328330
329331
This method creates a new span and sets it as the current span within a context
@@ -529,7 +531,7 @@ def start_as_current_generation(
529531
cost_details: Optional[Dict[str, float]] = None,
530532
prompt: Optional[PromptClient] = None,
531533
end_on_exit: Optional[bool] = None,
532-
):
534+
) -> _AgnosticContextManager[LangfuseGeneration]:
533535
"""Create a new generation span and set it as the current span in a context manager.
534536
535537
This method creates a specialized span for AI model generations and sets it as the
@@ -954,7 +956,7 @@ def _is_valid_span_id(self, span_id):
954956

955957
return bool(re.match(pattern, span_id))
956958

957-
def create_observation_id(self, *, seed: Optional[str] = None) -> str:
959+
def _create_observation_id(self, *, seed: Optional[str] = None) -> str:
958960
"""Create a unique observation ID for use with Langfuse.
959961
960962
This method generates a unique observation ID (span ID in OpenTelemetry terms)
@@ -1167,7 +1169,7 @@ def create_score(
11671169
if not self.tracing_enabled:
11681170
return
11691171

1170-
score_id = score_id or self.create_observation_id()
1172+
score_id = score_id or self._create_observation_id()
11711173

11721174
try:
11731175
score_event = {

langfuse/_client/span.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929

3030
from opentelemetry import trace as otel_trace_api
31+
from opentelemetry.util._decorator import _AgnosticContextManager
3132

3233
from langfuse.model import PromptClient
3334

@@ -654,7 +655,7 @@ def start_as_current_span(
654655
version: Optional[str] = None,
655656
level: Optional[SpanLevel] = None,
656657
status_message: Optional[str] = None,
657-
):
658+
) -> _AgnosticContextManager["LangfuseSpan"]:
658659
"""Create a new child span and set it as the current span in a context manager.
659660
660661
This method creates a new child span and sets it as the current span within
@@ -698,15 +699,18 @@ def start_as_current_span(
698699
status_message=status_message,
699700
)
700701

701-
return self._langfuse_client._create_span_with_parent_context(
702-
name=name,
703-
attributes=attributes,
704-
as_type="span",
705-
remote_parent_span=None,
706-
parent=self._otel_span,
707-
input=input,
708-
output=output,
709-
metadata=metadata,
702+
return cast(
703+
_AgnosticContextManager["LangfuseSpan"],
704+
self._langfuse_client._create_span_with_parent_context(
705+
name=name,
706+
attributes=attributes,
707+
as_type="span",
708+
remote_parent_span=None,
709+
parent=self._otel_span,
710+
input=input,
711+
output=output,
712+
metadata=metadata,
713+
),
710714
)
711715

712716
def start_generation(
@@ -832,7 +836,7 @@ def start_as_current_generation(
832836
usage_details: Optional[Dict[str, int]] = None,
833837
cost_details: Optional[Dict[str, float]] = None,
834838
prompt: Optional[PromptClient] = None,
835-
):
839+
) -> _AgnosticContextManager["LangfuseGeneration"]:
836840
"""Create a new child generation span and set it as the current span in a context manager.
837841
838842
This method creates a new child generation span and sets it as the current span
@@ -900,15 +904,18 @@ def start_as_current_generation(
900904
prompt=prompt,
901905
)
902906

903-
return self._langfuse_client._create_span_with_parent_context(
904-
name=name,
905-
attributes=attributes,
906-
as_type="generation",
907-
remote_parent_span=None,
908-
parent=self._otel_span,
909-
input=input,
910-
output=output,
911-
metadata=metadata,
907+
return cast(
908+
_AgnosticContextManager["LangfuseGeneration"],
909+
self._langfuse_client._create_span_with_parent_context(
910+
name=name,
911+
attributes=attributes,
912+
as_type="generation",
913+
remote_parent_span=None,
914+
parent=self._otel_span,
915+
input=input,
916+
output=output,
917+
metadata=metadata,
918+
),
912919
)
913920

914921

0 commit comments

Comments
 (0)