With typing hints, can Prefect handle more complic...
# ask-community
i
With typing hints, can Prefect handle more complicated hints for the UI? If, for example, I’d like to have a list of some pydantic basemodel, could Prefect process that to show the various fields?
j
Not sure about the actual answer to your question, but is something you could use blocks for? Or would it have to be passed in as a parameter?
i
It’s a parameter, and mostly just a UI question about the capabilities for Prefect. It’ll be nice to have it available as such, rather than having to specify a JSON format.
j
Ah, I gotcha. I'm curious to know the answer too, I agree it'd be nicer to handle it that way than via JSON
d
Enum are displayed as Drop down. The deployment specs has open api params. Maybe you can look into that?
n
if you have a pydantic model as an input like
Copy code
class Foo(BaseModel):
    a: int
    b: int

class Input(BaseModel):
    foo: Foo
    bar: str

@flow(log_prints=True)
def my_flow(input: Input) -> None:
    print(f"got input: {input!r}")
then the form will show up nicely in the UI with each field of
Input
(including the nested models), but if you have a
list[Input]
then the input in the UI will be a freeform JSON field. perhaps you could adjust the model you're passing in so that it contains your lists instead of passing a list of the models?
👍 1
i
Would that work? If e.g. it was
foo: List[Foo]
?
n
hmm actually it seems like whenever there's a
list[Model]
its going to be a freeform JSON field like (image is the Quick Run form for this deployment)
Copy code
from prefect import flow

from pydantic import BaseModel

class Foo(BaseModel):
    a: int
    b: int

class Input(BaseModel):
    foo: list[Foo]
    bar: str

@flow(log_prints=True)
def my_flow(input: Input) -> None:
    print(f"got input: {input!r}")

# prefect deploy typehints.py:my_flow
i
That's a pity :( any alternatives? Could one leverage
*args
for this? 🤔
n
what is it that you're trying to do? do you plan on spending a lot of time in the UI filling out the forms for flow runs?
i
Just trying to use the UI for the less tech-oriented staff, so that they may run production flows themselves
I wouldn't even mind not having type checks, I mostly want to be able to include a docstring for each field in the flow, so they don't mess up
n
makes sense! requirement about lists of models aside, seems like you could do
Copy code
from prefect import flow

from pydantic import BaseModel, Field

class Input(BaseModel):
    foo: int = Field(..., description="foo is a required field")
    bar: bool = Field(False, description="whether to bar or not")
    baz: list = Field(default_factory=list, description="a list of bazzes")

@flow(log_prints=True)
def my_flow(user_input: Input) -> None:
    print(f"got input: {user_input!r}")
i
Hello I'd like to bar please. Yes! This looks like a good compromise, thanks! Any idea about future UI support for lists? Just to one-up this?
😄 1
n
i don't know about future plans for the UI here, perhaps @Craig Harshbarger would have some input (pun intended)
😁 1
c
This is mostly a
List[...]
question. Currently we don't have any ui to handle lists unless there is an
enum
and then we give you a drop down. We would have to add some ui to visually let you add/remove values and render the schema for the model in the list. Which gets a little tricky considering there can be unions and such. But its possible! Just not a priority at the moment. But if this is something you'd like to see please open a github issue for it and if there are others that want this that will help us set an accurate priority.
🙏 2
i
Thanks @Craig Harshbarger! I've seen this before where usually there's simply "add" and "remove" buttons, that open a new item with the designated schema. I'll put a reminder to create a GH issue for this, would be a nice feature to have!
thank you 1
One last follow-up @Nate @Craig Harshbarger; is there a way to maintain docstring structure? In Nate’s example above (
a list of bazzes
), I’d like to include e.g. a sample JSON with explanation for the different kwargs. Prefect minimizes it to one-line, so the formatting is lost.
n
is there a way to maintain docstring structure?
hmm I'm not quite sure what you mean, could you show an example? of what you mean by
a sample JSON with explanation for the different kwargs
i
Let’s say I’d like to have:
Copy code
class Input(BaseModel):
    a: SomeOtherClass = Field(..., description="""A foo that bars some bazzes, with the following structure:
{
  # You may specify your foo as an integer here
  "foo": Optional[int]
  "bar": {
    # Your baz goes here
    "baz": str
    "should_baz": bool = False
  }
}
""")
This results in a single line, even though I’d like to maintain the multi-line description
c
Can you verify if its coming from the api in the way you'd expect? Curious if it is the ui or api that is removing the formatting.
i
Could you direct me how to check that?
c
If you look at the deployment in the network tab. Basically just looking for what json the api is returning.
i
Hm, I don’t see anything like that in the network tab 🤔
Ah, sorry, it was cached. Now I got it. In the JSON response it looks fine 👍 Newlines and indentations as expected, so it’s just a rendering thing.
c
Sorry for the delay. Was on vacation and just catching back up on things. If this is still happening could you please create a github issue with a minimum reproduction? That's the best way to get this picked up and fixed.
i
Sorry, I was also away on vacation. The ticketing list requires to verify with a different browser 🤔 I find that a bit discouraging, I don’t hold multiple browsers on my end and would prefer to refrain from installing additional ones 🙈