Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion backend/utils/str_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
import re
from typing import List, Optional

logger = logging.getLogger(__name__)


def remove_think_blocks(text: str) -> str:
"""Remove <think>...</think> blocks including inner content."""
Expand Down Expand Up @@ -36,4 +39,16 @@ def convert_string_to_list(items_str: Optional[str]) -> List[int]:
"""
if not items_str or items_str.strip() == "":
return []
return [int(item.strip()) for item in items_str.split(",") if item.strip().isdigit()]

items = []
for raw_item in items_str.split(","):
item = raw_item.strip()
if not item:
continue
try:
items.append(int(item))
except ValueError:
logger.warning(
"convert_string_to_list: dropping non-integer entry %r", item
)
return items
24 changes: 23 additions & 1 deletion test/backend/utils/test_str_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import pytest
from backend.utils.str_utils import remove_think_blocks, convert_list_to_string

from backend.utils.str_utils import (
convert_list_to_string,
convert_string_to_list,
remove_think_blocks,
)


class TestStrUtils:
Expand Down Expand Up @@ -89,6 +94,23 @@ def test_convert_list_to_string_zero_and_negative(self):
result = convert_list_to_string([0, -1, 5])
assert result == "0,-1,5"

def test_convert_string_to_list_round_trips_signed_integers(self):
"""Serialized signed integers should round-trip without data loss"""
serialized = convert_list_to_string([0, -1, 5])

assert convert_string_to_list(serialized) == [0, -1, 5]

def test_convert_string_to_list_accepts_whitespace_and_explicit_signs(self):
"""Whitespace and explicit integer signs should be accepted"""
assert convert_string_to_list(" -2, +3, 0 ") == [-2, 3, 0]

def test_convert_string_to_list_warns_and_keeps_valid_entries(self, caplog):
"""Malformed entries should be reported without discarding valid values"""
result = convert_string_to_list("invalid, 1, , -2")

assert result == [1, -2]
assert "dropping non-integer entry 'invalid'" in caplog.text


if __name__ == "__main__":
pytest.main()