Repository navigation
Accept int values for float type hints in PythonModel input validation - #27769
anishmehta24 wants to merge 2 commits into
Conversation
A PythonModel whose predict is annotated with list[float] or
dict[str, float] rejected whole numbers such as 1 with "Expected type
float, but got int". JSON has no separate float literal, so a serving
request like {"inputs": [1, 2.5]} failed with a 400, while a Pydantic
model field typed float already accepted the same value. Convert a
non-bool int to float when the type hint is float.
Signed-off-by: Anish Mehta <[email protected]>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Install mlflow from this PR
Install mlflow from this PR# mlflow
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/27769/merge
# mlflow-skinny
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/27769/merge#subdirectory=libs/skinnyFor Databricks, use the following command: %sh curl -LsSf https://raw.githubusercontent.com/mlflow/mlflow/HEAD/dev/install-skinny.sh | sh -s pull/27769/mergePR author's recent activityIn the last 14 days, @anishmehta24 opened 25 PRs across 18 repos (showing top 10):
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7d6aca7497
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| # An int is valid where a float is expected (PEP 484), and JSON payloads encode | ||
| # whole-number floats such as 1.0 as 1. | ||
| if type_hint is float and isinstance(data, int) and not isinstance(data, bool): | ||
| return float(data) |
There was a problem hiding this comment.
Convert float overflow into an input-validation error
For a served type-hint model such as list[float], a JSON payload containing a digit-only integer too large to represent as a float reaches this conversion and raises OverflowError. _wrap_predict_with_pyfunc wraps that as the default INTERNAL_ERROR, so scoring returns 500 for invalid input instead of a validation 400; catch the overflow and raise MlflowException.invalid_parameter_value.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch. Fixed in 5c50b59, an int that overflows float now raises the usual invalid-parameter error, with a test.
Signed-off-by: Anish Mehta <[email protected]>
Related Issues/PRs
None. I didn't find an existing issue for this, and the change is small (23 lines).
What changes are proposed in this pull request?
A
PythonModelwhosepredictinput is annotated withfloat(e.g.list[float],dict[str, float]) rejects whole numbers:The same happens on a served model: JSON has no separate float literal, so
{"inputs": [1, 2.5]}comes in with1as anintand the request fails. A JavaScript client can't even send1.0, sinceJSON.stringify(1.0)is"1". A Pydantic type hint with afloatfield already accepts1, solist[MyModel]works whilelist[dict[str, float]]doesn't._validate_data_against_type_hintnow converts a non-boolinttofloatwhen the type hint isfloat, which is what PEP 484 allows.boolis still rejected.How is this PR tested?
Added
test_type_hints_validation_accepts_int_for_float(fails on master, passes with the fix) and aboolcase totest_type_hints_validation_errors. I also saved and loaded alist[float]model and alist[dict[str, float]]model:predict([1, 2.5])now returns[2.0, 5.0]andpredict([{"x": 3}])returns[3.0]; both raised on master.Does this PR require documentation update?
Does this PR require updating the MLflow Skills repository?
Release Notes
Is this a user-facing change?
PythonModelinput validation now accepts integers forfloattype hints, so inputs such as[1, 2.5]forlist[float]no longer fail.What component(s), interfaces, languages, and integrations does this PR affect?
Components
area/models: MLmodel format, model serialization/deserialization, flavorsarea/scoring: MLflow Model server, model deployment tools, Spark UDFsHow should the PR be classified in the release notes? Choose one:
rn/bug-fix- A user-facing bug fix worth mentioning in the release notesIs this PR a critical bugfix or security fix that should go into the next patch release?
What is a minor/patch release?
Minor releases are expected to contain larger changes, such as new features and improvements. Non-critical bug fixes and doc updates can be included as well. By default, your PR should target the next minor release.
Patch releases are typically only performed when there has been a major regression or bug in the latest release. For the sake of stability, your PR should not be included in a patch release unless it is a critical fix, or if the risk level of your PR is exceedingly low.