From 1c8f528bcc7c8f4a5677836d8c4987e1a2d4a885 Mon Sep 17 00:00:00 2001 From: Paco Aranda Date: Wed, 20 Nov 2024 17:37:00 +0100 Subject: [PATCH] fix: Resolve failing tests after pydantic V2 merge (#5700) # Description This PR resolves failing tests after pydantic V2 upgrade: - Changes the expected error details - Catch TypeError exception inside validator to resolve as a 422 status response **Type of change** - Bug fix (non-breaking change which fixes an issue) **How Has This Been Tested** **Checklist** - I added relevant documentation - I followed the style guidelines of this project - I did a self-review of my code - I made corresponding changes to the documentation - I confirm My changes generate no new warnings - I have added tests that prove my fix is effective or that my feature works - I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/) --- .../argilla_server/api/schemas/v1/records.py | 2 + .../test_create_dataset_records_bulk.py | 62 ++++++++++--------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/argilla-server/src/argilla_server/api/schemas/v1/records.py b/argilla-server/src/argilla_server/api/schemas/v1/records.py index 28ca12e455..a6d1dade7b 100644 --- a/argilla-server/src/argilla_server/api/schemas/v1/records.py +++ b/argilla-server/src/argilla_server/api/schemas/v1/records.py @@ -140,6 +140,8 @@ def validate_chat_field_content(cls, fields: Any): fields[key] = [ item if isinstance(item, ChatFieldValue) else ChatFieldValue(**item) for item in value ] + except TypeError as e: + raise ValueError(f"Error parsing chat field '{key}': {e}") except ValidationError as e: raise ValueError(f"Error parsing chat field '{key}': {e.errors()}") diff --git a/argilla-server/tests/unit/api/handlers/v1/datasets/records/records_bulk/test_create_dataset_records_bulk.py b/argilla-server/tests/unit/api/handlers/v1/datasets/records/records_bulk/test_create_dataset_records_bulk.py index 75da4ca579..d0b61853b4 100644 --- a/argilla-server/tests/unit/api/handlers/v1/datasets/records/records_bulk/test_create_dataset_records_bulk.py +++ b/argilla-server/tests/unit/api/handlers/v1/datasets/records/records_bulk/test_create_dataset_records_bulk.py @@ -744,19 +744,19 @@ async def test_create_dataset_records_bulk_with_wrong_custom_field_value( "params": { "errors": [ { - "loc": ["body", "items", 0, "fields", "text-field"], - "msg": "str type expected", - "type": "type_error.str", + "loc": ["body", "items", 0, "fields", "text-field", "constrained-str"], + "msg": "Input should be a valid string", + "type": "string_type", }, { - "loc": ["body", "items", 0, "fields", "text-field"], - "msg": "value is not a valid list", - "type": "type_error.list", + "loc": ["body", "items", 0, "fields", "text-field", "list[ChatFieldValue]"], + "msg": "Input should be a valid list", + "type": "list_type", }, { - "loc": ["body", "items", 0, "fields", "text-field"], - "msg": "value is not a valid dict", - "type": "type_error.dict", + "loc": ["body", "items", 0, "fields", "text-field", "dict[constrained-str,any]"], + "msg": "Input should be a valid dictionary", + "type": "dict_type", }, ] }, @@ -771,19 +771,19 @@ async def test_create_dataset_records_bulk_with_wrong_custom_field_value( "params": { "errors": [ { - "loc": ["body", "items", 0, "fields", "text-field"], - "msg": "str type expected", - "type": "type_error.str", + "loc": ["body", "items", 0, "fields", "text-field", "constrained-str"], + "msg": "Input should be a valid string", + "type": "string_type", }, { - "loc": ["body", "items", 0, "fields", "text-field"], - "msg": "value is not a valid list", - "type": "type_error.list", + "loc": ["body", "items", 0, "fields", "text-field", "list[ChatFieldValue]"], + "msg": "Input should be a valid list", + "type": "list_type", }, { - "loc": ["body", "items", 0, "fields", "text-field"], - "msg": "value is not a valid dict", - "type": "type_error.dict", + "loc": ["body", "items", 0, "fields", "text-field", "dict[constrained-str,any]"], + "msg": "Input should be a valid dictionary", + "type": "dict_type", }, ] }, @@ -798,19 +798,19 @@ async def test_create_dataset_records_bulk_with_wrong_custom_field_value( "params": { "errors": [ { - "loc": ["body", "items", 0, "fields", "text-field"], - "msg": "str type expected", - "type": "type_error.str", + "loc": ["body", "items", 0, "fields", "text-field", "constrained-str"], + "msg": "Input should be a valid string", + "type": "string_type", }, { - "loc": ["body", "items", 0, "fields", "text-field"], - "msg": "value is not a valid list", - "type": "type_error.list", + "loc": ["body", "items", 0, "fields", "text-field", "list[ChatFieldValue]"], + "msg": "Input should be a valid list", + "type": "list_type", }, { - "loc": ["body", "items", 0, "fields", "text-field"], - "msg": "value is not a valid dict", - "type": "type_error.dict", + "loc": ["body", "items", 0, "fields", "text-field", "dict[constrained-str,any]"], + "msg": "Input should be a valid dictionary", + "type": "dict_type", }, ] }, @@ -826,8 +826,10 @@ async def test_create_dataset_records_bulk_with_wrong_custom_field_value( "errors": [ { "loc": ["body", "items", 0, "fields"], - "msg": "argilla_server.api.schemas.v1.chat.ChatFieldValue() argument after ** must be a mapping, not str", - "type": "type_error", + "msg": "Value error, Error parsing chat field 'text-field': " + "argilla_server.api.schemas.v1.chat.ChatFieldValue() " + "argument after ** must be a mapping, not str", + "type": "value_error", } ] }, @@ -871,7 +873,7 @@ async def test_create_dataset_records_bulk_with_wrong_text_field_value( }, ) - assert response.status_code == 422 + assert response.status_code == 422, response.json() assert response.json() == expected_error assert (await db.execute(select(func.count(Record.id)))).scalar_one() == 0