From fab2d9afb3583e5e5ef0447bc439a1432193cbcf Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 30 Oct 2024 21:27:05 -0300 Subject: [PATCH] fix: Refactor file reading and async task creation, fix return type in vertex_builds model (#4273) --- src/backend/base/langflow/api/v1/chat.py | 6 +++--- .../custom/directory_reader/directory_reader.py | 17 +++++++++-------- .../database/models/vertex_builds/model.py | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/backend/base/langflow/api/v1/chat.py b/src/backend/base/langflow/api/v1/chat.py index eff041f1b..742c1baea 100644 --- a/src/backend/base/langflow/api/v1/chat.py +++ b/src/backend/base/langflow/api/v1/chat.py @@ -329,7 +329,7 @@ async def build_flow( client_consumed_queue: asyncio.Queue, event_manager: EventManager, ) -> None: - build_task = asyncio.create_task(asyncio.to_thread(asyncio.run, _build_vertex(vertex_id, graph, event_manager))) + build_task = asyncio.create_task(_build_vertex(vertex_id, graph, event_manager)) try: await build_task except asyncio.CancelledError as exc: @@ -361,8 +361,8 @@ async def build_flow( async def event_generator(event_manager: EventManager, client_consumed_queue: asyncio.Queue) -> None: if not data: - # using another thread since the DB query is I/O bound - vertices_task = asyncio.create_task(asyncio.to_thread(asyncio.run, build_graph_and_get_order())) + # using another task since the build_graph_and_get_order is now an async function + vertices_task = asyncio.create_task(build_graph_and_get_order()) try: await vertices_task except asyncio.CancelledError: diff --git a/src/backend/base/langflow/custom/directory_reader/directory_reader.py b/src/backend/base/langflow/custom/directory_reader/directory_reader.py index 70b453b52..a47599106 100644 --- a/src/backend/base/langflow/custom/directory_reader/directory_reader.py +++ b/src/backend/base/langflow/custom/directory_reader/directory_reader.py @@ -93,15 +93,16 @@ class DirectoryReader: _file_path = Path(file_path) if not _file_path.is_file(): return None - with _file_path.open(encoding="utf-8") as file: - # UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 3069: character maps to - try: + try: + with _file_path.open(encoding="utf-8") as file: + # UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 3069: + # character maps to return file.read() - except UnicodeDecodeError: - # This is happening in Windows, so we need to open the file in binary mode - # The file is always just a python file, so we can safely read it as utf-8 - with _file_path.open("rb") as f: - return f.read().decode("utf-8") + except UnicodeDecodeError: + # This is happening in Windows, so we need to open the file in binary mode + # The file is always just a python file, so we can safely read it as utf-8 + with _file_path.open("rb") as f: + return f.read().decode("utf-8") def get_files(self): """Walk through the directory path and return a list of all .py files.""" diff --git a/src/backend/base/langflow/services/database/models/vertex_builds/model.py b/src/backend/base/langflow/services/database/models/vertex_builds/model.py index a3c9b8e4c..6302589c1 100644 --- a/src/backend/base/langflow/services/database/models/vertex_builds/model.py +++ b/src/backend/base/langflow/services/database/models/vertex_builds/model.py @@ -50,7 +50,7 @@ class VertexBuildBase(SQLModel): return truncate_long_strings(data) @field_serializer("params") - def serialize_params(self, data) -> dict: + def serialize_params(self, data) -> str: return truncate_long_strings(data)