Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

Fix jsonschema compliance: use number instead of float (#25049)

Yongtao Huangdb53656a45fd
diff --git a/api/core/mcp/server/streamable_http.py b/api/core/mcp/server/streamable_http.py
index 5851c6d4065..3d51ac23337 100644
--- a/api/core/mcp/server/streamable_http.py
+++ b/api/core/mcp/server/streamable_http.py
@@ -258,5 +258,5 @@ def convert_input_form_to_parameters(
parameters[item.variable]["type"] = "string"
parameters[item.variable]["enum"] = item.options
elif item.type == VariableEntityType.NUMBER:
- parameters[item.variable]["type"] = "float"
+ parameters[item.variable]["type"] = "number"
return parameters, required
diff --git a/api/tests/unit_tests/core/mcp/server/test_streamable_http.py b/api/tests/unit_tests/core/mcp/server/test_streamable_http.py
index ccc5d42bcfa..f1d741602a9 100644
--- a/api/tests/unit_tests/core/mcp/server/test_streamable_http.py
+++ b/api/tests/unit_tests/core/mcp/server/test_streamable_http.py
@@ -1,6 +1,7 @@
import json
from unittest.mock import Mock, patch
+import jsonschema
import pytest
from core.app.app_config.entities import VariableEntity, VariableEntityType
@@ -434,7 +435,7 @@ class TestUtilityFunctions:
assert parameters["category"]["enum"] == ["A", "B", "C"]
assert "count" in parameters
- assert parameters["count"]["type"] == "float"
+ assert parameters["count"]["type"] == "number"
# FILE type should be skipped - it creates empty dict but gets filtered later
# Check that it doesn't have any meaningful content
@@ -447,3 +448,65 @@ class TestUtilityFunctions:
assert "category" not in required
# Note: _get_request_id function has been removed as request_id is now passed as parameter
+
+ def test_convert_input_form_to_parameters_jsonschema_validation_ok(self):
+ """Current schema uses 'number' for numeric fields; it should be a valid JSON Schema."""
+ user_input_form = [
+ VariableEntity(
+ type=VariableEntityType.NUMBER,
+ variable="count",
+ description="Count",
+ label="Count",
+ required=True,
+ ),
+ VariableEntity(
+ type=VariableEntityType.TEXT_INPUT,
+ variable="name",
+ description="User name",
+ label="Name",
+ required=False,
+ ),
+ ]
+
+ parameters_dict = {
+ "count": "Enter count",
+ "name": "Enter your name",
+ }
+
+ parameters, required = convert_input_form_to_parameters(user_input_form, parameters_dict)
+
+ # Build a complete JSON Schema
+ schema = {
+ "type": "object",
+ "properties": parameters,
+ "required": required,
+ }
+
+ # 1) The schema itself must be valid
+ jsonschema.Draft202012Validator.check_schema(schema)
+
+ # 2) Both float and integer instances should pass validation
+ jsonschema.validate(instance={"count": 3.14, "name": "alice"}, schema=schema)
+ jsonschema.validate(instance={"count": 2, "name": "bob"}, schema=schema)
+
+ def test_legacy_float_type_schema_is_invalid(self):
+ """Legacy/buggy behavior: using 'float' should produce an invalid JSON Schema."""
+ # Manually construct a legacy/incorrect schema (simulating old behavior)
+ bad_schema = {
+ "type": "object",
+ "properties": {
+ "count": {
+ "type": "float", # Invalid type: JSON Schema does not support 'float'
+ "description": "Enter count",
+ }
+ },
+ "required": ["count"],
+ }
+
+ # The schema itself should raise a SchemaError
+ with pytest.raises(jsonschema.exceptions.SchemaError):
+ jsonschema.Draft202012Validator.check_schema(bad_schema)
+
+ # Or validation should also raise SchemaError
+ with pytest.raises(jsonschema.exceptions.SchemaError):
+ jsonschema.validate(instance={"count": 1.23}, schema=bad_schema)