-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlangfuse.py
More file actions
524 lines (437 loc) · 19.8 KB
/
langfuse.py
File metadata and controls
524 lines (437 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
"""Langfuse adapter for Eval Protocol.
This adapter allows pulling data from Langfuse deployments and converting it
to EvaluationRow format for use in evaluation pipelines.
"""
from langfuse.api.resources.commons.types.observations_view import ObservationsView
import logging
import random
import time
from datetime import datetime, timedelta
from typing import Any, Dict, Iterator, List, Optional, Callable, TYPE_CHECKING, cast, Protocol
from eval_protocol.models import EvaluationRow, InputMetadata, Message
logger = logging.getLogger(__name__)
class TraceConverter(Protocol):
"""Protocol for custom trace-to-EvaluationRow converter functions.
A converter function should take a Langfuse trace along with processing
options and return an EvaluationRow or None to skip the trace.
"""
def __call__(
self,
trace: "TraceWithFullDetails",
include_tool_calls: bool,
span_name: Optional[str],
) -> Optional[EvaluationRow]:
"""Convert a Langfuse trace to an EvaluationRow.
Args:
trace: The Langfuse trace object to convert
include_tool_calls: Whether to include tool calling information
span_name: Optional span name to extract messages from
Returns:
EvaluationRow or None if the trace should be skipped
"""
...
try:
from langfuse import get_client # pyright: ignore[reportPrivateImportUsage]
from langfuse.api.resources.trace.types.traces import Traces
from langfuse.api.resources.commons.types.trace import Trace
from langfuse.api.resources.commons.types.trace_with_full_details import TraceWithFullDetails
LANGFUSE_AVAILABLE = True
except ImportError: # pragma: no cover - optional dependency
LANGFUSE_AVAILABLE = False
if TYPE_CHECKING: # pragma: no cover - import is optional at runtime
from langfuse.client import Langfuse as _LangfuseClient # type: ignore[import-not-found]
else:
_LangfuseClient = Any
def convert_trace_to_evaluation_row(
trace: TraceWithFullDetails, include_tool_calls: bool = True, span_name: Optional[str] = None
) -> Optional[EvaluationRow]:
"""Convert a Langfuse trace to EvaluationRow format.
Args:
trace: Langfuse trace object
include_tool_calls: Whether to include tool calling information
span_name: If provided, extract messages from generations within this named span
converter: Optional custom converter implementing TraceConverter protocol
Returns:
EvaluationRow or None if conversion fails
"""
try:
# Extract messages from trace input and output
messages = extract_messages_from_trace(trace, include_tool_calls, span_name)
# Extract tools if available
tools = None
if include_tool_calls and isinstance(trace.input, dict) and "tools" in trace.input:
tools = trace.input["tools"]
if not messages:
return None
return EvaluationRow(
messages=messages,
tools=tools,
input_metadata=InputMetadata(
session_data={
"langfuse_trace_id": trace.id, # Store the trace ID here
}
),
)
except (AttributeError, ValueError, KeyError) as e:
logger.error("Error converting trace %s: %s", trace.id, e)
return None
def extract_messages_from_trace(
trace: TraceWithFullDetails, include_tool_calls: bool = True, span_name: Optional[str] = None
) -> List[Message]:
"""Extract messages from Langfuse trace input and output.
Args:
trace: Langfuse trace object
include_tool_calls: Whether to include tool calling information
span_name: If provided, extract messages from generations within this named span
Returns:
List of Message objects
"""
messages = []
if span_name: # Look for a generation tied to a span name
try:
# Find the final generation in the named span
gen: ObservationsView | None = find_final_generation_in_span(trace, span_name)
if not gen:
return messages
# Extract messages from generation input and output
if gen.input:
messages.extend(extract_messages_from_data(gen.input, include_tool_calls))
if gen.output:
messages.extend(extract_messages_from_data(gen.output, include_tool_calls))
return messages
except Exception as e:
logger.error("Failed to extract messages from span '%s' in trace %s: %s", span_name, trace.id, e)
return messages
else:
try:
# Extract messages from trace input and output
if trace.input:
messages.extend(extract_messages_from_data(trace.input, include_tool_calls))
if trace.output:
messages.extend(extract_messages_from_data(trace.output, include_tool_calls))
except (AttributeError, ValueError, KeyError) as e:
logger.warning("Error processing trace %s: %s", trace.id, e)
return messages
def extract_messages_from_data(data, include_tool_calls: bool) -> List[Message]:
"""Extract messages from data (works for both input and output).
Args:
data: Data from trace or generation (input or output)
include_tool_calls: Whether to include tool calling information
Returns:
List of Message objects
"""
messages = []
if isinstance(data, dict):
if "messages" in data:
# OpenAI-style messages format
for msg in data["messages"]:
messages.append(dict_to_message(msg, include_tool_calls))
elif "role" in data:
# Single message format
messages.append(dict_to_message(data, include_tool_calls))
elif "prompt" in data:
# Simple prompt format
messages.append(Message(role="user", content=str(data["prompt"])))
elif "content" in data:
# Simple content format
messages.append(Message(role="assistant", content=str(data["content"])))
else:
# Fallback: treat as single message
messages.append(dict_to_message(data, include_tool_calls))
elif isinstance(data, list):
# Direct list of message dicts
for msg in data:
if isinstance(msg, dict):
messages.append(dict_to_message(msg, include_tool_calls))
elif isinstance(data, str):
# Simple string - role depends on context, default to user
messages.append(Message(role="user", content=data))
return messages
def dict_to_message(msg_dict: Dict[str, Any], include_tool_calls: bool = True) -> Message:
"""Convert a dictionary to a Message object.
Args:
msg_dict: Dictionary containing message data
include_tool_calls: Whether to include tool calling information
Returns:
Message object
"""
# Extract basic message components
role = msg_dict.get("role", "assistant")
content = msg_dict.get("content")
name = msg_dict.get("name")
# Handle tool calls if enabled
tool_calls = None
tool_call_id = None
function_call = None
if include_tool_calls:
if "tool_calls" in msg_dict:
tool_calls = msg_dict["tool_calls"]
if "tool_call_id" in msg_dict:
tool_call_id = msg_dict["tool_call_id"]
if "function_call" in msg_dict:
function_call = msg_dict["function_call"]
return Message(
role=role,
content=content,
name=name,
tool_call_id=tool_call_id,
tool_calls=tool_calls,
function_call=function_call,
)
def find_final_generation_in_span(trace: TraceWithFullDetails, span_name: str) -> ObservationsView | None:
"""Find the final generation within a named span that contains full message history.
Args:
trace: Langfuse trace object
span_name: Name of the span to search for
Returns:
The final generation object, or None if not found
"""
# Get all observations from the trace
all_observations = trace.observations
# Find a span with the given name that has generation children
parent_span = None
for obs in all_observations:
if obs.name == span_name and obs.type == "SPAN":
# Check if this span has generation children
has_generations = any(
child.type == "GENERATION" and child.parent_observation_id == obs.id for child in all_observations
)
if has_generations:
parent_span = obs
break
if not parent_span:
logger.warning("No span named '%s' found in trace %s", span_name, trace.id)
return None
# Find all generations within this span
generations: List[ObservationsView] = []
for obs in all_observations:
if obs.type == "GENERATION" and obs.parent_observation_id == parent_span.id:
generations.append(obs)
if not generations:
logger.warning("No generations found in span '%s' in trace %s", span_name, trace.id)
return None
# Sort generations by start time for chronological order
generations.sort(key=lambda x: x.start_time)
# Return the final generation (contains full message history)
return generations[-1]
class LangfuseAdapter:
"""Adapter to pull data from Langfuse and convert to EvaluationRow format.
This adapter can pull both chat conversations and tool calling traces from
Langfuse deployments and convert them into the EvaluationRow format expected
by the evaluation protocol.
Examples:
Basic usage:
>>> adapter = LangfuseAdapter(
... public_key="your_public_key",
... secret_key="your_secret_key",
... host="https://your-langfuse-deployment.com"
... )
>>> rows = list(adapter.get_evaluation_rows(limit=10))
Filter by specific criteria:
>>> rows = list(adapter.get_evaluation_rows(
... limit=50,
... tags=["production"],
... user_id="specific_user",
... from_timestamp=datetime.now() - timedelta(days=7)
... ))
"""
def __init__(self):
"""Initialize the Langfuse adapter."""
if not LANGFUSE_AVAILABLE:
raise ImportError("Langfuse not installed. Install with: pip install 'eval-protocol[langfuse]'")
client_factory = cast(Callable[[], _LangfuseClient], get_client)
self.client = client_factory()
def get_evaluation_rows(
self,
limit: int = 100,
sample_size: Optional[int] = None,
tags: Optional[List[str]] = None,
user_id: Optional[str] = None,
session_id: Optional[str] = None,
name: Optional[str] = None,
environment: Optional[str] = None,
version: Optional[str] = None,
release: Optional[str] = None,
fields: Optional[str] = None,
hours_back: Optional[int] = None,
from_timestamp: Optional[datetime] = None,
to_timestamp: Optional[datetime] = None,
include_tool_calls: bool = True,
sleep_between_gets: float = 2.5,
max_retries: int = 3,
span_name: Optional[str] = None,
converter: Optional[TraceConverter] = None,
) -> List[EvaluationRow]:
"""Pull traces from Langfuse and convert to EvaluationRow format.
Args:
limit: Max number of trace summaries to collect via pagination (pre-sampling)
sample_size: Optional number of traces to randomly sample from collected summaries (if None, process all)
tags: Filter by specific tags
user_id: Filter by user ID
session_id: Filter by session ID
name: Filter by trace name
environment: Filter by environment (e.g., production, staging, development)
version: Filter by trace version
release: Filter by trace release
fields: Comma-separated list of fields to include (e.g., 'core,scores,metrics')
hours_back: Filter traces from this many hours ago
from_timestamp: Explicit start time (overrides hours_back)
to_timestamp: Explicit end time (overrides hours_back)
include_tool_calls: Whether to include tool calling traces
sleep_between_gets: Sleep time between individual trace.get() calls (2.5s for 30 req/min limit)
max_retries: Maximum retries for rate limit errors
span_name: If provided, extract messages from generations within this named span
converter: Optional custom converter implementing TraceConverter protocol.
If provided, this will be used instead of the default conversion logic.
Returns:
List[EvaluationRow]: Converted evaluation rows
"""
eval_rows = []
# Determine time window: explicit from/to takes precedence over hours_back
if from_timestamp is None and to_timestamp is None and hours_back:
to_timestamp = datetime.now()
from_timestamp = to_timestamp - timedelta(hours=hours_back)
# Collect trace summaries via pagination (up to limit)
all_traces = []
page = 1
collected = 0
while collected < limit:
current_page_limit = min(100, limit - collected) # Langfuse API max is 100
logger.debug(
"Fetching page %d with limit %d (collected: %d/%d)", page, current_page_limit, collected, limit
)
# Fetch trace list with retry logic
traces = None
list_retries = 0
while list_retries < max_retries:
try:
traces = self.client.api.trace.list(
page=page,
limit=current_page_limit,
tags=tags,
user_id=user_id,
session_id=session_id,
name=name,
environment=environment,
version=version,
release=release,
fields=fields,
from_timestamp=from_timestamp,
to_timestamp=to_timestamp,
order_by="timestamp.desc",
)
break
except Exception as e:
list_retries += 1
if "429" in str(e) and list_retries < max_retries:
sleep_time = 2**list_retries # Exponential backoff
logger.warning(
"Rate limit hit on trace.list(), retrying in %ds (attempt %d/%d)",
sleep_time,
list_retries,
max_retries,
)
time.sleep(sleep_time)
else:
logger.error("Failed to fetch trace list after %d retries: %s", max_retries, e)
return eval_rows # Return what we have so far
if not traces or not traces.data:
logger.debug("No more traces found on page %d", page)
break
logger.debug("Collected %d traces from page %d", len(traces.data), page)
all_traces.extend(traces.data)
collected += len(traces.data)
# Check if we have more pages
if hasattr(traces.meta, "page") and hasattr(traces.meta, "total_pages"):
if traces.meta.page >= traces.meta.total_pages:
break
elif len(traces.data) < current_page_limit:
break
page += 1
if not all_traces:
logger.debug("No traces found")
return eval_rows
# Optionally sample traces to fetch full details (respect rate limits)
if sample_size is not None:
actual_sample_size = min(sample_size, len(all_traces))
selected_traces = random.sample(all_traces, actual_sample_size)
logger.debug("Randomly selected %d traces from %d collected", actual_sample_size, len(all_traces))
else:
selected_traces = all_traces
logger.debug("Processing all %d collected traces (no sampling)", len(all_traces))
# Process each selected trace with sleep and retry logic
for trace_info in selected_traces:
# Sleep between gets to avoid rate limits
if sleep_between_gets > 0:
time.sleep(sleep_between_gets)
# Fetch full trace details with retry logic
trace_full = None
detail_retries = 0
while detail_retries < max_retries:
try:
trace_full = self.client.api.trace.get(trace_info.id)
break
except Exception as e:
detail_retries += 1
if "429" in str(e) and detail_retries < max_retries:
sleep_time = 2**detail_retries # Exponential backoff
logger.warning(
"Rate limit hit on trace.get(%s), retrying in %ds (attempt %d/%d)",
trace_info.id,
sleep_time,
detail_retries,
max_retries,
)
time.sleep(sleep_time)
else:
logger.warning("Failed to fetch trace %s after %d retries: %s", trace_info.id, max_retries, e)
break # Skip this trace
if trace_full:
try:
if converter:
eval_row = converter(trace_full, include_tool_calls, span_name)
else:
eval_row = convert_trace_to_evaluation_row(trace_full, include_tool_calls, span_name)
if eval_row:
eval_rows.append(eval_row)
except (AttributeError, ValueError, KeyError) as e:
logger.warning("Failed to convert trace %s: %s", trace_info.id, e)
continue
logger.info(
"Successfully processed %d selected traces into %d evaluation rows", len(selected_traces), len(eval_rows)
)
return eval_rows
def get_evaluation_rows_by_ids(
self,
trace_ids: List[str],
include_tool_calls: bool = True,
span_name: Optional[str] = None,
converter: Optional[TraceConverter] = None,
) -> List[EvaluationRow]:
"""Get specific traces by their IDs and convert to EvaluationRow format.
Args:
trace_ids: List of trace IDs to fetch
include_tool_calls: Whether to include tool calling traces
span_name: If provided, extract messages from generations within this named span
converter: Optional custom converter implementing TraceConverter protocol.
If provided, this will be used instead of the default conversion logic.
Returns:
List[EvaluationRow]: Converted evaluation rows
"""
eval_rows = []
for trace_id in trace_ids:
try:
trace: TraceWithFullDetails = self.client.api.trace.get(trace_id)
if converter:
eval_row = converter(trace, include_tool_calls, span_name)
else:
eval_row = convert_trace_to_evaluation_row(trace, include_tool_calls, span_name)
if eval_row:
eval_rows.append(eval_row)
except (AttributeError, ValueError, KeyError) as e:
logger.warning("Failed to fetch/convert trace %s: %s", trace_id, e)
continue
return eval_rows
def create_langfuse_adapter() -> LangfuseAdapter:
"""Factory function to create a Langfuse adapter."""
return LangfuseAdapter()