From 32996aa0d08b0c90dd234607bd6436b79e72e5b1 Mon Sep 17 00:00:00 2001 From: hajdul88 <52442977+hajdul88@users.noreply.github.com> Date: Wed, 13 Aug 2025 13:40:50 +0200 Subject: [PATCH] feat: adds new error classes to llm and databases + introduces loglevel and logging from child error --- cognee/exceptions/exceptions.py | 16 +++++++++++---- .../exceptions/EmbeddingException.py | 20 ------------------- .../databases/exceptions/__init__.py | 1 + .../databases/exceptions/exceptions.py | 18 +++++++++++++++++ .../embeddings/LiteLLMEmbeddingEngine.py | 2 +- .../databases/vector/exceptions/exceptions.py | 4 +++- .../vector/pgvector/PGVectorAdapter.py | 4 ++-- cognee/infrastructure/llm/exceptions.py | 8 ++++++++ .../litellm_instructor/llm/openai/adapter.py | 5 ++--- cognee/tasks/storage/index_data_points.py | 2 +- 10 files changed, 48 insertions(+), 32 deletions(-) delete mode 100644 cognee/infrastructure/databases/exceptions/EmbeddingException.py diff --git a/cognee/exceptions/exceptions.py b/cognee/exceptions/exceptions.py index e7eb784b8..9b6cef21d 100644 --- a/cognee/exceptions/exceptions.py +++ b/cognee/exceptions/exceptions.py @@ -43,8 +43,10 @@ class CogneeSystemError(CogneeApiError): message: str = "A system error occurred.", name: str = "CogneeSystemError", status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + log=True, + log_level="ERROR", ): - super().__init__(message, name, status_code) + super().__init__(message, name, status_code, log, log_level) class CogneeValidationError(CogneeApiError): @@ -55,8 +57,10 @@ class CogneeValidationError(CogneeApiError): message: str = "A validation error occurred.", name: str = "CogneeValidationError", status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, + log=True, + log_level="ERROR", ): - super().__init__(message, name, status_code) + super().__init__(message, name, status_code, log, log_level) class CogneeConfigurationError(CogneeApiError): @@ -67,8 +71,10 @@ class CogneeConfigurationError(CogneeApiError): message: str = "A system configuration error occurred.", name: str = "CogneeConfigurationError", status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + log=True, + log_level="ERROR", ): - super().__init__(message, name, status_code) + super().__init__(message, name, status_code, log, log_level) class CogneeTransientError(CogneeApiError): @@ -79,6 +85,8 @@ class CogneeTransientError(CogneeApiError): message: str = "A transient error occurred.", name: str = "CogneeTransientError", status_code=status.HTTP_503_SERVICE_UNAVAILABLE, + log=True, + log_level="ERROR", ): - super().__init__(message, name, status_code) + super().__init__(message, name, status_code, log, log_level) diff --git a/cognee/infrastructure/databases/exceptions/EmbeddingException.py b/cognee/infrastructure/databases/exceptions/EmbeddingException.py deleted file mode 100644 index 62616899c..000000000 --- a/cognee/infrastructure/databases/exceptions/EmbeddingException.py +++ /dev/null @@ -1,20 +0,0 @@ -from cognee.exceptions import CogneeApiError -from fastapi import status - - -class EmbeddingException(CogneeApiError): - """ - Custom exception for handling embedding-related errors. - - This exception class is designed to indicate issues specifically related to embeddings - within the application. It extends the base exception class CogneeApiError and allows - for customization of the error message, name, and status code. - """ - - def __init__( - self, - message: str = "Embedding Exception.", - name: str = "EmbeddingException", - status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, - ): - super().__init__(message, name, status_code) diff --git a/cognee/infrastructure/databases/exceptions/__init__.py b/cognee/infrastructure/databases/exceptions/__init__.py index 9d8d18567..c7d2a8feb 100644 --- a/cognee/infrastructure/databases/exceptions/__init__.py +++ b/cognee/infrastructure/databases/exceptions/__init__.py @@ -8,4 +8,5 @@ from .exceptions import ( EntityNotFoundError, EntityAlreadyExistsError, DatabaseNotCreatedError, + EmbeddingException, ) diff --git a/cognee/infrastructure/databases/exceptions/exceptions.py b/cognee/infrastructure/databases/exceptions/exceptions.py index 4aba09d37..66740fa5e 100644 --- a/cognee/infrastructure/databases/exceptions/exceptions.py +++ b/cognee/infrastructure/databases/exceptions/exceptions.py @@ -84,3 +84,21 @@ class NodesetFilterNotSupportedError(CogneeConfigurationError): self.message = message self.name = name self.status_code = status_code + + +class EmbeddingException(CogneeConfigurationError): + """ + Custom exception for handling embedding-related errors. + + This exception class is designed to indicate issues specifically related to embeddings + within the application. It extends the base exception class CogneeConfigurationError allows + for customization of the error message, name, and status code. + """ + + def __init__( + self, + message: str = "Embedding Exception.", + name: str = "EmbeddingException", + status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, + ): + super().__init__(message, name, status_code) diff --git a/cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py b/cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py index e3cdaea00..dae664907 100644 --- a/cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py +++ b/cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py @@ -6,7 +6,7 @@ import math import litellm import os from cognee.infrastructure.databases.vector.embeddings.EmbeddingEngine import EmbeddingEngine -from cognee.infrastructure.databases.exceptions.EmbeddingException import EmbeddingException +from cognee.infrastructure.databases.exceptions import EmbeddingException from cognee.infrastructure.llm.tokenizer.Gemini import ( GeminiTokenizer, ) diff --git a/cognee/infrastructure/databases/vector/exceptions/exceptions.py b/cognee/infrastructure/databases/vector/exceptions/exceptions.py index 48d9976e0..ee0712433 100644 --- a/cognee/infrastructure/databases/vector/exceptions/exceptions.py +++ b/cognee/infrastructure/databases/vector/exceptions/exceptions.py @@ -16,5 +16,7 @@ class CollectionNotFoundError(CogneeValidationError): message, name: str = "CollectionNotFoundError", status_code: int = status.HTTP_422_UNPROCESSABLE_ENTITY, + log=True, + log_level="DEBUG", ): - super().__init__(message, name, status_code) + super().__init__(message, name, status_code, log, log_level) diff --git a/cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py b/cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py index ac40b91b8..96b2056c4 100644 --- a/cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py +++ b/cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py @@ -9,7 +9,7 @@ from sqlalchemy.exc import ProgrammingError from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential from asyncpg import DeadlockDetectedError, DuplicateTableError, UniqueViolationError -from cognee.exceptions import InvalidValueError + from cognee.shared.logging_utils import get_logger from cognee.infrastructure.engine import DataPoint from cognee.infrastructure.engine.utils import parse_id @@ -275,7 +275,7 @@ class PGVectorAdapter(SQLAlchemyAdapter, VectorDBInterface): return metadata.tables[collection_name] else: raise CollectionNotFoundError( - f"Collection '{collection_name}' not found!", log_level="DEBUG" + f"Collection '{collection_name}' not found!", ) async def retrieve(self, collection_name: str, data_point_ids: List[str]): diff --git a/cognee/infrastructure/llm/exceptions.py b/cognee/infrastructure/llm/exceptions.py index 287820448..c8f5726fb 100644 --- a/cognee/infrastructure/llm/exceptions.py +++ b/cognee/infrastructure/llm/exceptions.py @@ -20,3 +20,11 @@ class UnsupportedLLMProviderError(CogneeValidationError): def __init__(self, provider: str): message = f"Unsupported LLM provider: {provider}" super().__init__(message=message, name="UnsupportedLLMProviderError") + +class MissingSystemPromptPathError(CogneeValidationError): + def __init__( + self, + name: str = "MissingSystemPromptPathError", + ): + message = "No system prompt path provided." + super().__init__(message, name) diff --git a/cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py b/cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py index 69c1bac1c..38e1bc82e 100644 --- a/cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +++ b/cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py @@ -7,12 +7,11 @@ from openai import ContentFilterFinishReasonError from litellm.exceptions import ContentPolicyViolationError from instructor.exceptions import InstructorRetryException -from cognee.exceptions import InvalidValueError from cognee.infrastructure.llm.LLMGateway import LLMGateway from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import ( LLMInterface, ) -from cognee.infrastructure.llm.exceptions import ContentPolicyFilterError +from cognee.infrastructure.llm.exceptions import ContentPolicyFilterError, MissingSystemPromptPathError from cognee.infrastructure.files.utils.open_data_file import open_data_file from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.rate_limiter import ( rate_limit_async, @@ -325,7 +324,7 @@ class OpenAIAdapter(LLMInterface): if not text_input: text_input = "No user input provided." if not system_prompt: - raise InvalidValueError(message="No system prompt path provided.") + raise MissingSystemPromptPathError() system_prompt = LLMGateway.read_query_prompt(system_prompt) formatted_prompt = ( diff --git a/cognee/tasks/storage/index_data_points.py b/cognee/tasks/storage/index_data_points.py index 51b6c2d6e..9c363c04c 100644 --- a/cognee/tasks/storage/index_data_points.py +++ b/cognee/tasks/storage/index_data_points.py @@ -1,6 +1,6 @@ from cognee.shared.logging_utils import get_logger -from cognee.infrastructure.databases.exceptions.EmbeddingException import EmbeddingException +from cognee.infrastructure.databases.exceptions import EmbeddingException from cognee.infrastructure.databases.vector import get_vector_engine from cognee.infrastructure.engine import DataPoint