NVIDIA / NVIDIA/NeMo-Agent-Toolkit

truncate_string returns a string longer than max_length for small limits

Open Beginner friendly
#2,220 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.6k
Forks
762
Avg merge
21h 28m
Merged PRs (30d)
27

Description

Describe the bug

nat.utils.string_utils.truncate_string(text, max_length) is documented to
"truncate a string to a maximum length," but for max_length < 3 it returns a
string longer than max_length. When the input needs truncating it does:

return text[:max_length - 3] + "..."

For max_length < 3, max_length - 3 is negative, so text[:max_length - 3]
slices from the end of the string instead of the start, and the "..." is then
appended — producing output that is both longer than max_length and cut from
the wrong end.

Reproduction
from nat.utils.string_utils import truncate_string

truncate_string("abcdefghij", 2)   # -> 'abcdefghi...'  (12 chars for max_length=2)
truncate_string("abcdefghij", 1)   # -> 'abcdefgh...'   (11 chars for max_length=1)

Expected: the result never exceeds max_length (e.g. "..", "." — as much of
the ellipsis as fits).

For max_length >= 3 the function already behaves correctly
(truncate_string("abcdefghij", 5) == "ab..."); only small limits are affected.

Proposed fix

Guard the small-max_length case so the ellipsis is truncated to fit instead of
using a negative slice, keeping the existing behavior for max_length >= 3.
Happy to send a small PR with tests.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at nat.utils.string_utils.truncate_string and reproduce the small-limit cases described in the issue. Add focused tests for max_length values 1 and 2, while confirming the existing max_length >= 3 behavior remains unchanged; done means the result never exceeds max_length and the tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
88/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.