Skip to content

Commit 69f3b71

Browse files
committed
Add checking to Pydantic so that null and empty questions cannot be sent to Harmony
1 parent 4aa8091 commit 69f3b71

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

src/harmony/matching/default_matcher.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def match_instruments(
8282
clustering_algorithm: str = "affinity_propagation",
8383
num_clusters_for_kmeans: int = None
8484
) -> MatchResult:
85+
for instrument in instruments:
86+
for question in instrument.questions:
87+
if question.question_text is None or question.question_text == "":
88+
raise ValueError("Invalid argument: you cannot send an empty question to Harmony. Please remove all null and empty questions.")
8589
return match_instruments_with_function(
8690
instruments=instruments,
8791
query=query,

src/harmony/matching/matcher.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,10 @@ def match_instruments_with_function(
616616
for instrument in instruments:
617617
all_questions.extend(instrument.questions)
618618

619+
for question in all_questions:
620+
if question.question_text is None or question.question_text == "":
621+
raise ValueError("Invalid argument: you cannot send an empty question to Harmony. Please remove all null and empty questions.")
622+
619623
text_vectors, new_vectors_dict = create_full_text_vectors(
620624
all_questions=[q.question_text for q in all_questions],
621625
query=query,

src/harmony/schemas/requests/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class RawFile(BaseModel):
6161
class Question(BaseModel):
6262
question_no: Optional[str] = Field(None, description="Number of the question")
6363
question_intro: Optional[str] = Field(None, description="Introductory text applying to the question")
64-
question_text: str = Field(description="Text of the question")
64+
question_text: str = Field(description="Text of the question", min_length=1)
6565
options: List[str] = Field([], description="The possible answer options")
6666
source_page: int = Field(0, description="The page of the PDF on which the question was located, zero-indexed")
6767
instrument_id: Optional[str] = Field(None, description="Unique identifier for the instrument (UUID-4)")

0 commit comments

Comments
 (0)