<@ULVA73B9P> i would like to knw if it is posible ...
# ask-marvin
p
@Marvin i would like to knw if it is posible to generate an input variable in the paraemters dashaboard, with a button that make possible to select to use a dictionary as a variable or a float, like the boolean toggle, with useing in both of them values by default. This input right here is the one i have right now, and i would like to adapt to the new functionlaity of having two different values, dict and float
Copy code
mip_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=""
)
m
thought for 262.3 seconds
Great question — yes, this is possible in Prefect 3.x. The Parameters panel supports Union types and will render a toggle to switch between variants. To have separate defaults for each choice (dict vs float), the most reliable approach is to wrap each variant in its own Pydantic model. That way, when you switch variants in the UI, the form can populate the appropriate defaults for that variant. Here’s how you can adapt your parameter to support a “dict” or a “float” with defaults for both, using a discriminated union so the UI shows a clean toggle:
Copy code
from 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?
p
@Marvin i would like to added to my actual input file, not directly to the flow with the same structure as the example i share with you
m
thought for 43.1 seconds
Got it — you want to keep this in your input/schema file (not in the flow) and keep the same “field = Field(...)” structure. To support a UI toggle with defaults for both options, define two small Pydantic models (one for the dict, one for the float) and use a discriminated union as the field type. Then set the field default to whichever variant you want selected initially. Drop this into your input file:
Copy code
from 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.