Skip to content

Commit 5fa6582

Browse files
Validate input length in generate_id utility (#460)
1 parent 3f36754 commit 5fa6582

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

  • agentlightning/utils

agentlightning/utils/id.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77

88

99
def generate_id(length: int) -> str:
10-
"""Generate a random ID of the given length.
10+
"""Generate a random hexadecimal ID of the given length.
1111
1212
Args:
13-
length: The length of the ID to generate.
13+
length: The desired length of the generated ID. Must be a positive integer.
1414
1515
Returns:
16-
A random ID of the given length.
16+
A random hexadecimal ID string of the given length.
17+
18+
Raises:
19+
ValueError: If length is not a positive integer.
1720
"""
21+
if length <= 0:
22+
raise ValueError("length must be a positive integer")
23+
1824
return hashlib.sha1(uuid.uuid4().bytes).hexdigest()[:length]

0 commit comments

Comments
 (0)