refactor: add model validator to FlowHeader model and fix response handling (#5355)

* refactor: streamline imports and enhance flow header response handling in flows.py

- Consolidated import statements for better readability.
- Updated the response structure in the  function to utilize  for improved data validation and consistency.

* refactor: enhance FlowHeader model with detailed field descriptions and validation

- Updated FlowHeader model to include field descriptions for better clarity and documentation.
- Introduced a model validator to ensure data integrity before processing.
- Streamlined import statements for improved readability.

---------

Co-authored-by: Lucas Oliveira <62335616+lucaseduoli@users.noreply.github.com>
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-12-19 15:58:09 -03:00 • committed by GitHub
commit 0619ed7248
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 41 deletions

View file

@ -17,13 +17,7 @@ from fastapi_pagination.ext.sqlalchemy import paginate
from sqlmodel import and_, col, select from sqlmodel import and_, col, select
from sqlmodel.ext.asyncio.session import AsyncSession from sqlmodel.ext.asyncio.session import AsyncSession
from langflow.api.utils import ( from langflow.api.utils import CurrentActiveUser, DbSession, cascade_delete_flow, remove_api_keys, validate_is_component
CurrentActiveUser,
DbSession,
cascade_delete_flow,
remove_api_keys,
validate_is_component,
)
from langflow.api.v1.schemas import FlowListCreate from langflow.api.v1.schemas import FlowListCreate
from langflow.initial_setup.constants import STARTER_FOLDER_NAME from langflow.initial_setup.constants import STARTER_FOLDER_NAME
from langflow.services.database.models.flow import Flow, FlowCreate, FlowRead, FlowUpdate from langflow.services.database.models.flow import Flow, FlowCreate, FlowRead, FlowUpdate
@ -221,17 +215,7 @@ async def read_flows(
if remove_example_flows and starter_folder_id: if remove_example_flows and starter_folder_id:
flows = [flow for flow in flows if flow.folder_id != starter_folder_id] flows = [flow for flow in flows if flow.folder_id != starter_folder_id]
if header_flows: if header_flows:
return [ return [FlowHeader.model_validate(flow, from_attributes=True) for flow in flows]
{
"id": flow.id,
"name": flow.name,
"folder_id": flow.folder_id,
"is_component": flow.is_component,
"description": flow.description,
"endpoint_name": flow.endpoint_name,
}
for flow in flows
]
return flows return flows
stmt = stmt.where(Flow.folder_id == folder_id) stmt = stmt.where(Flow.folder_id == folder_id)

View file

@ -9,7 +9,7 @@ import emoji
from emoji import purely_emoji from emoji import purely_emoji
from fastapi import HTTPException, status from fastapi import HTTPException, status
from loguru import logger from loguru import logger
from pydantic import BaseModel, field_serializer, field_validator from pydantic import BaseModel, field_serializer, field_validator, model_validator
from sqlalchemy import Text, UniqueConstraint from sqlalchemy import Text, UniqueConstraint
from sqlmodel import JSON, Column, Field, Relationship, SQLModel from sqlmodel import JSON, Column, Field, Relationship, SQLModel
@ -198,30 +198,21 @@ class FlowRead(FlowBase):
class FlowHeader(BaseModel): class FlowHeader(BaseModel):
"""Model representing a header for a flow - Without the data. """Model representing a header for a flow - Without the data."""
Attributes: id: UUID = Field(description="Unique identifier for the flow")
----------- name: str = Field(description="The name of the flow")
id : UUID folder_id: UUID | None = Field(
Unique identifier for the flow. None, description="The ID of the folder containing the flow. None if not associated with a folder"
name : str )
The name of the flow. is_component: bool | None = Field(None, description="Flag indicating whether the flow is a component")
folder_id : UUID | None, optional endpoint_name: str | None = Field(None, description="The name of the endpoint associated with this flow")
The ID of the folder containing the flow. None if not associated with a folder. description: str | None = Field(None, description="A description of the flow")
is_component : bool | None, optional
Flag indicating whether the flow is a component.
endpoint_name : str | None, optional
The name of the endpoint associated with this flow.
description : str | None, optional
A description of the flow.
"""
id: UUID @model_validator(mode="before")
name: str @classmethod
folder_id: UUID | None = None def validate_flow_header(cls, data: dict):
is_component: bool | None = None return data
endpoint_name: str | None = None
description: str | None = None
class FlowUpdate(SQLModel): class FlowUpdate(SQLModel):