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

Unable to serialize unknown type - Pydantic 2.10 #16090

Open
tonal opened this issue Nov 22, 2024 · 1 comment
Open

Unable to serialize unknown type - Pydantic 2.10 #16090

tonal opened this issue Nov 22, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@tonal
Copy link

tonal commented Nov 22, 2024

Bug summary

from asyncio import run

from prefect.blocks.core import Block
from prefect.utilities.asyncutils import sync_compatible
from pydantic import MongoDsn, SecretStr


class ConfMongoTest(Block):
  uri:MongoDsn
  username:str|None=None
  password:SecretStr|None=None
  db:str


@sync_compatible
async def save_auth():
  conf = ConfMongoTest(uri='mongodb://localhost:27017', db='test')
  await conf.save('rem-gr-auth-aquapolis', overwrite=True)


async def amain():
  await save_auth()


if __name__ == '__main__':
  run(amain())

Version info

Version:             3.1.4
API version:         0.8.4
Python version:      3.12.7
Git commit:          78ee41cb
Built:               Wed, Nov 20, 2024 7:37 PM
OS/Arch:             linux/x86_64
Profile:             ephemeral
Server type:         ephemeral
Pydantic version:    2.10.1
Server:
  Database:          sqlite
  SQLite version:    3.45.1
Integrations:
  prefect-ray:       0.4.3
  prefect-dask:      0.3.2
  prefect-docker:    0.6.2
  prefect-aws:       0.5.2

Additional context

Downgrading the pedantic version to <2.10 solves the problem

@tonal tonal added the bug Something isn't working label Nov 22, 2024
@tonal tonal changed the title Unable to serialize unknown type Unable to serialize unknown type - Pydantic 2.10 Nov 22, 2024
@zzstoatzz
Copy link
Collaborator

hi @tonal - I'm not sure this is related to prefect, moreso pydantic (see pydantic/pydantic#10946)

in the meantime you can add a field serializer to tell pydantic how to serialize your uri

from asyncio import run

from pydantic import MongoDsn, SecretStr, field_serializer

from prefect.blocks.core import Block
from prefect.utilities.asyncutils import sync_compatible


class ConfMongoTest(Block):
    uri: MongoDsn
    username: str | None = None
    password: SecretStr | None = None
    db: str

    @field_serializer("uri")
    def serialize_uri(self, uri: MongoDsn) -> str:
        return str(uri)


@sync_compatible
async def save_auth():
    conf = ConfMongoTest(uri="mongodb://localhost:27017", db="test")
    await conf.save("rem-gr-auth-aquapolis", overwrite=True)


async def amain():
    await save_auth()


if __name__ == "__main__":
    run(amain())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants