2222 List ,
2323 Literal ,
2424 Optional ,
25+ Type ,
2526 Union ,
2627 cast ,
28+ get_args ,
2729 overload ,
2830)
2931
5254# Factory mapping for observation classes
5355# Note: "event" is handled separately due to special instantiation logic
5456# Populated after class definitions
55- _OBSERVATION_CLASS_MAP = {}
56-
57- # Cache generation-like types for performance
58- _GENERATION_LIKE_TYPES = None
57+ _OBSERVATION_CLASS_MAP : Dict [str , Type ["LangfuseSpanWrapper" ]] = {}
5958
6059
6160class LangfuseSpanWrapper :
@@ -143,7 +142,7 @@ def __init__(
143142
144143 attributes = {}
145144
146- if as_type in ObservationTypeGenerationLike . __args__ :
145+ if as_type in get_args ( ObservationTypeGenerationLike ) :
147146 attributes = create_generation_attributes (
148147 input = media_processed_input ,
149148 output = media_processed_output ,
@@ -602,7 +601,7 @@ def update(
602601 self ._otel_span .update_name (name )
603602
604603 # Use same logic as __init__ to determine which attributes to create
605- if self ._observation_type in ObservationTypeGenerationLike . __args__ :
604+ if self ._observation_type in get_args ( ObservationTypeGenerationLike ) :
606605 attributes = create_generation_attributes (
607606 input = processed_input ,
608607 output = processed_output ,
@@ -747,18 +746,15 @@ def start_span(
747746 parent_span.end()
748747 ```
749748 """
750- return cast (
751- "LangfuseSpan" ,
752- self .start_observation (
753- name = name ,
754- as_type = "span" ,
755- input = input ,
756- output = output ,
757- metadata = metadata ,
758- version = version ,
759- level = level ,
760- status_message = status_message ,
761- ),
749+ return self .start_observation (
750+ name = name ,
751+ as_type = "span" ,
752+ input = input ,
753+ output = output ,
754+ metadata = metadata ,
755+ version = version ,
756+ level = level ,
757+ status_message = status_message ,
762758 )
763759
764760 def start_as_current_span (
@@ -806,18 +802,15 @@ def start_as_current_span(
806802 parent_span.update(output=result)
807803 ```
808804 """
809- return cast (
810- _AgnosticContextManager ["LangfuseSpan" ],
811- self .start_as_current_observation (
812- name = name ,
813- as_type = "span" ,
814- input = input ,
815- output = output ,
816- metadata = metadata ,
817- version = version ,
818- level = level ,
819- status_message = status_message ,
820- ),
805+ return self .start_as_current_observation (
806+ name = name ,
807+ as_type = "span" ,
808+ input = input ,
809+ output = output ,
810+ metadata = metadata ,
811+ version = version ,
812+ level = level ,
813+ status_message = status_message ,
821814 )
822815
823816 def start_generation (
@@ -894,24 +887,21 @@ def start_generation(
894887 span.end()
895888 ```
896889 """
897- return cast (
898- "LangfuseGeneration" ,
899- self .start_observation (
900- name = name ,
901- as_type = "generation" ,
902- input = input ,
903- output = output ,
904- metadata = metadata ,
905- version = version ,
906- level = level ,
907- status_message = status_message ,
908- completion_start_time = completion_start_time ,
909- model = model ,
910- model_parameters = model_parameters ,
911- usage_details = usage_details ,
912- cost_details = cost_details ,
913- prompt = prompt ,
914- ),
890+ return self .start_observation (
891+ name = name ,
892+ as_type = "generation" ,
893+ input = input ,
894+ output = output ,
895+ metadata = metadata ,
896+ version = version ,
897+ level = level ,
898+ status_message = status_message ,
899+ completion_start_time = completion_start_time ,
900+ model = model ,
901+ model_parameters = model_parameters ,
902+ usage_details = usage_details ,
903+ cost_details = cost_details ,
904+ prompt = prompt ,
915905 )
916906
917907 def start_as_current_generation (
@@ -1331,9 +1321,7 @@ def start_observation(
13311321 "status_message" : status_message ,
13321322 }
13331323
1334- # TODO: perf, cache this value? "calculated" on every observation create
1335- # if as_type in get_observation_types_list(ObservationTypeGenerationLike):
1336- if as_type in ObservationTypeGenerationLike .__args__ :
1324+ if as_type in get_args (ObservationTypeGenerationLike ):
13371325 common_args .update (
13381326 {
13391327 "completion_start_time" : completion_start_time ,
@@ -1345,7 +1333,7 @@ def start_observation(
13451333 }
13461334 )
13471335
1348- return observation_class (** common_args )
1336+ return observation_class (** common_args ) # type: ignore[no-any-return,return-value,arg-type]
13491337
13501338 @overload
13511339 def start_as_current_observation (
@@ -1361,26 +1349,6 @@ def start_as_current_observation(
13611349 status_message : Optional [str ] = None ,
13621350 ) -> _AgnosticContextManager ["LangfuseSpan" ]: ...
13631351
1364- @overload
1365- def start_as_current_observation (
1366- self ,
1367- * ,
1368- name : str ,
1369- as_type : Literal ["generation" ],
1370- input : Optional [Any ] = None ,
1371- output : Optional [Any ] = None ,
1372- metadata : Optional [Any ] = None ,
1373- version : Optional [str ] = None ,
1374- level : Optional [SpanLevel ] = None ,
1375- status_message : Optional [str ] = None ,
1376- completion_start_time : Optional [datetime ] = None ,
1377- model : Optional [str ] = None ,
1378- model_parameters : Optional [Dict [str , MapValue ]] = None ,
1379- usage_details : Optional [Dict [str , int ]] = None ,
1380- cost_details : Optional [Dict [str , float ]] = None ,
1381- prompt : Optional [PromptClient ] = None ,
1382- ) -> _AgnosticContextManager ["LangfuseGeneration" ]: ...
1383-
13841352 @overload
13851353 def start_as_current_observation (
13861354 self ,
@@ -1424,7 +1392,7 @@ def start_as_current_observation(
14241392 status_message : Optional [str ] = None ,
14251393 ) -> _AgnosticContextManager ["LangfuseGuardrail" ]: ...
14261394
1427- def start_as_current_observation (
1395+ def start_as_current_observation ( # type: ignore[misc]
14281396 self ,
14291397 * ,
14301398 name : str ,
@@ -1441,7 +1409,20 @@ def start_as_current_observation(
14411409 usage_details : Optional [Dict [str , int ]] = None ,
14421410 cost_details : Optional [Dict [str , float ]] = None ,
14431411 prompt : Optional [PromptClient ] = None ,
1444- ):
1412+ # TODO: or union of context managers?
1413+ ) -> _AgnosticContextManager [
1414+ Union [
1415+ "LangfuseSpan" ,
1416+ "LangfuseGeneration" ,
1417+ "LangfuseAgent" ,
1418+ "LangfuseTool" ,
1419+ "LangfuseChain" ,
1420+ "LangfuseRetriever" ,
1421+ "LangfuseEvaluator" ,
1422+ "LangfuseEmbedding" ,
1423+ "LangfuseGuardrail" ,
1424+ ]
1425+ ]:
14451426 """Create a new child observation and set it as the current observation in a context manager.
14461427
14471428 This is the generic method for creating any type of child observation with
0 commit comments