diff --git a/dataherald/api/types/requests.py b/dataherald/api/types/requests.py index fcb90348..32488af9 100644 --- a/dataherald/api/types/requests.py +++ b/dataherald/api/types/requests.py @@ -1,4 +1,5 @@ -from pydantic import BaseModel +from pydantic import BaseModel, validator +from sql_metadata import Parser from dataherald.types import LLMConfig @@ -17,6 +18,14 @@ class SQLGenerationRequest(BaseModel): sql: str | None metadata: dict | None + @validator("sql") + def validate_model_name(cls, v: str | None): + try: + Parser(v).tables # noqa: B018 + except Exception as e: + raise ValueError(f"SQL {v} is malformed. Please check the syntax.") from e + return v + class StreamSQLGenerationRequest(BaseModel): finetuning_id: str | None