feat: adds input checks for add datapoints and summarization tasks

This commit is contained in:
hajdul88 2025-08-14 14:17:13 +02:00
parent 9f965c44b4
commit 63d071f0d8
6 changed files with 62 additions and 1 deletions

View file

@ -5,9 +5,18 @@ from cognee.infrastructure.databases.graph import get_graph_engine
from cognee.modules.graph.utils import deduplicate_nodes_and_edges, get_graph_from_model from cognee.modules.graph.utils import deduplicate_nodes_and_edges, get_graph_from_model
from .index_data_points import index_data_points from .index_data_points import index_data_points
from .index_graph_edges import index_graph_edges from .index_graph_edges import index_graph_edges
from cognee.tasks.storage.exceptions import (
InvalidDataPointsInAddDataPointsError,
)
async def add_data_points(data_points: List[DataPoint]) -> List[DataPoint]: async def add_data_points(data_points: List[DataPoint]) -> List[DataPoint]:
if not isinstance(data_points, list):
raise InvalidDataPointsInAddDataPointsError("data_points must be a list.")
if not all(isinstance(dp, DataPoint) for dp in data_points):
raise InvalidDataPointsInAddDataPointsError("data_points: each item must be a DataPoint.")
nodes = [] nodes = []
edges = [] edges = []

View file

@ -0,0 +1,9 @@
"""
Custom exceptions for the Cognee API.
This module defines a set of exceptions for handling various data errors
"""
from .exceptions import (
InvalidDataPointsInAddDataPointsError,
)

View file

@ -0,0 +1,13 @@
from cognee.exceptions import (
CogneeValidationError,
)
from fastapi import status
class InvalidDataPointsInAddDataPointsError(CogneeValidationError):
def __init__(self, detail: str):
super().__init__(
message=f"Invalid data_points: {detail}",
name="InvalidDataPointsInAddDataPointsError",
status_code=status.HTTP_400_BAD_REQUEST)

View file

@ -0,0 +1,9 @@
"""
Custom exceptions for the Cognee API.
This module defines a set of exceptions for handling various data errors
"""
from .exceptions import (
InvalidSummaryInputsError,
)

View file

@ -0,0 +1,13 @@
from cognee.exceptions import (
CogneeValidationError,
CogneeConfigurationError,
)
from fastapi import status
class InvalidSummaryInputsError(CogneeValidationError):
def __init__(self, detail: str):
super().__init__(
message=f"Invalid summarize_text inputs: {detail}",
name="InvalidSummaryInputsError",
status_code=status.HTTP_400_BAD_REQUEST)

View file

@ -3,10 +3,11 @@ from typing import Type
from uuid import uuid5 from uuid import uuid5
from pydantic import BaseModel from pydantic import BaseModel
from cognee.tasks.summarization.exceptions import InvalidSummaryInputsError
from cognee.modules.chunking.models.DocumentChunk import DocumentChunk from cognee.modules.chunking.models.DocumentChunk import DocumentChunk
from cognee.infrastructure.llm.LLMGateway import LLMGateway from cognee.infrastructure.llm.LLMGateway import LLMGateway
from cognee.modules.cognify.config import get_cognify_config from cognee.modules.cognify.config import get_cognify_config
from .models import TextSummary from cognee.tasks.summarization.models import TextSummary
async def summarize_text( async def summarize_text(
@ -35,6 +36,13 @@ async def summarize_text(
A list of TextSummary objects, each containing the summary of a corresponding A list of TextSummary objects, each containing the summary of a corresponding
DocumentChunk. DocumentChunk.
""" """
if not isinstance(data_chunks, list):
raise InvalidSummaryInputsError("data_chunks must be a list.")
if not all(hasattr(c, "text") for c in data_chunks):
raise InvalidSummaryInputsError("each DocumentChunk must have a 'text' attribute.")
if len(data_chunks) == 0: if len(data_chunks) == 0:
return data_chunks return data_chunks