From 805af68173d94ea9eccb84c2d52b6aea0293126a Mon Sep 17 00:00:00 2001 From: Ranjith Guggilla Date: Sun, 6 Dec 2026 00:49:13 -0600 Subject: [PATCH 1/2] fix(serverless): report invalid input instead of raising from validator constraints Skip schema constraints for missing or wrong-typed values so validation returns the errors already collected. Add regression tests for missing, invalid, and valid values. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- runpod/serverless/utils/rp_validator.py | 12 +++- .../test_utils/test_validate.py | 66 +++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/runpod/serverless/utils/rp_validator.py b/runpod/serverless/utils/rp_validator.py index 5a66e18e5..2d7c1d708 100644 --- a/runpod/serverless/utils/rp_validator.py +++ b/runpod/serverless/utils/rp_validator.py @@ -79,10 +79,16 @@ def _validate_input_against_schema(schema, validated_input, error_list): error_list, f"{key} should be {rules['type']} type, not {type(validated_input[key])}.", ) + # Constraints are written for the declared type, so a value + # of another type is reported once and not passed to them. + continue - # Check lambda constraints. - if "constraints" in rules and not rules["constraints"]( - validated_input.get(key) + # Check lambda constraints. A missing input has no value to check and + # was already reported as required or as a schema error. + if ( + key in validated_input + and "constraints" in rules + and not rules["constraints"](validated_input[key]) ): _add_error(error_list, CONSTRAINTS_ERROR.format(key)) diff --git a/tests/test_serverless/test_utils/test_validate.py b/tests/test_serverless/test_utils/test_validate.py index 603851303..98aacc46e 100644 --- a/tests/test_serverless/test_utils/test_validate.py +++ b/tests/test_serverless/test_utils/test_validate.py @@ -92,6 +92,72 @@ def test_validate_invalid_type(self): "x should be type, not .", result["errors"] ) + def test_validate_missing_required_input_with_constraints(self): + """ + A missing required input is reported without calling its constraints + """ + schema = { + "steps": {"type": int, "required": True, "constraints": lambda x: x > 0} + } + + result = rp_validator.validate({}, schema) + + self.assertEqual(result, {"errors": ["steps is a required input."]}) + + def test_validate_missing_default_with_constraints(self): + """ + An optional input with no default is reported without calling its constraints + """ + schema = { + "steps": {"type": int, "required": False, "constraints": lambda x: x > 0} + } + + result = rp_validator.validate({}, schema) + + self.assertEqual( + result, {"errors": ["Schema error, missing default value for steps."]} + ) + + def test_validate_invalid_type_with_constraints(self): + """ + A value of the wrong type is reported without calling the constraints + """ + for value in ("20", None, [20]): + with self.subTest(value=value): + constraints = Mock(side_effect=lambda x: x > 0) + schema = { + "steps": {"type": int, "required": True, "constraints": constraints} + } + + result = rp_validator.validate({"steps": value}, schema) + + self.assertEqual( + result, + { + "errors": [ + f"steps should be type, not {type(value)}." + ] + }, + ) + constraints.assert_not_called() + + def test_validate_constraints_receive_valid_value(self): + """ + Constraints still run on a value of the declared type + """ + schema = { + "steps": {"type": int, "required": True, "constraints": lambda x: x > 0} + } + + self.assertEqual( + rp_validator.validate({"steps": 20}, schema), + {"validated_input": {"steps": 20}}, + ) + self.assertEqual( + rp_validator.validate({"steps": 0}, schema), + {"errors": ["steps does not meet the constraints."]}, + ) + def test_validate_rules_not_dict(self): """ Tests validate with rules not dict From 93f2fbd3a570e8fbf986e5f791d3045c85c4069b Mon Sep 17 00:00:00 2001 From: Ranjith Guggilla Date: Sun, 6 Dec 2026 01:08:52 -0600 Subject: [PATCH 2/2] fix(serverless): apply constraints when defaults exist Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 1 + runpod/serverless/utils/rp_validator.py | 12 +++-- .../test_utils/test_validate.py | 49 +++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56125be51..0e8c94eda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ ### Bug Fixes +* Serverless input constraints are enforced for supplied values even when the schema defines a default. * [BREAKING] `rp login` asks you to paste the API key shown in the console after approval. It no longer collects the key by polling the auth request, and it fails fast without an interactive terminal. * Task calls support indefinite execution with bounded interruption cleanup. Failed deletion retains the pod ID for recovery. * Network volume creation intersects catalog storage support with every sharing resource's hardware stock and datacenter constraints. Unsupported explicit pins fail before creation; existing volumes retain their authoritative IDs and datacenters. diff --git a/runpod/serverless/utils/rp_validator.py b/runpod/serverless/utils/rp_validator.py index 2d7c1d708..2f77c7682 100644 --- a/runpod/serverless/utils/rp_validator.py +++ b/runpod/serverless/utils/rp_validator.py @@ -68,13 +68,13 @@ def _validate_input_against_schema(schema, validated_input, error_list): except TypeError: continue - # skip type check assuming rare case, default value sets value - if "default" in rules and isinstance(validated_input[key], type(rules["default"])): - continue + matches_default = "default" in rules and isinstance( + validated_input[key], type(rules["default"]) + ) # Check for the correct type. is_instance = isinstance(validated_input[key], rules["type"]) - if not is_instance: + if not matches_default and not is_instance: _add_error( error_list, f"{key} should be {rules['type']} type, not {type(validated_input[key])}.", @@ -82,6 +82,10 @@ def _validate_input_against_schema(schema, validated_input, error_list): # Constraints are written for the declared type, so a value # of another type is reported once and not passed to them. continue + # Values accepted by matching the default's type are not passed to + # constraints. + if not is_instance: + continue # Check lambda constraints. A missing input has no value to check and # was already reported as required or as a schema error. diff --git a/tests/test_serverless/test_utils/test_validate.py b/tests/test_serverless/test_utils/test_validate.py index 98aacc46e..c8458c2c3 100644 --- a/tests/test_serverless/test_utils/test_validate.py +++ b/tests/test_serverless/test_utils/test_validate.py @@ -158,6 +158,55 @@ def test_validate_constraints_receive_valid_value(self): {"errors": ["steps does not meet the constraints."]}, ) + def test_validate_constraints_apply_when_default_is_set(self): + """ + Constraints are enforced on an optional input that has a default + """ + schema = { + "steps": { + "type": int, + "required": False, + "default": 10, + "constraints": lambda x: 0 < x <= 100, + } + } + + self.assertEqual( + rp_validator.validate({"steps": 5000}, schema), + {"errors": ["steps does not meet the constraints."]}, + ) + self.assertEqual( + rp_validator.validate({"steps": 50}, schema), + {"validated_input": {"steps": 50}}, + ) + self.assertEqual( + rp_validator.validate({}, schema), {"validated_input": {"steps": 10}} + ) + + def test_validate_default_of_other_type_skips_constraints(self): + """ + A None default is accepted and is not passed to the constraints + """ + schema = { + "prompt": { + "type": str, + "required": False, + "default": None, + "constraints": lambda x: len(x) < 5, + } + } + + for raw_input in ({}, {"prompt": None}): + with self.subTest(raw_input=raw_input): + self.assertEqual( + rp_validator.validate(raw_input, schema), + {"validated_input": {"prompt": None}}, + ) + self.assertEqual( + rp_validator.validate({"prompt": "too long"}, schema), + {"errors": ["prompt does not meet the constraints."]}, + ) + def test_validate_rules_not_dict(self): """ Tests validate with rules not dict