Skip to main content

truncate_json_array

Function truncate_json_array 

Source
pub fn truncate_json_array(value: &mut Value, key: &str, max: usize)
Expand description

Truncate a JSON array in-place, appending a note about total count.

Useful for keeping context window manageable (segments, beats, etc.).

use baml_agent::helpers::truncate_json_array;
let mut v = serde_json::json!({"items": [1,2,3,4,5,6,7,8,9,10,11,12]});
truncate_json_array(&mut v, "items", 3);
assert_eq!(v["items"].as_array().unwrap().len(), 4); // 3 items + note