Skip to content

Commit 5f98eb0

Browse files
committed
fix: use async features in _validate_document_file
Code quality improvement: - Add asyncio import for async operations - Wrap blocking file operations (os.path.exists, os.path.getsize) with asyncio.to_thread() - Keep string operations synchronous as they don't block - Function now properly uses async features instead of being unnecessarily async Benefits: - Properly async: uses await for blocking I/O operations - Non-blocking: file system operations run in thread pool - Maintains async contract: function is truly asynchronous - Better performance: doesn't block the event loop during file checks
1 parent a3214a8 commit 5f98eb0

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/api/agents/document/action_tools.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import logging
77
import base64
88
import re
9+
import asyncio
910
from typing import Dict, Any, List, Optional, Union
1011
from datetime import datetime
1112
import uuid
@@ -563,17 +564,20 @@ async def reject_document(
563564

564565
# Helper methods (mock implementations for now)
565566
async def _validate_document_file(self, file_path: str) -> Dict[str, Any]:
566-
"""Validate document file."""
567-
if not os.path.exists(file_path):
567+
"""Validate document file using async file operations."""
568+
# Run synchronous file operations in thread pool to avoid blocking
569+
file_exists = await asyncio.to_thread(os.path.exists, file_path)
570+
if not file_exists:
568571
return {"valid": False, "error": "File does not exist"}
569572

570-
file_size = os.path.getsize(file_path)
573+
file_size = await asyncio.to_thread(os.path.getsize, file_path)
571574
if file_size > self.max_file_size:
572575
return {
573576
"valid": False,
574577
"error": f"File size exceeds {self.max_file_size} bytes",
575578
}
576579

580+
# String operations are fast and don't need threading
577581
file_ext = os.path.splitext(file_path)[1].lower().lstrip(".")
578582
if file_ext not in self.supported_file_types:
579583
return {"valid": False, "error": f"Unsupported file type: {file_ext}"}

0 commit comments

Comments
 (0)