Hello, i'm trying to use custom blocks and i got e...
# ask-community
s
Hello, i'm trying to use custom blocks and i got errors with my code, and finally, if i try with the example given by the documentation , i get the same error which is :
ValueError: Value not declarable with JSON Schema, field: name='aws_secret_access_key' type=Optional[SecretStr] required=False default=None
Example code (you'll see a previous error i had to fix) :
Copy code
from typing import Optional

from prefect.blocks.core import Block
from pydantic import SecretStr

class AWSCredentials(Block):
    aws_access_key_id: Optional[str] = None
    aws_secret_access_key: Optional[SecretStr] = None
    aws_session_token: Optional[str] = None
    profile_name: Optional[str] = None
    region_name: Optional[str] = None
    
    # To prevent "RuntimeError: no validator found for <class 'pydantic.types.SecretStr'>, see `arbitrary_types_allowed` in Config"
    class Config:
        arbitrary_types_allowed = True

aws_credentials_block = AWSCredentials(
    aws_access_key_id="AKIAJKLJKLJKLJKLJKLJK",
    aws_secret_access_key="secret_access_key"
)

print(aws_credentials_block)
I hope someone could help me 🙂 Thank you
u
hi serial, i think the problem is that your snippet above is using a pydantic v2 object. (
SecretStr
) in one of our pydantic v1 only classes (
Block
) can you try updating this line in your snippet above
from pydantic import SecretStr
to
from pydantic.v1 import SecretStr
?
s
Hello, thank you, it works well ! Otherwise, i could force the pydantic version during pip install ?
u
yes! if you pin pydantic to v1 then your original snippet should work just fine
👍 1
btw we have an ongoing workstream to fully migrate to pydantic v2. we'll announce when that happens so keep an out for that in the coming months so that you can remove your pin on v1 and replace it with a pin on v2 🙂
s
Thanks for the information
By chance, i just discovered this conditional Pydantic import : https://github.com/PrefectHQ/prefect/blob/5e58d7bd06a6466ec2e4994716562c925ea2990e/src/prefect/blocks/fields.py#L5 So i put it in my code and it works with pydantic v2 installed 🙂, it will smoothly consider the prefect compatibility as soon as it's released, i guess.