MCPcopy
hub / github.com/Agenta-AI/agenta / get_field_value_from_trace_tree_v2

Function get_field_value_from_trace_tree_v2

api/oss/src/utils/traces.py:366–413  ·  view source on GitHub ↗

Retrieve the value of the key from the trace tree. Args: tree (Dict[str, Any]): The nested dictionary to retrieve the value from. i.e. inline trace e.g. tree["spans"]["rag"]["spans"]["retriever"]["internals"]["prompt"] key (str): The dot-separated ke

(
    tree: Dict[str, Any],
    field: str,
)

Source from the content-addressed store, hash-verified

364
365
366def get_field_value_from_trace_tree_v2(
367 tree: Dict[str, Any],
368 field: str,
369):
370 """
371 Retrieve the value of the key from the trace tree.
372
373 Args:
374 tree (Dict[str, Any]): The nested dictionary to retrieve the value from.
375 i.e. inline trace
376 e.g. tree["spans"]["rag"]["spans"]["retriever"]["internals"]["prompt"]
377 key (str): The dot-separated key to access the value.
378 e.g. rag.summarizer[0].outputs.report
379
380 Returns:
381 Dict[str, Any]: The retrieved value or None if the key does not exist or an error occurs.
382 """
383
384 separate_by_spans_key = True
385
386 parts = field.split(".")
387
388 try:
389 for part in parts:
390 # by default, expects something like 'retriever'
391 key, idx = _parse_field_part(part)
392
393 # before 'SPECIAL_KEYS', spans are nested within a 'spans' key
394 # e.g. trace["spans"]["rag"]["spans"]["retriever"]...
395 if key in SPECIAL_KEYS:
396 separate_by_spans_key = False
397
398 # after 'SPECIAL_KEYS', it is a normal dict.
399 # e.g. trace[...]["internals"]["prompt"]
400 if separate_by_spans_key:
401 tree = tree[SPANS_KEY]
402
403 tree = tree[key]
404
405 if idx is not None:
406 tree = tree[idx]
407
408 return tree
409
410 # Suppress all Exception and leave Exception management to the caller.
411 except Exception as e:
412 log.error(f"Error retrieving trace value from key: {traceback.format_exc()}")
413 return None
414
415
416def get_field_value_from_trace_tree_v3(trace_data: Dict[str, any], key: str):

Callers 1

Calls 2

_parse_field_partFunction · 0.85
errorMethod · 0.45

Tested by

no test coverage detected