Skip to content

Commit 0b25836

Browse files
committed
simplify
1 parent abddc45 commit 0b25836

File tree

1 file changed

+23
-48
lines changed

1 file changed

+23
-48
lines changed

langfuse/_client/client.py

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ def start_as_current_observation(
791791
status_message: Optional status message for the observation
792792
end_on_exit (default: True): Whether to end the span automatically when leaving the context manager. If False, the span must be manually ended to avoid memory leaks.
793793
794+
# TODO: also add the generation like types here!
794795
The following parameters are only available when as_type="generation":
795796
completion_start_time: When the model started generating the response
796797
model: Name/identifier of the AI model used (e.g., "gpt-4")
@@ -804,11 +805,16 @@ def start_as_current_observation(
804805
805806
Example:
806807
```python
807-
# Create an agent observation
808-
with langfuse.start_as_current_observation(name="planning-agent", as_type="agent") as agent:
809-
# Do agent work
810-
plan = create_plan()
811-
agent.update(output=plan)
808+
# Create a span
809+
with langfuse.start_as_current_observation(name="process-query", as_type="span") as span:
810+
# Do work
811+
result = process_data()
812+
span.update(output=result)
813+
814+
# Create a child span automatically
815+
with span.start_as_current_span(name="sub-operation") as child_span:
816+
# Do sub-operation work
817+
child_span.update(output="sub-result")
812818
813819
# Create a tool observation
814820
with langfuse.start_as_current_observation(name="web-search", as_type="tool") as tool:
@@ -827,47 +833,21 @@ def start_as_current_observation(
827833
generation.update(output=response)
828834
```
829835
"""
830-
# Delegate to existing methods for consistency
831836
if as_type == "span":
832-
return cast(
833-
Union[
834-
_AgnosticContextManager[LangfuseGeneration],
835-
_AgnosticContextManager[LangfuseSpan],
836-
_AgnosticContextManager[LangfuseAgent],
837-
_AgnosticContextManager[LangfuseTool],
838-
_AgnosticContextManager[LangfuseChain],
839-
_AgnosticContextManager[LangfuseRetriever],
840-
_AgnosticContextManager[LangfuseEvaluator],
841-
_AgnosticContextManager[LangfuseEmbedding],
842-
_AgnosticContextManager[LangfuseGuardrail],
843-
],
844-
self.start_as_current_span(
845-
trace_context=trace_context,
846-
name=name,
847-
input=input,
848-
output=output,
849-
metadata=metadata,
850-
version=version,
851-
level=level,
852-
status_message=status_message,
853-
end_on_exit=end_on_exit,
854-
),
837+
return self.start_as_current_span(
838+
trace_context=trace_context,
839+
name=name,
840+
input=input,
841+
output=output,
842+
metadata=metadata,
843+
version=version,
844+
level=level,
845+
status_message=status_message,
846+
end_on_exit=end_on_exit,
855847
)
856848

857849
if as_type == "generation":
858-
return cast(
859-
Union[
860-
_AgnosticContextManager[LangfuseGeneration],
861-
_AgnosticContextManager[LangfuseSpan],
862-
_AgnosticContextManager[LangfuseAgent],
863-
_AgnosticContextManager[LangfuseTool],
864-
_AgnosticContextManager[LangfuseChain],
865-
_AgnosticContextManager[LangfuseRetriever],
866-
_AgnosticContextManager[LangfuseEvaluator],
867-
_AgnosticContextManager[LangfuseEmbedding],
868-
_AgnosticContextManager[LangfuseGuardrail],
869-
],
870-
self.start_as_current_generation(
850+
return self.start_as_current_generation(
871851
trace_context=trace_context,
872852
name=name,
873853
input=input,
@@ -883,8 +863,7 @@ def start_as_current_observation(
883863
cost_details=cost_details,
884864
prompt=prompt,
885865
end_on_exit=end_on_exit,
886-
),
887-
)
866+
)
888867

889868
if trace_context:
890869
trace_id = trace_context.get("trace_id", None)
@@ -897,8 +876,6 @@ def start_as_current_observation(
897876

898877
return cast(
899878
Union[
900-
_AgnosticContextManager[LangfuseGeneration],
901-
_AgnosticContextManager[LangfuseSpan],
902879
_AgnosticContextManager[LangfuseAgent],
903880
_AgnosticContextManager[LangfuseTool],
904881
_AgnosticContextManager[LangfuseChain],
@@ -930,8 +907,6 @@ def start_as_current_observation(
930907

931908
return cast(
932909
Union[
933-
_AgnosticContextManager[LangfuseGeneration],
934-
_AgnosticContextManager[LangfuseSpan],
935910
_AgnosticContextManager[LangfuseAgent],
936911
_AgnosticContextManager[LangfuseTool],
937912
_AgnosticContextManager[LangfuseChain],

0 commit comments

Comments
 (0)