Skip to content

Commit 56f77de

Browse files
committed
feat(batch-evaluation): allow passing fields param for efficient trace fetching
1 parent ea5658f commit 56f77de

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

langfuse/_client/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@
7979
from langfuse._utils.parse_error import handle_fern_exception
8080
from langfuse._utils.prompt_cache import PromptCache
8181
from langfuse.api.resources.commons.errors.error import Error
82+
from langfuse.api.resources.commons.errors.not_found_error import NotFoundError
8283
from langfuse.api.resources.commons.types import DatasetRunWithItems
8384
from langfuse.api.resources.datasets.types import (
8485
DeleteDatasetRunResponse,
8586
PaginatedDatasetRuns,
8687
)
87-
from langfuse.api.resources.commons.errors.not_found_error import NotFoundError
8888
from langfuse.api.resources.ingestion.types.score_body import ScoreBody
8989
from langfuse.api.resources.prompts.types import (
9090
CreatePromptRequest_Chat,
@@ -3081,6 +3081,7 @@ def run_batched_evaluation(
30813081
mapper: MapperFunction,
30823082
filter: Optional[str] = None,
30833083
fetch_batch_size: int = 50,
3084+
fetch_trace_fields: Optional[str] = None,
30843085
max_items: Optional[int] = None,
30853086
max_retries: int = 3,
30863087
evaluators: List[EvaluatorFunction],
@@ -3123,6 +3124,7 @@ def run_batched_evaluation(
31233124
Default: None (fetches all items).
31243125
fetch_batch_size: Number of items to fetch per API call and hold in memory.
31253126
Larger values may be faster but use more memory. Default: 50.
3127+
fetch_trace_fields: Comma-separated list of fields to include in the when fetching traces. Available field groups: 'core' (always included), 'io' (input, output, metadata), 'scores', 'observations', 'metrics'. If not specified, all fields are returned. Example: 'core,scores,metrics'. Note: Excluded 'observations' or 'scores' fields return empty arrays; excluded 'metrics' returns -1 for 'totalCost' and 'latency'. Only relevant if scope is 'traces'.
31263128
max_items: Maximum total number of items to process. If None, processes all
31273129
items matching the filter. Useful for testing or limiting evaluation runs.
31283130
Default: None (process all).
@@ -3291,6 +3293,7 @@ def composite_evaluator(*, item, evaluations):
32913293
evaluators=evaluators,
32923294
filter=filter,
32933295
fetch_batch_size=fetch_batch_size,
3296+
fetch_trace_fields=fetch_trace_fields,
32943297
max_items=max_items,
32953298
max_concurrency=max_concurrency,
32963299
composite_evaluator=composite_evaluator,

langfuse/batch_evaluation.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ async def run_async(
846846
evaluators: List[EvaluatorFunction],
847847
filter: Optional[str] = None,
848848
fetch_batch_size: int = 50,
849+
fetch_trace_fields: Optional[str] = None,
849850
max_items: Optional[int] = None,
850851
max_concurrency: int = 50,
851852
composite_evaluator: Optional[CompositeEvaluatorFunction] = None,
@@ -866,6 +867,7 @@ async def run_async(
866867
evaluators: List of evaluation functions to run on each item.
867868
filter: JSON filter string for querying items.
868869
fetch_batch_size: Number of items to fetch per API call.
870+
fetch_trace_fields: Comma-separated list of fields to include in the when fetching traces. Available field groups: 'core' (always included), 'io' (input, output, metadata), 'scores', 'observations', 'metrics'. If not specified, all fields are returned. Example: 'core,scores,metrics'. Note: Excluded 'observations' or 'scores' fields return empty arrays; excluded 'metrics' returns -1 for 'totalCost' and 'latency'. Only relevant if scope is 'traces'.
869871
max_items: Maximum number of items to process (None = all).
870872
max_concurrency: Maximum number of concurrent evaluations.
871873
composite_evaluator: Optional function to create composite scores.
@@ -935,6 +937,7 @@ async def run_async(
935937
page=page,
936938
limit=fetch_batch_size,
937939
max_retries=max_retries,
940+
fields=fetch_trace_fields,
938941
)
939942
except Exception as e:
940943
# Failed after max_retries - create resume token and return
@@ -1114,6 +1117,7 @@ async def _fetch_batch_with_retry(
11141117
page: int,
11151118
limit: int,
11161119
max_retries: int,
1120+
fields: Optional[str],
11171121
) -> List[Union[TraceWithFullDetails, ObservationsView]]:
11181122
"""Fetch a batch of items with retry logic.
11191123
@@ -1124,6 +1128,7 @@ async def _fetch_batch_with_retry(
11241128
limit: Number of items per page.
11251129
max_retries: Maximum number of retry attempts.
11261130
verbose: Whether to log retry attempts.
1131+
fields: Trace fields to fetch
11271132
11281133
Returns:
11291134
List of items from the API.
@@ -1137,6 +1142,7 @@ async def _fetch_batch_with_retry(
11371142
limit=limit,
11381143
filter=filter,
11391144
request_options={"max_retries": max_retries},
1145+
fields=fields,
11401146
) # type: ignore
11411147
return list(response.data) # type: ignore
11421148
elif scope == "observations":

0 commit comments

Comments
 (0)