pseco
12/12/2025, 12:45 PMmip_ratio_thresholds: Union[Dict[str, confloat(ge=0, le=1)], float] = Field(
default={
"0": 0.10, "1": 0.35, "2": 0.50, "3": 0.20, "4": 0.80, "5": 0.45, "6": 0.60, "7": 0.30, "8": 0.15,
"9": 0.55, "10": 0.25, "11": 0.70, "12": 0.40, "13": 0.90, "14": 0.65, "15": 0.75, "16": 0.85,
"17": 0.33, "18": 0.12, "19": 0.58, "20": 0.22, "21": 0.48, "22": 0.95, "23": 0.52, "24": 0.18,
"25": 0.28, "26": 0.68, "27": 0.88},
description=(
"""
),
title=""
)Marvin
12/12/2025, 12:45 PMMarvin
12/12/2025, 12:50 PMfrom typing import Dict, Union, Annotated, Literal
from pydantic import BaseModel, Field, confloat
from pydantic import Discriminator
from prefect import flow
class MipThresholdsDict(BaseModel):
"""Use a per-index dictionary of thresholds"""
kind: Literal["dict"] = "dict"
values: Dict[str, confloat(ge=0, le=1)] = Field(
default={
"0": 0.10, "1": 0.35, "2": 0.50, "3": 0.20, "4": 0.80, "5": 0.45, "6": 0.60, "7": 0.30, "8": 0.15,
"9": 0.55, "10": 0.25, "11": 0.70, "12": 0.40, "13": 0.90, "14": 0.65, "15": 0.75, "16": 0.85,
"17": 0.33, "18": 0.12, "19": 0.58, "20": 0.22, "21": 0.48, "22": 0.95, "23": 0.52, "24": 0.18,
"25": 0.28, "26": 0.68, "27": 0.88
},
description="Thresholds by index (0–1)"
)
class MipThresholdsFloat(BaseModel):
"""Use a single float for all indices"""
kind: Literal["float"] = "float"
value: confloat(ge=0, le=1) = Field(
default=0.5,
description="Single threshold applied to all indices (0–1)"
)
MipRatioThresholds = Annotated[
Union[MipThresholdsDict, MipThresholdsFloat],
Discriminator("kind"),
]
@flow
def my_flow(
mip_ratio_thresholds: MipRatioThresholds = MipThresholdsDict()
):
# Use as:
if isinstance(mip_ratio_thresholds, MipThresholdsDict):
thresholds = mip_ratio_thresholds.values
else:
thresholds = mip_ratio_thresholds.value
return thresholds
What you’ll see in the UI:
- A toggle to switch between “dict” and “float”.
- When “dict” is selected, the form is populated with your default mapping.
- When “float” is selected, the form shows the default value 0.5.
- The initially selected variant is the one you set as the parameter default (above, the dict).
Notes:
- Your original Union[Dict[str, confloat(ge=0, le=1)], float] will render a toggle, but you can only supply one default at the parameter level. Wrapping each variant in a model is how you get per-variant defaults when switching.
- If you prefer not to use a discriminator, Union[MipThresholdsDict, MipThresholdsFloat] also works; the UI still shows a toggle. The discriminated form just makes the schema more explicit.
If you’re on Prefect 2.x, behavior differs — I’m assuming Prefect 3.x here. If you want, I can tailor the snippet for your exact version. For background on discriminated unions in Pydantic v2, see Pydantic docs on discriminated unions.
Would you like me to drop this into your existing flow file with your exact parameter name and description text?pseco
12/12/2025, 12:53 PMMarvin
12/12/2025, 12:53 PMMarvin
12/12/2025, 12:53 PMfrom typing import Dict, Union, Annotated, Literal
from pydantic import BaseModel, Field, confloat
from pydantic import Discriminator
# Variants
class MipThresholdsDict(BaseModel):
kind: Literal["dict"] = "dict"
values: Dict[str, confloat(ge=0, le=1)] = Field(
default={
"0": 0.10, "1": 0.35, "2": 0.50, "3": 0.20, "4": 0.80, "5": 0.45, "6": 0.60, "7": 0.30, "8": 0.15,
"9": 0.55, "10": 0.25, "11": 0.70, "12": 0.40, "13": 0.90, "14": 0.65, "15": 0.75, "16": 0.85,
"17": 0.33, "18": 0.12, "19": 0.58, "20": 0.22, "21": 0.48, "22": 0.95, "23": 0.52, "24": 0.18,
"25": 0.28, "26": 0.68, "27": 0.88
},
description="Thresholds by index (0–1)"
)
class MipThresholdsFloat(BaseModel):
kind: Literal["float"] = "float"
value: confloat(ge=0, le=1) = Field(
default=0.5,
description="Single threshold applied to all indices (0–1)"
)
# Discriminated union for the field type
MipRatioThresholds = Annotated[
Union[MipThresholdsDict, MipThresholdsFloat],
Discriminator("kind"),
]
# In your params model:
# class MyParams(BaseModel):
# ...
mip_ratio_thresholds: MipRatioThresholds = Field(
default=MipThresholdsDict(), # or MipThresholdsFloat(value=0.5) if you want float selected by default
description="""
Choose between:
- dict: per-index thresholds
- float: single threshold for all indices
""",
title="MIP ratio thresholds",
)
What this gives you in the Prefect Parameters UI:
- A toggle to switch between “dict” and “float”.
- Each variant shows its own default values when selected.
- The initially selected variant is controlled by the field default (change to MipThresholdsFloat(value=0.5) if you want float by default).
Important note:
- Keeping the raw type Union[Dict[str, confloat(ge=0, le=1)], float] only allows a single default overall — the UI can toggle, but you won’t get separate defaults per variant. The small wrapper models above are what enable per-variant defaults.
If you share the name of your parameters model class, I can drop this field in-place for you. For more on discriminated unions in Pydantic v2: Pydantic docs on discriminated unions.