|
3 | 3 | This module defines constants used throughout the Langfuse OpenTelemetry integration. |
4 | 4 | """ |
5 | 5 |
|
6 | | -import enum |
7 | | -from typing import Literal |
| 6 | +from typing import Literal, List, get_args |
8 | 7 | from typing_extensions import TypeAlias |
9 | 8 |
|
10 | 9 | LANGFUSE_TRACER_NAME = "langfuse-sdk" |
11 | 10 |
|
12 | 11 |
|
13 | | -class ObservationType(str, enum.Enum): |
14 | | - """Enumeration of valid observation types for Langfuse tracing. |
15 | | -
|
16 | | - This enum defines all the observation types that can be used with the @observe |
17 | | - decorator and other Langfuse SDK methods. |
18 | | - """ |
19 | | - |
20 | | - SPAN = "SPAN" |
21 | | - GENERATION = "GENERATION" |
22 | | - AGENT = "AGENT" |
23 | | - TOOL = "TOOL" |
24 | | - CHAIN = "CHAIN" |
25 | | - RETRIEVER = "RETRIEVER" |
26 | | - EMBEDDING = "EMBEDDING" |
27 | | - EVALUATOR = "EVALUATOR" |
28 | | - GUARDRAIL = "GUARDRAIL" |
29 | | - |
30 | | - |
31 | | -ObservationTypeLiteralNoEvent: TypeAlias = Literal[ |
32 | | - "span", |
| 12 | +ObservationTypeGenerationLike: TypeAlias = Literal[ |
33 | 13 | "generation", |
34 | 14 | "agent", |
35 | 15 | "tool", |
36 | 16 | "chain", |
37 | 17 | "retriever", |
38 | 18 | "evaluator", |
39 | 19 | "embedding", |
40 | | - "guardrail", |
41 | 20 | ] |
42 | 21 |
|
| 22 | +ObservationTypeLiteralNoEvent: TypeAlias = ( |
| 23 | + ObservationTypeGenerationLike |
| 24 | + | Literal[ |
| 25 | + "span", |
| 26 | + "guardrail", |
| 27 | + ] |
| 28 | +) |
| 29 | + |
43 | 30 | ObservationTypeLiteral: TypeAlias = ObservationTypeLiteralNoEvent | Literal["event"] |
| 31 | +"""Enumeration of valid observation types for Langfuse tracing. |
| 32 | +
|
| 33 | +This Literal defines all available observation types that can be used with the @observe |
| 34 | +decorator and other Langfuse SDK methods. |
| 35 | +""" |
| 36 | + |
| 37 | + |
| 38 | +def get_observation_types_list( |
| 39 | + literal_type: ObservationTypeGenerationLike |
| 40 | + | ObservationTypeLiteralNoEvent |
| 41 | + | ObservationTypeLiteral, |
| 42 | +) -> List[str]: |
| 43 | + """Flattens the Literal type to provide a list of strings. |
| 44 | +
|
| 45 | + Args: |
| 46 | + literal_type: A Literal type or union of Literals to flatten |
| 47 | +
|
| 48 | + Returns: |
| 49 | + Flat list of all string values contained in the Literal type |
| 50 | + """ |
| 51 | + result = [] |
| 52 | + args = get_args(literal_type) |
| 53 | + |
| 54 | + for arg in args: |
| 55 | + if hasattr(arg, "__args__"): |
| 56 | + result.extend(get_observation_types_list(arg)) |
| 57 | + else: |
| 58 | + result.append(arg) |
| 59 | + |
| 60 | + return result |
0 commit comments