langfuse.api.legacy

 1# This file was auto-generated by Fern from our API Definition.
 2
 3# isort: skip_file
 4
 5import typing
 6from importlib import import_module
 7
 8if typing.TYPE_CHECKING:
 9    from . import metrics_v1, observations_v1, score_v1
10    from .metrics_v1 import MetricsResponse
11    from .observations_v1 import Observations, ObservationsViews
12_dynamic_imports: typing.Dict[str, str] = {
13    "MetricsResponse": ".metrics_v1",
14    "Observations": ".observations_v1",
15    "ObservationsViews": ".observations_v1",
16    "metrics_v1": ".metrics_v1",
17    "observations_v1": ".observations_v1",
18    "score_v1": ".score_v1",
19}
20
21
22def __getattr__(attr_name: str) -> typing.Any:
23    module_name = _dynamic_imports.get(attr_name)
24    if module_name is None:
25        raise AttributeError(
26            f"No {attr_name} found in _dynamic_imports for module name -> {__name__}"
27        )
28    try:
29        module = import_module(module_name, __package__)
30        if module_name == f".{attr_name}":
31            return module
32        else:
33            return getattr(module, attr_name)
34    except ImportError as e:
35        raise ImportError(
36            f"Failed to import {attr_name} from {module_name}: {e}"
37        ) from e
38    except AttributeError as e:
39        raise AttributeError(
40            f"Failed to get {attr_name} from {module_name}: {e}"
41        ) from e
42
43
44def __dir__():
45    lazy_attrs = list(_dynamic_imports.keys())
46    return sorted(lazy_attrs)
47
48
49__all__ = [
50    "MetricsResponse",
51    "Observations",
52    "ObservationsViews",
53    "metrics_v1",
54    "observations_v1",
55    "score_v1",
56]
57
58# Score-create compatibility aliases (LFE-10397).
59from .score_v1 import CreateScoreRequest
60from .score_v1 import CreateScoreResponse
61from .score_v1 import CreateScoreSource
62
63__all__ = [*__all__, "CreateScoreRequest", "CreateScoreResponse", "CreateScoreSource"]
MetricsResponse
Observations
ObservationsViews
class CreateScoreRequest(langfuse.api.core.pydantic_utilities.UniversalBaseModel):
15class CreateScoreRequest(UniversalBaseModel):
16    """
17    Examples
18    --------
19    from langfuse.scores import CreateScoreRequest
20
21    CreateScoreRequest(
22        name="novelty",
23        value=0.9,
24        trace_id="cdef-1234-5678-90ab",
25    )
26    """
27
28    id: typing.Optional[str] = None
29    trace_id: typing_extensions.Annotated[
30        typing.Optional[str], FieldMetadata(alias="traceId")
31    ] = None
32    session_id: typing_extensions.Annotated[
33        typing.Optional[str], FieldMetadata(alias="sessionId")
34    ] = None
35    observation_id: typing_extensions.Annotated[
36        typing.Optional[str], FieldMetadata(alias="observationId")
37    ] = None
38    dataset_run_id: typing_extensions.Annotated[
39        typing.Optional[str], FieldMetadata(alias="datasetRunId")
40    ] = None
41    name: str
42    value: CreateScoreValue = pydantic.Field()
43    """
44    The value of the score. Must be passed as string for categorical and text scores, and numeric for boolean and numeric scores. Boolean score values must equal either 1 or 0 (true or false). Text score values must be between 1 and 500 characters.
45    """
46
47    comment: typing.Optional[str] = None
48    metadata: typing.Optional[typing.Dict[str, typing.Any]] = None
49    environment: typing.Optional[str] = pydantic.Field(default=None)
50    """
51    The environment of the score. Can be any lowercase alphanumeric string with hyphens and underscores that does not start with 'langfuse'.
52    """
53
54    queue_id: typing_extensions.Annotated[
55        typing.Optional[str], FieldMetadata(alias="queueId")
56    ] = pydantic.Field(default=None)
57    """
58    The annotation queue referenced by the score. Indicates if score was initially created while processing annotation queue.
59    """
60
61    data_type: typing_extensions.Annotated[
62        typing.Optional[ScoreDataType], FieldMetadata(alias="dataType")
63    ] = pydantic.Field(default=None)
64    """
65    The data type of the score. When passing a configId this field is inferred. Otherwise, this field must be passed or will default to numeric.
66    """
67
68    config_id: typing_extensions.Annotated[
69        typing.Optional[str], FieldMetadata(alias="configId")
70    ] = pydantic.Field(default=None)
71    """
72    Reference a score config on a score. The unique langfuse identifier of a score config. When passing this field, the dataType and stringValue fields are automatically populated.
73    """
74
75    source: typing.Optional[CreateScoreSource] = pydantic.Field(default=None)
76    """
77    The source of the score. Defaults to API. Set to ANNOTATION to prefill scores (e.g. from an LLM) for a human reviewer to verify in an annotation queue. When source is ANNOTATION, a configId is required unless dataType is CORRECTION. EVAL is reserved for internal evaluator outputs and is not accepted on this endpoint.
78    """
79
80    model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(
81        extra="allow", frozen=True
82    )

Examples

from langfuse.scores import CreateScoreRequest

CreateScoreRequest( name="novelty", value=0.9, trace_id="cdef-1234-5678-90ab", )

id: Optional[str] = None
trace_id: Annotated[Optional[str], <langfuse.api.core.serialization.FieldMetadata object at 0x7f11c7567f90>] = None
session_id: Annotated[Optional[str], <langfuse.api.core.serialization.FieldMetadata object at 0x7f11c7566e90>] = None
observation_id: Annotated[Optional[str], <langfuse.api.core.serialization.FieldMetadata object at 0x7f11c7567fd0>] = None
dataset_run_id: Annotated[Optional[str], <langfuse.api.core.serialization.FieldMetadata object at 0x7f11c757c390>] = None
name: str = PydanticUndefined
value: Union[float, str] = PydanticUndefined

The value of the score. Must be passed as string for categorical and text scores, and numeric for boolean and numeric scores. Boolean score values must equal either 1 or 0 (true or false). Text score values must be between 1 and 500 characters.

comment: Optional[str] = None
metadata: Optional[Dict[str, Any]] = None
environment: Optional[str] = None

The environment of the score. Can be any lowercase alphanumeric string with hyphens and underscores that does not start with 'langfuse'.

queue_id: Annotated[Optional[str], <langfuse.api.core.serialization.FieldMetadata object at 0x7f11c757c610>] = None

The annotation queue referenced by the score. Indicates if score was initially created while processing annotation queue.

data_type: Annotated[Optional[langfuse.api.ScoreDataType], <langfuse.api.core.serialization.FieldMetadata object at 0x7f11c757c690>] = None

The data type of the score. When passing a configId this field is inferred. Otherwise, this field must be passed or will default to numeric.

config_id: Annotated[Optional[str], <langfuse.api.core.serialization.FieldMetadata object at 0x7f11c757c790>] = None

Reference a score config on a score. The unique langfuse identifier of a score config. When passing this field, the dataType and stringValue fields are automatically populated.

source: Optional[CreateScoreSource] = None

The source of the score. Defaults to API. Set to ANNOTATION to prefill scores (e.g. from an LLM) for a human reviewer to verify in an annotation queue. When source is ANNOTATION, a configId is required unless dataType is CORRECTION. EVAL is reserved for internal evaluator outputs and is not accepted on this endpoint.

class CreateScoreResponse(langfuse.api.core.pydantic_utilities.UniversalBaseModel):
10class CreateScoreResponse(UniversalBaseModel):
11    id: str = pydantic.Field()
12    """
13    The id of the created object in Langfuse
14    """
15
16    model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(
17        extra="allow", frozen=True
18    )

!!! abstract "Usage Documentation" Models

A base class for creating Pydantic models.

Attributes:
  • __class_vars__: The names of the class variables defined on the model.
  • __private_attributes__: Metadata about the private attributes of the model.
  • __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.
  • __pydantic_complete__: Whether model building is completed, or if there are still undefined fields.
  • __pydantic_core_schema__: The core schema of the model.
  • __pydantic_custom_init__: Whether the model has a custom __init__ function.
  • __pydantic_decorators__: Metadata containing the decorators defined on the model. This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.
  • __pydantic_generic_metadata__: A dictionary containing metadata about generic Pydantic models. The origin and args items map to the [__origin__][genericalias.__origin__] and [__args__][genericalias.__args__] attributes of [generic aliases][types-genericalias], and the parameter item maps to the __parameter__ attribute of generic classes.
  • __pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models.
  • __pydantic_post_init__: The name of the post-init method for the model, if defined.
  • __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel].
  • __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model.
  • __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.
  • __pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects.
  • __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.
  • __pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra] is set to 'allow'.
  • __pydantic_fields_set__: The names of fields explicitly set during instantiation.
  • __pydantic_private__: Values of private attributes set on the model instance.
id: str = PydanticUndefined

The id of the created object in Langfuse

class CreateScoreSource(enum.StrEnum):
11class CreateScoreSource(enum.StrEnum):
12    """
13    Source values accepted when creating a score via the public REST API.
14    EVAL is reserved for internal evaluator outputs and is intentionally not
15    exposed here — use commons.ScoreSource when reading scores.
16    """
17
18    API = "API"
19    ANNOTATION = "ANNOTATION"
20
21    def visit(
22        self,
23        api: typing.Callable[[], T_Result],
24        annotation: typing.Callable[[], T_Result],
25    ) -> T_Result:
26        if self is CreateScoreSource.API:
27            return api()
28        if self is CreateScoreSource.ANNOTATION:
29            return annotation()

Source values accepted when creating a score via the public REST API. EVAL is reserved for internal evaluator outputs and is intentionally not exposed here — use commons.ScoreSource when reading scores.

API = <CreateScoreSource.API: 'API'>
ANNOTATION = <CreateScoreSource.ANNOTATION: 'ANNOTATION'>
def visit( self, api: Callable[[], ~T_Result], annotation: Callable[[], ~T_Result]) -> ~T_Result:
21    def visit(
22        self,
23        api: typing.Callable[[], T_Result],
24        annotation: typing.Callable[[], T_Result],
25    ) -> T_Result:
26        if self is CreateScoreSource.API:
27            return api()
28        if self is CreateScoreSource.ANNOTATION:
29            return annotation()