Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR] argilla server: using pydantic v2 #5666

Open
wants to merge 23 commits into
base: develop
Choose a base branch
from

Conversation

frascuchon
Copy link
Member

@frascuchon frascuchon commented Nov 4, 2024

Description

An alternative work of pydantic v2 upgrade with the latest changes from develop.

The main problems I found:

  • The Optional values are now required. This will affect how schemas are returned since default values are sets, including them as part of the schema result (record without responses or suggestions, for example)
  • I found some serialization errors when the response body (an error) contains a ValueError object. The same is working with pydantic <2
  • Automatic type conversion is not working anymore. This means that many client requests may fail ( values are not automatically converted to str, or some complex structures (record fields may be affected by this). We can still define custom model_validators, but we must apply them carefully.

Closes #4935

Refs #5508

Type of change

  • Refactor (change restructuring the codebase without changing functionality)

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/)

Copy link

codecov bot commented Nov 5, 2024

Codecov Report

Attention: Patch coverage is 98.72611% with 4 lines in your changes missing coverage. Please review.

Project coverage is 91.35%. Comparing base (91ed8ca) to head (612c721).

Files with missing lines Patch % Lines
...-server/src/argilla_server/errors/error_handler.py 93.33% 1 Missing ⚠️
argilla-server/src/argilla_server/jobs/hub_jobs.py 0.00% 1 Missing ⚠️
argilla-server/src/argilla_server/models/mixins.py 66.66% 1 Missing ⚠️
argilla-server/src/argilla_server/utils.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5666      +/-   ##
===========================================
+ Coverage    91.10%   91.35%   +0.24%     
===========================================
  Files          150      146       -4     
  Lines         6253     6292      +39     
===========================================
+ Hits          5697     5748      +51     
+ Misses         556      544      -12     
Flag Coverage Δ
argilla-server 91.35% <98.72%> (+0.24%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@frascuchon frascuchon force-pushed the refactor/argilla-server/using-pydanticV2 branch from f14425f to ff30ee1 Compare November 5, 2024 12:17
@frascuchon frascuchon changed the title [DRAFT] [REFACTOR] argilla server: using pydantic v2 [REFACTOR] argilla server: using pydantic v2 Nov 6, 2024
frascuchon and others added 2 commits November 6, 2024 16:49
# Description
<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. List any
dependencies that are required for this change. -->

Closes #5633

**Type of change**
<!-- Please delete options that are not relevant. Remember to title the
PR according to the type of change -->

- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)

**How Has This Been Tested**
<!-- Please add some reference about how your feature has been tested.
-->

**Checklist**
<!-- Please go over the list and make sure you've taken everything into
account -->

- 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/)

---------

Co-authored-by: José Francisco Calvo <jose@argilla.io>
Co-authored-by: José Francisco Calvo <josefranciscocalvo@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@frascuchon frascuchon added this to the v2.5.0 milestone Nov 14, 2024
Copy link
Member

@jfcalvo jfcalvo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Masive effort! 💪

@@ -1,2 +1,2 @@
ALEMBIC_CONFIG=src/argilla_server/alembic.ini
ARGILLA_DATABASE_URL=sqlite+aiosqlite:///${HOME}/.argilla/argilla.db?check_same_thread=False
ARGILLA_DATABASE_URL=sqlite+aiosqlite:///${HOME}/.argilla/argilla.dev.db?check_same_thread=False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For .env.test we have the following:

ARGILLA_DATABASE_URL=sqlite+aiosqlite:///${HOME}/.argilla/argilla-test.db?check_same_thread=False

So I guess we should do the same here:

Suggested change
ARGILLA_DATABASE_URL=sqlite+aiosqlite:///${HOME}/.argilla/argilla.dev.db?check_same_thread=False
ARGILLA_DATABASE_URL=sqlite+aiosqlite:///${HOME}/.argilla/argilla-dev.db?check_same_thread=False

@@ -22,7 +22,8 @@ maintainers = [{ name = "argilla", email = "contact@argilla.io" }]
dependencies = [
# Basic dependencies
"fastapi ~= 0.115.0",
"pydantic ~= 1.10.18",
"pydantic ~= 2.9.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we upgrade to the latest version?

Suggested change
"pydantic ~= 2.9.0",
"pydantic ~= 2.9.2",

Don't forget to do pdm install after change this, so the lock file is updated.

@@ -22,7 +22,8 @@ maintainers = [{ name = "argilla", email = "contact@argilla.io" }]
dependencies = [
# Basic dependencies
"fastapi ~= 0.115.0",
"pydantic ~= 1.10.18",
"pydantic ~= 2.9.0",
"pydantic-settings ~= 2.6.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something similar for pydantic-settings:

Suggested change
"pydantic-settings ~= 2.6.0",
"pydantic-settings ~= 2.6.1",

Don't forget to do pdm install after change this, so the lock file is updated.

Comment on lines -26 to -27
role: str = Field(..., min_role_length=MIN_ROLE_LENGTH, max_length=MAX_ROLE_LENGTH, regex=MAX_ROLE_REGEX)
content: str = Field(..., min_message_length=MIN_MESSAGE_LENGTH, max_length=MAX_MESSAGE_LENGTH)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we didn't have a test checking this right (for empty strings)? Should we add one?

le=SCORE_LESS_THAN_OR_EQUAL,
description="The score assigned to the suggestion",
)
score: Optional[Union[float, List[float]]] = Field(None, description="The score assigned to the suggestion")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding custom validations maybe we can define custom types like SuggestionScore.

Take a look to what I did here: https://github.com/argilla-io/argilla/pull/5508/files#diff-a9277ada64762a8062c1e132567edba6ce2aec3e279d1ded0452b9557bfd6b26

password: str = Field(min_length=USER_PASSWORD_MIN_LENGTH, max_length=USER_PASSWORD_MAX_LENGTH)
first_name: constr(min_length=1, strip_whitespace=True)
last_name: Optional[constr(min_length=1, strip_whitespace=True)] = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should start using annotated str with StringConstraints instead of using the old constr.

This applies to all the constr used in the PR.

So something like:

last_name: Optional[Annotated[str, StringConstraints(min_length=1, strip_whitespace=True)]] = None

You can take a look to https://github.com/argilla-io/argilla/pull/5508/files#diff-0e72eee9f8cea03b100e007fe81d5a97c5ebea93955187f083bb908a4cee2e29R48

@@ -16,7 +16,8 @@
from typing import List
from uuid import UUID

from argilla_server.pydantic_v1 import BaseModel, Field
from pydantic import BaseModel, Field, ConfigDict
from starlette.config import Config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this import needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CONFIG] Resolve python requirements for python version and dependencies with server.
2 participants