Skip to content

Accept int values for float type hints in PythonModel input validation - #27769

Open
anishmehta24 wants to merge 2 commits into
mlflow:masterfrom
anishmehta24:fix/type-hint-int-for-float
Open

anishmehta24 wants to merge 2 commits into
mlflow:masterfrom
anishmehta24:fix/type-hint-int-for-float

Conversation

@anishmehta24

Copy link
Copy Markdown
Contributor

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 PythonModel whose predict input is annotated with float (e.g. list[float], dict[str, float]) rejects whole numbers:

import mlflow


class Model(mlflow.pyfunc.PythonModel):
    def predict(self, model_input: list[float]) -> list[float]:
        return [x * 2 for x in model_input]


Model().predict([1, 2.5])
# MlflowException: Failed to validate data against type hint `list[float]`,
# invalid elements: [('1', 'Expected type float, but got int')]

The same happens on a served model: JSON has no separate float literal, so {"inputs": [1, 2.5]} comes in with 1 as an int and the request fails. A JavaScript client can't even send 1.0, since JSON.stringify(1.0) is "1". A Pydantic type hint with a float field already accepts 1, so list[MyModel] works while list[dict[str, float]] doesn't.

_validate_data_against_type_hint now converts a non-bool int to float when the type hint is float, which is what PEP 484 allows. bool is still rejected.

How is this PR tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests

Added test_type_hints_validation_accepts_int_for_float (fails on master, passes with the fix) and a bool case to test_type_hints_validation_errors. I also saved and loaded a list[float] model and a list[dict[str, float]] model: predict([1, 2.5]) now returns [2.0, 5.0] and predict([{"x": 3}]) returns [3.0]; both raised on master.

Does this PR require documentation update?

  • No.

Does this PR require updating the MLflow Skills repository?

  • No.

Release Notes

Is this a user-facing change?

  • Yes. Give a description of this change to be included in the release notes for MLflow users.

PythonModel input validation now accepts integers for float type hints, so inputs such as [1, 2.5] for list[float] no longer fail.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs

How should the PR be classified in the release notes? Choose one:

  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes

Is this PR a critical bugfix or security fix that should go into the next patch release?

What is a minor/patch release?
  • Minor release: a release that increments the second part of the version number (e.g., 1.2.0 -> 1.3.0).
    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 release: a release that increments the third part of the version number (e.g., 1.2.0 -> 1.2.1).
    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.
  • This PR is critical and needs to be in the next patch release
  • This PR can wait for the next minor release

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]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T23:37:21.918493Z 7d6aca7 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions github-actions Bot added the size/S Small PR (10-49 LoC) label Nov 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor
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/skinny

For Databricks, use the following command:

%sh curl -LsSf https://raw.githubusercontent.com/mlflow/mlflow/HEAD/dev/install-skinny.sh | sh -s pull/27769/merge

PR author's recent activity

In the last 14 days, @anishmehta24 opened 25 PRs across 18 repos (showing top 10):

Repository Open Closed Merged Total
goharbor/harbor-cli 5 0 0 5
tobymao/sqlglot 1 0 2 3
apache/beam 2 0 0 2
mlflow/mlflow 1 0 0 1
aws-powertools/powertools-lambda-typescript 0 0 1 1
WasmEdge/WasmEdge 1 0 0 1
huggingface/huggingface_hub 1 0 0 1
GoogleCloudPlatform/cloud-sql-proxy 1 0 0 1
aws-powertools/powertools-lambda-python 1 0 0 1
comet-ml/opik 1 0 0 1

@github-actions github-actions Bot added community Community/external contribution area/models MLmodel format, model serialization/deserialization, flavors area/scoring MLflow Model server, model deployment tools, Spark UDFs rn/bug-fix Mention under Bug Fixes in Changelogs. labels Nov 13, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread mlflow/types/type_hints.py Outdated
# 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed in 5c50b59, an int that overflows float now raises the usual invalid-parameter error, with a test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/models MLmodel format, model serialization/deserialization, flavors area/scoring MLflow Model server, model deployment tools, Spark UDFs community Community/external contribution rn/bug-fix Mention under Bug Fixes in Changelogs. size/S Small PR (10-49 LoC)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant