diff --git a/gliner_inference_server/helpers.py b/gliner_inference_server/helpers.py index 3b1dedd..8a659d0 100644 --- a/gliner_inference_server/helpers.py +++ b/gliner_inference_server/helpers.py @@ -27,18 +27,45 @@ def validate_single_or_batch(text: Optional[str], texts: Optional[List[str]]): } ) + if text == "" and texts is None: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail={ + "error": { + "code": "INVALID_INPUT", + "message": "'text' cannot be empty or only whitespace", + "details": {} + } + } + ) + if texts is not None and len(texts) == 0: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail={ "error": { "code": "INVALID_INPUT", - "message": "texts list cannot be empty", + "message": "Texts list cannot be empty", "details": {} } } ) + if texts is not None and len(texts) != 0: + # Check for empty strings within the list + for i, t in enumerate(texts): + if not isinstance(t, str) or not t.strip(): + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail={ + "error": { + "code": "INVALID_INPUT", + "message": f"Text item at index {i} in 'texts' cannot be empty or only whitespace", + "details": {} + } + } + ) + return text if text is not None else texts diff --git a/tests/test_general_endpoint.py b/tests/test_general_endpoint.py index cf31ddc..f735437 100644 --- a/tests/test_general_endpoint.py +++ b/tests/test_general_endpoint.py @@ -82,6 +82,53 @@ class TestGeneralEndpoint: assert "error" in error assert error["error"]["code"] == "INVALID_INPUT" + def test_error_empty_text_provided(self, api_client): + """Test error when the text is empty""" + payload = { + "text": "", + "entities": ["organization"] + } + response = api_client.post(f"{BASE_URL}/general", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" + + def test_error_texts_has_empty_value(self, api_client): + """Test error when texts has an empty value""" + payload = { + "texts": [ + "Apple Inc. was founded by Steve Jobs in California.", + "", + "Elon Musk leads Tesla and SpaceX." + ], + "entities": ["organization"] + } + response = api_client.post(f"{BASE_URL}/general", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" + + def test_error_texts_has_empty_values(self, api_client): + """Test error when texts has empty values""" + payload = { + "texts": [ + "", + "", + "" + ], + "entities": ["organization"] + } + response = api_client.post(f"{BASE_URL}/general", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" + def test_error_empty_entities_list(self, api_client): """Test error with an empty entities list""" payload = { diff --git a/tests/test_relation_extraction_endpoint.py b/tests/test_relation_extraction_endpoint.py index 52932bd..5ef846e 100644 --- a/tests/test_relation_extraction_endpoint.py +++ b/tests/test_relation_extraction_endpoint.py @@ -85,3 +85,63 @@ class TestRelationExtractionEndpoint: response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload) assert response.status_code == 400 + def test_error_no_text_provided(self, api_client): + """Test error when neither text nor texts is provided""" + payload = { + "text": "", + "relations": ["founded"] + } + response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" + + def test_error_empty_text_provided(self, api_client): + """Test error when the text is empty""" + payload = { + "relations": ["founded"] + } + response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" + + def test_error_texts_has_empty_value(self, api_client): + """Test error when texts has an empty value""" + payload = { + "texts": [ + "Bill Gates founded Microsoft.", + "", + "Apple is based in California." + ], + "relations": ["founded", "founded_by", "based_in"], + "threshold": 0.3 + } + response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" + + def test_error_texts_has_empty_values(self, api_client): + """Test error when texts has empty values""" + payload = { + "texts": [ + "", + "", + "" + ], + "relations": ["founded", "founded_by", "based_in"], + "threshold": 0.3 + } + response = api_client.post(f"{BASE_URL}/relation-extraction", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" diff --git a/tests/test_summarization_endpoint.py b/tests/test_summarization_endpoint.py index 3b7af15..9757064 100644 --- a/tests/test_summarization_endpoint.py +++ b/tests/test_summarization_endpoint.py @@ -57,3 +57,54 @@ class TestSummarizationEndpoint: } response = api_client.post(f"{BASE_URL}/summarization", json=payload) assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" + + def test_error_empty_text(self, api_client): + """Test error when the text is empty""" + payload = { + "text": "", + "threshold": 0.5 + } + response = api_client.post(f"{BASE_URL}/summarization", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" + + def test_error_texts_has_empty_value(self, api_client): + """Test error when texts has an empty value""" + payload = { + "texts": [ + "Python is a high-level programming language. It's known for its simplicity and readability.", + "", + "Rust is a systems programming language. It focuses on safety and performance." + ], + "threshold": 0.3 + } + response = api_client.post(f"{BASE_URL}/summarization", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT" + + def test_error_texts_has_empty_values(self, api_client): + """Test error when texts has empty values""" + payload = { + "texts": [ + "", + "", + "" + ], + "threshold": 0.3 + } + response = api_client.post(f"{BASE_URL}/summarization", json=payload) + assert response.status_code == 400 + + error = response.json() + assert "error" in error + assert error["error"]["code"] == "INVALID_INPUT"