<@ULVA73B9P> I have the next input in my prefect p...
# ask-marvin
p
@Marvin I have the next input in my prefect pipeline, how could I fix the number of elements in the array to be 3 or 4? and not be able to have nothing more than that
Copy code
pesos: List[float] = Field(
    default_factory=lambda: [0.4, 0.2, 0.2, 0.2],
    description=(
        ""
        ""
    ),
    title=""
)
m
thought for 76.7 seconds
You can enforce that with Pydantic validation. In Prefect 3.x (Pydantic v2), use
Field(min_length=3, max_length=4)
. If you’re defining this on a Pydantic model:
Copy code
from typing import List
from pydantic import BaseModel, Field

class Params(BaseModel):
    pesos: List[float] = Field(
        default_factory=lambda: [0.4, 0.2, 0.2, 0.2],
        min_length=3,
        max_length=4,
        description="",
        title="",
    )
If you want it directly as a flow parameter:
Copy code
from typing import Annotated, List
from pydantic import Field
from prefect import flow

@flow
def my_flow(
    pesos: Annotated[List[float], Field(min_length=3, max_length=4)] = [0.4, 0.2, 0.2, 0.2]
):
    # your logic here
    return pesos
Both approaches only allow lists of length 3 or 4; anything else will raise a validation error. Note: If you happen to be on Prefect 2.x (Pydantic v1), use `min_items`/`max_items` or
conlist
, e.g.:
Copy code
from pydantic import conlist, BaseModel

class Params(BaseModel):
    pesos: conlist(float, min_items=3, max_items=4) = [0.4, 0.2, 0.2, 0.2]
p
@Marvin if i use the first solution you mentioned, i can fix the maximum but not the minimum from typing import List from pydantic import BaseModel, Field class Params(BaseModel): pesos: List[float] = Field( default_factory=lambda: [0.4, 0.2, 0.2, 0.2], min_length=3, max_length=4, description="", title="", )
m
thought for 101.8 seconds
Got it — quick clarifier: are you trying to enforce - the list’s length (min number of items), or - the min/max value for each float in the list? Also, which Pydantic version are you on? Prefect 3.x uses Pydantic 2.x, and the syntax differs from 1.x. If you mean list length: - Pydantic 2.x (Prefect 3.x): use
min_length
/
max_length
Copy code
from typing import List
from typing_extensions import Annotated
from pydantic import BaseModel, Field

class Params(BaseModel):
    pesos: Annotated[List[float], Field(min_length=3, max_length=4)] = Field(
        default_factory=lambda: [0.4, 0.2, 0.2, 0.2]
    )

# This should raise a ValidationError (too few items)
# Params(pesos=[0.1, 0.2])
- Pydantic 1.x: use
min_items
/
max_items
(or
conlist
)
Copy code
from typing import List
from pydantic import BaseModel, Field

class Params(BaseModel):
    pesos: List[float] = Field(
        default_factory=lambda: [0.4, 0.2, 0.2, 0.2],
        min_items=3,
        max_items=4,
    )
or
Copy code
from pydantic import BaseModel, conlist

class Params(BaseModel):
    pesos: conlist(float, min_items=3, max_items=4) = [0.4, 0.2, 0.2, 0.2]
If you mean per-item bounds (e.g., each weight between 0 and 1): - Pydantic 2.x:
Copy code
from typing import List
from typing_extensions import Annotated
from pydantic import BaseModel, Field

Number = Annotated[float, Field(ge=0, le=1)]

class Params(BaseModel):
    pesos: Annotated[List[Number], Field(min_length=3, max_length=4)] = Field(
        default_factory=lambda: [0.4, 0.2, 0.2, 0.2]
    )
- Pydantic 1.x:
Copy code
from pydantic import BaseModel, conlist, confloat

Weight = confloat(ge=0, le=1)

class Params(BaseModel):
    pesos: conlist(Weight, min_items=3, max_items=4) = [0.4, 0.2, 0.2, 0.2]
If you’re still seeing “max works but min doesn’t,” please share: - your Pydantic version - an example input that should fail the min check but doesn’t I can reproduce and pinpoint the issue from there.
p
i am truin g to fix the min number of items to be 3 and the maximum to be 4
@Marvin i am truin g to fix the min number of items to be 3 and the maximum to be 4
m
thought for 60.6 seconds
Thanks! To constrain the list length to min=3 and max=4: First, which Pydantic version are you on? Prefect 3.x uses Pydantic 2.x. Use the matching snippet below. Pydantic 2.x (Prefect 3.x) — use min_length/max_length
Copy code
from typing import List
from typing_extensions import Annotated
from pydantic import BaseModel, Field

class Params(BaseModel):
    pesos: Annotated[List[float], Field(min_length=3, max_length=4)] = Field(
        default_factory=lambda: [0.4, 0.2, 0.2, 0.2]
    )

# Fails (too few items)
# Params(pesos=[0.1, 0.2])

# Fails (too many items)
# Params(pesos=[0.1, 0.2, 0.3, 0.4, 0.5])

# OK (3 or 4 items)
# Params(pesos=[0.1, 0.2, 0.3])
Pydantic 1.x — use min_items/max_items or conlist
Copy code
from typing import List
from pydantic import BaseModel, Field

class Params(BaseModel):
    pesos: List[float] = Field(
        default_factory=lambda: [0.4, 0.2, 0.2, 0.2],
        min_items=3,
        max_items=4,
    )
or
Copy code
from pydantic import BaseModel, conlist

class Params(BaseModel):
    pesos: conlist(float, min_items=3, max_items=4) = [0.4, 0.2, 0.2, 0.2]
Common gotcha: if you provide a default of length 4 (via
default_factory
), omitting
pesos
will always pass. To actually see the “min items” error, pass a shorter list:
Copy code
Params(pesos=[0.1, 0.2])  # should raise a ValidationError
If you still see “max works but min doesn’t,” please share your Pydantic version and the exact input that isn’t failing so I can reproduce.
p
@Marvin i am being able to select 2 items on th elist, and that could not be possible, just 3 o 4
m
thought for 398.6 seconds
Got it. Two quick points to make sure this works end-to-end: 1) Pydantic constraints - If you’re on Pydantic 2.x (Prefect 3.x), use min_length/max_length on the field. - If you’re on Pydantic 1.x (Prefect 2.x), use min_items/max_items (or conlist). Pydantic 2.x (Prefect 3.x)
Copy code
from typing import List
from typing_extensions import Annotated
from pydantic import BaseModel, Field

class Params(BaseModel):
    pesos: Annotated[List[float], Field(min_length=3, max_length=4)] = Field(
        default_factory=lambda: [0.4, 0.2, 0.2, 0.2]
    )
Pydantic 1.x
Copy code
from typing import List
from pydantic import BaseModel, Field

class Params(BaseModel):
    pesos: List[float] = Field(
        default_factory=lambda: [0.4, 0.2, 0.2, 0.2],
        min_items=3,
        max_items=4,
    )
# or
from pydantic import BaseModel, conlist

class Params(BaseModel):
    pesos: conlist(float, min_items=3, max_items=4) = [0.4, 0.2, 0.2, 0.2]
2) Prefect UI vs server-side enforcement - The Prefect 3 UI may allow you to enter 2 items in the form, but the server will reject the run at creation if
enforce_parameter_schema
is enabled on the deployment (true by default) and your flow parameter is typed with your
Params
model. - Make sure your flow signature actually uses the model so the schema is generated:
Copy code
from prefect import flow

@flow
def my_flow(params: Params):
    ...
If you still see a run start with only 2 items (no validation error), it usually means one of: - The flow parameter is not typed as
Params
(so the deployment has no schema for it) - You’re on Pydantic 1.x but using
min_length
instead of
min_items
- Schema enforcement is disabled for the deployment/run If you can share: - Prefect version (
prefect version
) and Pydantic version (
import pydantic; pydantic.__version__
) - Your flow signature - How you’re starting the run (UI/CLI/API) …I can pinpoint exactly why the 2-item case is slipping through.