|
| 1 | +# (C) 2026 GoodData Corporation |
| 2 | +"""Langfuse experiment root-span construction and score-target resolution.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import uuid |
| 8 | +from dataclasses import dataclass |
| 9 | +from datetime import datetime |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +from gooddata_eval.core.langfuse.otlp import ( |
| 13 | + ATTR_ENVIRONMENT, |
| 14 | + ATTR_EXPERIMENT_DATASET_ID, |
| 15 | + ATTR_EXPERIMENT_DESCRIPTION, |
| 16 | + ATTR_EXPERIMENT_ID, |
| 17 | + ATTR_EXPERIMENT_ITEM_EXPECTED_OUTPUT, |
| 18 | + ATTR_EXPERIMENT_ITEM_ID, |
| 19 | + ATTR_EXPERIMENT_ITEM_METADATA_PREFIX, |
| 20 | + ATTR_EXPERIMENT_ITEM_ROOT_OBSERVATION_ID, |
| 21 | + ATTR_EXPERIMENT_METADATA_PREFIX, |
| 22 | + ATTR_EXPERIMENT_NAME, |
| 23 | + ATTR_OBSERVATION_INPUT, |
| 24 | + ATTR_OBSERVATION_METADATA_PREFIX, |
| 25 | + ATTR_OBSERVATION_OUTPUT, |
| 26 | + ATTR_OBSERVATION_TYPE, |
| 27 | + ATTR_SESSION_ID, |
| 28 | + ATTR_TRACE_METADATA_PREFIX, |
| 29 | + ATTR_TRACE_NAME, |
| 30 | + ATTR_TRACE_TAGS, |
| 31 | + ATTR_VERSION, |
| 32 | + Span, |
| 33 | + flatten_metadata, |
| 34 | + new_span_id, |
| 35 | + new_trace_id, |
| 36 | + otlp_attribute, |
| 37 | +) |
| 38 | + |
| 39 | +# Fixed namespace for deriving experiment ids from run names — arbitrary but stable across processes. |
| 40 | +_EXPERIMENT_NAMESPACE = uuid.UUID("6f6e2f5a-2f0e-4f0c-9c1b-8f6f2a3b9d10") |
| 41 | + |
| 42 | + |
| 43 | +def experiment_id_for(run_name: str) -> str: |
| 44 | + return str(uuid.uuid5(_EXPERIMENT_NAMESPACE, run_name)) |
| 45 | + |
| 46 | + |
| 47 | +@dataclass(frozen=True) |
| 48 | +class ExperimentRun: |
| 49 | + name: str |
| 50 | + dataset_id: str |
| 51 | + metadata: dict[str, Any] | None = None |
| 52 | + description: str | None = None |
| 53 | + |
| 54 | + |
| 55 | +@dataclass(frozen=True) |
| 56 | +class ExperimentItem: |
| 57 | + item_id: str |
| 58 | + input: Any = None |
| 59 | + output: Any = None |
| 60 | + expected_output: Any = None |
| 61 | + metadata: dict[str, Any] | None = None |
| 62 | + |
| 63 | + |
| 64 | +def build_experiment_root_span( |
| 65 | + run: ExperimentRun | None, |
| 66 | + item: ExperimentItem, |
| 67 | + *, |
| 68 | + start: datetime, |
| 69 | + end: datetime, |
| 70 | + trace_name: str, |
| 71 | + session_id: str | None = None, |
| 72 | + version: str | None = None, |
| 73 | + tags: tuple[str, ...] = (), |
| 74 | + observation_metadata: dict[str, Any] | None = None, |
| 75 | + trace_metadata: dict[str, Any] | None = None, |
| 76 | + environment: str | None = None, |
| 77 | +) -> Span: |
| 78 | + """Build the single root span gd-eval emits per (dataset item, run). |
| 79 | +
|
| 80 | + With `run=None` this is a plain observation span carrying no `langfuse.experiment.*` |
| 81 | + attributes at all — used when there is no experiment to attach the item to. |
| 82 | + """ |
| 83 | + if end < start: |
| 84 | + end = start |
| 85 | + span_id = new_span_id() |
| 86 | + |
| 87 | + attributes: list[dict[str, Any]] = [otlp_attribute(ATTR_OBSERVATION_TYPE, "span")] |
| 88 | + if item.input is not None: |
| 89 | + attributes.append(otlp_attribute(ATTR_OBSERVATION_INPUT, json.dumps(item.input, default=str))) |
| 90 | + if item.output is not None: |
| 91 | + attributes.append(otlp_attribute(ATTR_OBSERVATION_OUTPUT, json.dumps(item.output, default=str))) |
| 92 | + attributes.extend(flatten_metadata(ATTR_OBSERVATION_METADATA_PREFIX, observation_metadata)) |
| 93 | + |
| 94 | + attributes.append(otlp_attribute(ATTR_TRACE_NAME, trace_name)) |
| 95 | + if session_id is not None: |
| 96 | + attributes.append(otlp_attribute(ATTR_SESSION_ID, session_id)) |
| 97 | + if version is not None: |
| 98 | + attributes.append(otlp_attribute(ATTR_VERSION, version)) |
| 99 | + if environment is not None: |
| 100 | + attributes.append(otlp_attribute(ATTR_ENVIRONMENT, environment)) |
| 101 | + if tags: |
| 102 | + attributes.append(otlp_attribute(ATTR_TRACE_TAGS, list(tags))) |
| 103 | + attributes.extend(flatten_metadata(ATTR_TRACE_METADATA_PREFIX, trace_metadata)) |
| 104 | + |
| 105 | + if run is not None: |
| 106 | + attributes.append(otlp_attribute(ATTR_EXPERIMENT_ID, experiment_id_for(run.name))) |
| 107 | + attributes.append(otlp_attribute(ATTR_EXPERIMENT_NAME, run.name)) |
| 108 | + attributes.append(otlp_attribute(ATTR_EXPERIMENT_DATASET_ID, run.dataset_id)) |
| 109 | + if run.description is not None: |
| 110 | + attributes.append(otlp_attribute(ATTR_EXPERIMENT_DESCRIPTION, run.description)) |
| 111 | + attributes.extend(flatten_metadata(ATTR_EXPERIMENT_METADATA_PREFIX, run.metadata)) |
| 112 | + |
| 113 | + attributes.append(otlp_attribute(ATTR_EXPERIMENT_ITEM_ID, item.item_id)) |
| 114 | + attributes.append(otlp_attribute(ATTR_EXPERIMENT_ITEM_ROOT_OBSERVATION_ID, span_id)) |
| 115 | + if item.expected_output is not None: |
| 116 | + attributes.append( |
| 117 | + otlp_attribute(ATTR_EXPERIMENT_ITEM_EXPECTED_OUTPUT, json.dumps(item.expected_output, default=str)) |
| 118 | + ) |
| 119 | + attributes.extend(flatten_metadata(ATTR_EXPERIMENT_ITEM_METADATA_PREFIX, item.metadata)) |
| 120 | + |
| 121 | + return Span(trace_id=new_trace_id(), span_id=span_id, name=trace_name, start=start, end=end, attributes=attributes) |
| 122 | + |
| 123 | + |
| 124 | +class ScoreTarget(str): |
| 125 | + """Where a score for one evaluated item is written: the gen-ai trace, gd-eval's own |
| 126 | + experiment root observation, or both. |
| 127 | +
|
| 128 | + The `str` value is the gen-ai trace id when present, else the experiment trace id, else |
| 129 | + empty — so a `ScoreTarget` can be used directly wherever a plain trace id string was used. |
| 130 | + """ |
| 131 | + |
| 132 | + gen_ai_trace_id: str | None |
| 133 | + experiment_trace_id: str | None |
| 134 | + experiment_span_id: str | None |
| 135 | + |
| 136 | + def __new__( |
| 137 | + cls, |
| 138 | + gen_ai_trace_id: str | None = None, |
| 139 | + experiment_trace_id: str | None = None, |
| 140 | + experiment_span_id: str | None = None, |
| 141 | + ) -> ScoreTarget: |
| 142 | + value = gen_ai_trace_id or experiment_trace_id or "" |
| 143 | + instance = super().__new__(cls, value) |
| 144 | + instance.gen_ai_trace_id = gen_ai_trace_id |
| 145 | + instance.experiment_trace_id = experiment_trace_id |
| 146 | + instance.experiment_span_id = experiment_span_id |
| 147 | + return instance |
| 148 | + |
| 149 | + def destinations(self) -> list[tuple[str, str | None]]: |
| 150 | + """Score write destinations as `(trace_id, observation_id)` pairs, gen-ai first.""" |
| 151 | + targets: list[tuple[str, str | None]] = [] |
| 152 | + if self.gen_ai_trace_id: |
| 153 | + targets.append((self.gen_ai_trace_id, None)) |
| 154 | + if self.experiment_trace_id: |
| 155 | + targets.append((self.experiment_trace_id, self.experiment_span_id)) |
| 156 | + return targets |
0 commit comments