Skip to content

Commit 5500f28

Browse files
committed
fix: guard against very small max_length in table truncation
Handle edge cases where max_length is <= 0 or smaller than the truncation note itself. Made-with: Cursor
1 parent c768a65 commit 5500f28

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

elementary/utils/json_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ def list_of_dicts_to_markdown_table(
138138
if max_length is None or len(full_table) <= max_length:
139139
return full_table
140140

141+
if max_length <= 0:
142+
return ""
141143
truncation_note = "\n(truncated)"
144+
if max_length <= len(truncation_note):
145+
return "(truncated)"[:max_length]
142146
effective_max = max_length - len(truncation_note)
143147
for row_count in range(len(processed_data) - 1, 0, -1):
144148
table = tabulate(
@@ -156,6 +160,6 @@ def list_of_dicts_to_markdown_table(
156160
tablefmt="github",
157161
disable_numparse=True,
158162
)
159-
if effective_max <= 0 or len(single_row_table) <= effective_max:
163+
if len(single_row_table) <= effective_max:
160164
return single_row_table + truncation_note
161165
return single_row_table[:effective_max].rstrip() + truncation_note

0 commit comments

Comments
 (0)