truncate_for_llm(data: Any, *, max_string_length: int = 200, max_array_items: int = 5, max_object_keys: int = 30, max_depth: int = 6, _current_depth: int = 0) -> Any
Truncate nested data for LLM consumption while preserving structure.
Arrays keep the first max_array_items entries (plus a remaining-count
marker), strings are capped at max_string_length chars, objects keep the
first max_object_keys keys, and nesting beyond max_depth collapses to
a one-line summary. Primitives pass through unchanged.
Source code in datarobot_genai/drtools/panels/truncate.py
| def truncate_for_llm(
data: Any,
*,
max_string_length: int = 200,
max_array_items: int = 5,
max_object_keys: int = 30,
max_depth: int = 6,
_current_depth: int = 0,
) -> Any:
"""Truncate nested data for LLM consumption while preserving structure.
Arrays keep the first ``max_array_items`` entries (plus a remaining-count
marker), strings are capped at ``max_string_length`` chars, objects keep the
first ``max_object_keys`` keys, and nesting beyond ``max_depth`` collapses to
a one-line summary. Primitives pass through unchanged.
"""
if _current_depth >= max_depth:
return _summarize_truncated(data)
if isinstance(data, str):
if len(data) > max_string_length:
return data[:max_string_length] + f"... ({len(data) - max_string_length} more chars)"
return data
recurse_kwargs = {
"max_string_length": max_string_length,
"max_array_items": max_array_items,
"max_object_keys": max_object_keys,
"max_depth": max_depth,
"_current_depth": _current_depth + 1,
}
if isinstance(data, list):
truncated_list: list[Any] = [
truncate_for_llm(item, **recurse_kwargs) for item in data[:max_array_items]
]
if len(data) > max_array_items:
truncated_list.append(f"... ({len(data) - max_array_items} more items)")
return truncated_list
if isinstance(data, dict):
keys = list(data.keys())
truncated_dict: dict[str, Any] = {
key: truncate_for_llm(data[key], **recurse_kwargs) for key in keys[:max_object_keys]
}
if len(keys) > max_object_keys:
truncated_dict["..."] = f"({len(keys) - max_object_keys} more keys)"
return truncated_dict
return data
|