Skip to content

VariableEntity rejects json_schema input that is a JSON string or empty string #164

Closed

Description

@shiyuanfang2nd

Summary

VariableEntity.json_schema is typed as dict[str, Any] | None with a default mode="after" validator. When VariableEntity.model_validate(...) is called with json_schema as a JSON string or an empty string, Pydantic raises a dict_type ValidationError before the validator ever runs.

This breaks every Dify workflow whose Start node has a JSON-typed variable with a schema, because Dify's frontend code editor persists the schema as raw text into the workflow definition, not as a parsed object.

Related issue in dify: langgenius/dify#31621

Reproduction

    1. insert a variable with json schema:
{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer",
      "minimum": 0
    }
  },
  "required": [
    "name"
  ]
}
    1. run the work flow
    1. error in worker process:
2026-05-29 05:49:29,565 ERROR [trace.py:309] 8c4280d69b Task tasks.app_generate.workflow_execute_task.workflow_based_app_execution_task[8f4fcdab-389d-4e4e-9262-131b4fce577e] raised unexpected: 1 validation error for VariableEntity
json_schema
  Input should be a valid dictionary [type=dict_type, input_value='{\n  "type": "object",\n...: [\n    "name"\n  ]\n}', input_type=str]
    For further information visit https://errors.pydantic.dev/2.13/v/dict_type
Traceback (most recent call last):
  File "/Users/shiyuanfang/Documents/dify_DMS/api/.venv/lib/python3.12/site-packages/celery/app/trace.py", line 585, in trace_task
    R = retval = fun(*args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^
  File "/Users/shiyuanfang/Documents/dify_DMS/api/extensions/ext_celery.py", line 101, in __call__
    return self.run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shiyuanfang/Documents/dify_DMS/api/tasks/app_generate/workflow_execute_task.py", line 269, in workflow_based_app_execution_task
    return runner.run()
           ^^^^^^^^^^^^
  File "/Users/shiyuanfang/Documents/dify_DMS/api/tasks/app_generate/workflow_execute_task.py", line 165, in run
    response = self._run_app(
               ^^^^^^^^^^^^^^
  File "/Users/shiyuanfang/Documents/dify_DMS/api/tasks/app_generate/workflow_execute_task.py", line 198, in _run_app
    return WorkflowAppGenerator().generate(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shiyuanfang/Documents/dify_DMS/api/core/app/apps/workflow/app_generator.py", line 154, in generate
    app_config = WorkflowAppConfigManager.get_app_config(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shiyuanfang/Documents/dify_DMS/api/core/app/apps/workflow/app_config_manager.py", line 31, in get_app_config
    variables=WorkflowVariablesConfigManager.convert(workflow=workflow),
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shiyuanfang/Documents/dify_DMS/api/core/app/app_config/workflow_ui_based_app/variables/manager.py", line 23, in convert
    variables.append(VariableEntity.model_validate(variable))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shiyuanfang/Documents/dify_DMS/api/.venv/lib/python3.12/site-packages/pydantic/main.py", line 732, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for VariableEntity
json_schema
  Input should be a valid dictionary [type=dict_type, input_value='{\n  "type": "object",\n...: [\n    "name"\n  ]\n}', input_type=str]
    For further information visit https://errors.pydantic.dev/2.13/v/dict_type

Environment

  • dify 1.14.1
  • graphon: 0.3.1
  • Python: 3.12
  • Pydantic: 2.13

Activity

  1. added
    bug
    on May 29, 2026
  2. dosubot commented on May 29, 2026

    @dosubot

    Hi @shiyuanfang2nd! I'm Dosu and I’m helping the graphon team.

    This is a confirmed bug that has been reported by multiple users across Dify versions 1.11.4 through 1.14.2 [1] [2].

    The root cause is clear: the json_schema field on VariableEntity is typed as dict[str, Any] | None, and the validate_json_schema validator uses default mode="after" — meaning Pydantic's type coercion rejects the string before the validator ever runs [3]. Dify's frontend persists the schema as a raw JSON string in the workflow definition, so any workflow with a JSON-typed variable that has a schema hits this wall.

    The fix would be to add a mode="before" validator that:

    1. Converts empty strings (or whitespace-only strings) to None
    2. Parses valid JSON strings into a dict via json.loads()
    3. Passes through dict and None values unchanged

    Something like:

    @field_validator("json_schema", mode="before")
    @classmethod
    def _coerce_json_schema(cls, value: Any) -> dict[str, Any] | None:
        if value is None:
            return None
        if isinstance(value, str):
            value = value.strip()
            if not value:
                return None
            import json
            try:
                value = json.loads(value)
            except json.JSONDecodeError as e:
                msg = f"json_schema is not valid JSON: {e}"
                raise ValueError(msg) from e
        return value

    The existing validate_json_schema (mode="after") would then continue to validate the parsed dict against Draft 7 as before.

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about graphon with Dosu

  3. self-assigned this
    on May 30, 2026
  4. WH-2099 commented on May 30, 2026

    @WH-2099
    Member

    Thank you very much for your contribution. I have confirmed the issue and will follow up on it immediately.

  5. added a commit that references this issue on Jun 3, 2026
    bfe73c1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugproject#graphon

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions