<@ULVA73B9P> I'm trying to load a secret block fro...
# ask-marvin
f
@Marvin I'm trying to load a secret block from within a class constructor, but I'm running into issues. Locally this works well, but when I deploy my flow, I get the following exception: AttributeError("'coroutine' object has no attribute 'get'") It seems like loading the block with Secret.load within the constructor returns a coroutine. Is there any mention of similar issues?
m
The issue you're encountering is related to the asynchronous nature of the
Secret.load
method in Prefect 3.x. When you deploy your flow, the
Secret.load
call returns a coroutine, which needs to be awaited, hence the
AttributeError
you're seeing when trying to call
.get()
directly. To resolve this, you should either: 1. Await the coroutine: If your class constructor can be made asynchronous, you can use
await
to retrieve the secret:
Copy code
python
   from prefect.blocks.system import Secret

   class MyClass:
       async def __init__(self):
           self.secret = await Secret.load("my-secret")

   # Usage within an async context
   async def main():
       my_instance = await MyClass()
2. Use the
_sync
flag
: If you need to keep your constructor synchronous, you can use the
_sync=True
flag to force synchronous execution:
Copy code
python
   from prefect.blocks.system import Secret

   class MyClass:
       def __init__(self):
           self.secret = Secret.load("my-secret", _sync=True)

   # Usage in a synchronous context
   my_instance = MyClass()
For more details, you can refer to the Prefect 3.x documentation on blocks or check out related GitHub issues discussing similar problems. If you have further questions or need additional assistance, feel free to ask.