From dfabb80567a606c127bfa74e9015c131c8c06071 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 27 Oct 2023 11:05:02 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(schema.py):=20fix=20the=20tr?= =?UTF-8?q?ansformation=20of=20tags=20in=20ListComponentResponse=20to=20re?= =?UTF-8?q?turn=20a=20list=20of=20TagResponse=20objects=20=F0=9F=94=A5=20c?= =?UTF-8?q?hore(service.py):=20remove=20unnecessary=20code=20that=20flatte?= =?UTF-8?q?ns=20the=20tags=20in=20ListComponentResponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/services/store/schema.py | 16 ++++++++++++++-- src/backend/langflow/services/store/service.py | 6 +++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/backend/langflow/services/store/schema.py b/src/backend/langflow/services/store/schema.py index 215b15b91..4f11ad262 100644 --- a/src/backend/langflow/services/store/schema.py +++ b/src/backend/langflow/services/store/schema.py @@ -1,5 +1,5 @@ from datetime import datetime -from pydantic import BaseModel +from pydantic import BaseModel, validator from typing import Optional, List from uuid import UUID @@ -45,9 +45,21 @@ class ListComponentResponse(BaseModel): is_component: Optional[bool] metadata: Optional[dict] user_created: Optional[dict] - tags: Optional[List[TagsIdResponse]] = None + tags: Optional[List[TagResponse]] = None downloads_count: Optional[int] + # tags comes as a TagsIdResponse but we want to return a list of TagResponse + @validator("tags", pre=True) + def tags_to_list(cls, v): + # Check if all values are have id and name + # if so, return v else transform to TagResponse + if all(["id" in tag and "name" in tag for tag in v]): + return v + else: + return [ + TagResponse(**tag.get("tags_id")) for tag in v if tag.get("tags_id") + ] + class DownloadComponentResponse(BaseModel): id: UUID diff --git a/src/backend/langflow/services/store/service.py b/src/backend/langflow/services/store/service.py index 5fcce2860..3015a2729 100644 --- a/src/backend/langflow/services/store/service.py +++ b/src/backend/langflow/services/store/service.py @@ -188,9 +188,9 @@ class StoreService(Service): results = self._get(self.components_url, api_key, params) results_objects = [ListComponentResponse(**component) for component in results] # Flatten the tags - for component in results_objects: - if component.tags: - component.tags = [tags_id.tags_id for tags_id in component.tags] + # for component in results_objects: + # if component.tags: + # component.tags = [tags_id.tags_id for tags_id in component.tags] return results_objects def download(self, api_key: str, component_id: str) -> DownloadComponentResponse: