Git Product home page Git Product logo

Comments (2)

martin-schulze-e2m avatar martin-schulze-e2m commented on September 26, 2024

Note that the suggested workaround of clicking omit value on the optional parameter is not viable in our case. Accidentally sending the (unwanted) default value False could have huge consequences. For now we work around this by explicitly using the default value True but we'd rather have an explicitly optional value.

Another suggestion was to use a dropdown, maybe something. like Literal['true', 'false'] | None. However, that would require a redundant pydantic BaseModel for the UI vs internal representation.

from prefect.

zzstoatzz avatar zzstoatzz commented on September 26, 2024

hello @martin-schulze-e2m - I can reproduce this only with pydantic<2

from prefect import flow


@flow(log_prints=True)
def optional_bool(x: bool | None = None) -> bool:
    print(x)


if __name__ == "__main__":
    optional_bool.serve("optional-bool")

with pydantic==1.x

ยป prefect deployment inspect optional-bool/optional-bool
{
  "parameter_openapi_schema": {
    "type": "object",
    "title": "Parameters",
    "properties": {
      "x": {
        "type": "boolean",
        "title": "x",
        "position": 0
      }
    }
  }
}

with pydantic 2.x

{
  "parameter_openapi_schema": {
    "type": "object",
    "title": "Parameters",
    "properties": {
      "x": {
        "anyOf": [
          {
            "type": "boolean"
          },
          {
            "type": "null"
          }
        ],
        "title": "x",
        "default": null,
        "position": 0
      }
    }
}

a bool toggle seems like the correct thing for the UI to render in the case of the OpenAPI schema resulting from the pydantic 1 case. It seems like this is a limitation of the way pydantic 1 dumps Optional types into an OpenAPI schema.

if you're stuck on pydantic 1, how is this for a workaround?

from pydantic import BaseModel

from prefect import flow


class noop(BaseModel):
    """A class that does nothing"""


@flow(log_prints=True)
def optional_bool(x: bool | noop = noop()):
    print(x)


if __name__ == "__main__":
    optional_bool.serve("optional-bool")
pydantic 2 behavior

If I use Custom Run I see this as expected:
image

and in either case, I see null passed to the flow run as expected
image

from prefect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.