model has wrong default value #21995

Open
opened 2026-02-21 20:15:12 -05:00 by yindo · 1 comment
Owner

Originally created by @asukaminato0721 on GitHub (Jan 30, 2026).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

master

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

we have

class OnlineDriveBrowseFilesRequest(BaseModel):
    """
    Get online drive file list request
    """

    bucket: str | None = Field(None, description="The file bucket")
    prefix: str = Field(..., description="The parent folder ID")
    max_keys: int = Field(20, description="Page size for pagination")
    next_page_parameters: dict | None = Field(None, description="Parameters for fetching the next page")

It doesn't have default value.

but we then have

        datasource_parameters = query.datasource_parameters or {}

The validation gets wrong.

related pr https://github.com/langgenius/dify/pull/31756

✔️ Expected Behavior

The type correct.

Actual Behavior

wrong

Originally created by @asukaminato0721 on GitHub (Jan 30, 2026). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version master ### Cloud or Self Hosted Self Hosted (Source) ### Steps to reproduce we have ``` class OnlineDriveBrowseFilesRequest(BaseModel): """ Get online drive file list request """ bucket: str | None = Field(None, description="The file bucket") prefix: str = Field(..., description="The parent folder ID") max_keys: int = Field(20, description="Page size for pagination") next_page_parameters: dict | None = Field(None, description="Parameters for fetching the next page") ``` It doesn't have default value. but we then have ``` datasource_parameters = query.datasource_parameters or {} ``` The validation gets wrong. related pr https://github.com/langgenius/dify/pull/31756 ### ✔️ Expected Behavior The type correct. ### ❌ Actual Behavior wrong
yindo added the 🐞 bug label 2026-02-21 20:15:12 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jan 30, 2026):

The validation error happens because the OnlineDriveBrowseFilesRequest model requires the prefix field, but when you use query.datasource_parameters or {}, you might end up passing an empty dict that lacks prefix. Pydantic enforces that required fields (like prefix) must be present, so instantiating the model without it will always fail validation see model definition.

If prefix is truly required, you should always supply it in your payload or request data. If you want to make it optional, you can change its definition to prefix: str | None = Field(None, ...) but this will require you to handle cases where it is missing throughout your logic.

For best practices, always validate and construct your request data before instantiating the model. If you have mutable defaults (like dicts), use Field(default_factory=dict) for clarity reference.

Here’s a safe pattern for instantiating the model:

from pydantic import ValidationError

try:
    request_data = query.datasource_parameters or {}
    # Ensure required fields are present
    request = OnlineDriveBrowseFilesRequest(**request_data)
except ValidationError as e:
    # Handle missing required fields
    print(e)

In Dify, the recommended approach is to convert request.args to a dict and validate it with Pydantic’s model_validate method, which improves maintainability and type safety see discussion.

Bottom line: always provide all required fields when instantiating Pydantic models, especially for API request schemas. If you need to support missing fields, make them optional and handle the logic accordingly.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Jan 30, 2026): <!-- Answer --> The validation error happens because the OnlineDriveBrowseFilesRequest model requires the prefix field, but when you use query.datasource_parameters or {}, you might end up passing an empty dict that lacks prefix. Pydantic enforces that required fields (like prefix) must be present, so instantiating the model without it will always fail validation [see model definition](https://github.com/langgenius/dify/blob/4346f61b0cfaf9eee69e8c6f819ecf3f676717f6/api/core/datasource/entities/datasource_entities.py#L357-L366). If prefix is truly required, you should always supply it in your payload or request data. If you want to make it optional, you can change its definition to prefix: str | None = Field(None, ...) but this will require you to handle cases where it is missing throughout your logic. For best practices, always validate and construct your request data before instantiating the model. If you have mutable defaults (like dicts), use Field(default_factory=dict) for clarity [reference](https://github.com/langgenius/dify/issues/24063). Here’s a safe pattern for instantiating the model: ```python from pydantic import ValidationError try: request_data = query.datasource_parameters or {} # Ensure required fields are present request = OnlineDriveBrowseFilesRequest(**request_data) except ValidationError as e: # Handle missing required fields print(e) ``` In Dify, the recommended approach is to convert request.args to a dict and validate it with Pydantic’s model_validate method, which improves maintainability and type safety [see discussion](https://github.com/langgenius/dify/issues/31077). Bottom line: always provide all required fields when instantiating Pydantic models, especially for API request schemas. If you need to support missing fields, make them optional and handle the logic accordingly. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/b2292179-dec4-4b46-8f82-75c1291ab9f7?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/b2292179-dec4-4b46-8f82-75c1291ab9f7?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/b2292179-dec4-4b46-8f82-75c1291ab9f7?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/b2292179-dec4-4b46-8f82-75c1291ab9f7?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/b2292179-dec4-4b46-8f82-75c1291ab9f7?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/b2292179-dec4-4b46-8f82-75c1291ab9f7?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/b2292179-dec4-4b46-8f82-75c1291ab9f7?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=The%20validation%20error%20happens%20because%20the%20OnlineDriveBrowseFilesRequest%20model%20requires%20the%20prefix%20field%2C%20but%20when%20you%20use%20query.datasource_parameters%20or%20%7B%7D%2C%20you%20might%20end%20up%20passing%20an%20empty%20dict%20that%20lacks%20prefix.%20Pydantic%20enforces%20that%20required%20fields%20%28like%20prefix%29%20must%20be%20present%2C%20so%20instantiating%20the%20model%20without%20it%20will%20always%20fail%20validation%C2%A0%5Bsee%20model%20definition%5D%28https%3A//github.com/langgenius/dify/blob/4346f61b0cfaf9eee69e8c6f819ecf3f676717f6/api/core/datasource/entities/datasource_entities.py%23L357-L366%29.%0A%0AIf%20prefix%20is%20truly%20required%2C%20you%20should%20always%20supply%20it%20in%20your%20payload%20or%20request%20data.%20If%20you%20want%20to%20make%20it%20optional%2C%20you%20can%20change%20its%20definition%20to%20prefix%3A%20str%20%7C%20None%20%3D%20Field%28None%2C%20...%29%20but%20this%20will%20require%20you%20to%20handle%20cases%20where%20it%20is%20missing%20throughout%20your%20logic.%0A%0AFor%20best%20practices%2C%20always%20validate%20and%20construct%20your%20request%20data%20before%20instantiating%20the%20model.%20If%20you%20have%20mutable%20defaults%20%28like%20dicts%29%2C%20use%20Field%28default_factory%3Ddict%29%20for%20clarity%C2%A0%5Breference%5D%28https%3A//github.com/langgenius/dify/issues/24063%29.%0A%0AHere%E2%80%99s%20a%20safe%20pattern%20for%20instantiating%20the%20model%3A%0A%0A%60%60%60python%0Afrom%20pydantic%20import%20ValidationError%0A%0Atry%3A%0A%20%20%20%20request_data%20%3D%20query.datasource_parameters%20or%20%7B%7D%0A%20%20%20%20%23%20Ensure%20required%20fields%20are%20present%0A%20%20%20%20request%20%3D%20OnlineDriveBrowseFilesRequest%28%2A%2Arequest_data%29%0Aexcept%20ValidationError%20as%20e%3A%0A%20%20%20%20%23%20Handle%20missing%20required%20fields%0A%20%20%20%20print%28e%29%0A%60%60%60%0A%0AIn%20Dify%2C%20the%20recommended%20approach%20is%20to%20convert%20request.args%20to%20a%20dict%20and%20validate%20it%20with%20Pydantic%E2%80%99s%20model_validate%20method%2C%20which%20improves%20maintainability%20and%20type%20safety%C2%A0%5Bsee%20discussion%5D%28https%3A//github.com/langgenius/dify/issues/31077%29.%0A%0ABottom%20line%3A%20always%20provide%20all%20required%20fields%20when%20instantiating%20Pydantic%20models%2C%20especially%20for%20API%20request%20schemas.%20If%20you%20need%20to%20support%20missing%20fields%2C%20make%20them%20optional%20and%20handle%20the%20logic%20accordingly.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/31761)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#21995