feat: Add MAX_ITEMS_LENGTH constant and implement list truncation in ResultDataResponse (#5807)

* feat: add MAX_ITEMS_LENGTH constant and implement list truncation in ResultDataResponse

- Introduced a new constant MAX_ITEMS_LENGTH set to 1000 in constants.py.
- Updated ResultDataResponse in schemas.py to truncate lists exceeding MAX_ITEMS_LENGTH, appending a message indicating the number of truncated items.

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Gabriel Luiz Freitas Almeida 2025-01-20 13:53:11 -03:00 committed by GitHub
commit 3b8578ca8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View file

@ -17,7 +17,7 @@ from langflow.services.database.models.flow import FlowCreate, FlowRead
from langflow.services.database.models.user import UserRead
from langflow.services.settings.feature_flags import FeatureFlags
from langflow.services.tracing.schema import Log
from langflow.utils.constants import MAX_TEXT_LENGTH
from langflow.utils.constants import MAX_ITEMS_LENGTH, MAX_TEXT_LENGTH
from langflow.utils.util_strings import truncate_long_strings
@ -312,6 +312,11 @@ class ResultDataResponse(BaseModel):
if isinstance(obj, dict):
return {k: ResultDataResponse._serialize_and_truncate(v, max_length=max_length) for k, v in obj.items()}
if isinstance(obj, list | tuple):
# If list is too long, truncate it
if len(obj) > MAX_ITEMS_LENGTH:
truncated_list = list(obj)[:MAX_ITEMS_LENGTH]
truncated_list.append(f"... [truncated {len(obj) - MAX_ITEMS_LENGTH} items]")
obj = truncated_list
return [ResultDataResponse._serialize_and_truncate(item, max_length=max_length) for item in obj]
return obj

View file

@ -175,3 +175,4 @@ MESSAGE_SENDER_NAME_AI = "AI"
MESSAGE_SENDER_NAME_USER = "User"
MAX_TEXT_LENGTH = 20000
MAX_ITEMS_LENGTH = 1000