Skip to content

FastAPI OpenAPI doesn't handle well small float input parameters #1893

@davidefiocco

Description

@davidefiocco

Hi, if you run code along the lines of:

from fastapi import FastAPI
import uvicorn


app = FastAPI(title="Sum one",
              description="Get a number, add one to it",
              version="0.1.0",
              )

@app.post("/compute")
def compute(input: float = 0.0000001): # problematic float default value
    return {'result': input + 1}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

and then

  • Open the browser at http://localhost:8000/docs and click and try out the /compute endpoint, then click on the Execute button
  • Execution fails because validation of the parameter field (a small float in scientific notation) is not passed, and the libraries automatically format the small float in scientific notation:

image

To solve, the user needs to manually override the scientific notation of the float in order to execute correctly, as done below:

image

  • Intended behavior would be flawless execution with a float value.

Environment

  • OS: Windows (but this should reproduce on Linux as well)
  • FastAPI Version: 0.61.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions