Object of type File is not JSON serializable #6553

Closed
opened 2026-02-21 18:16:16 -05:00 by yindo · 4 comments
Owner

Originally created by @svcvit on GitHub (Nov 5, 2024).

Self Checks

  • 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 (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.10.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

I would like the uploaded file to create a file.url, which I can then request in the sandbox to retrieve the file and process it through code.

My test code is as follows, mainly aiming to print out the file content.
Xnip2024-11-05_16-58-50

When I only test the code node and upload the file, this node runs fine.
Xnip2024-11-05_16-58-26

However, if I click on "Preview" and upload the file, I get an error: Object of type File is not JSON serializable.
Xnip2024-11-05_16-58-41

✔️ Expected Behavior

No response

Actual Behavior

No response

Originally created by @svcvit on GitHub (Nov 5, 2024). ### Self Checks - [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 (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.10.2 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce I would like the uploaded file to create a file.url, which I can then request in the sandbox to retrieve the file and process it through code. My test code is as follows, mainly aiming to print out the file content. ![Xnip2024-11-05_16-58-50](https://github.com/user-attachments/assets/c10d319d-ada6-4052-9581-f353556eb397) When I only test the code node and upload the file, this node runs fine. ![Xnip2024-11-05_16-58-26](https://github.com/user-attachments/assets/66940d36-68be-48a2-b672-5d39baac437a) However, if I click on "Preview" and upload the file, I get an error: Object of type File is not JSON serializable. ![Xnip2024-11-05_16-58-41](https://github.com/user-attachments/assets/9ed099ce-5a8b-4c09-b64f-928ad9617c12) ### ✔️ Expected Behavior _No response_ ### ❌ Actual Behavior _No response_
yindo added the 🐞 bug label 2026-02-21 18:16:16 -05:00
yindo closed this issue 2026-02-21 18:16:17 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Nov 5, 2024):

I found a similar closed issue related to file handling in Dify. The issue titled "Error message received when I upload PDF" was closed by a pull request that addressed the problem by cleaning un-allowed special characters during indexing estimation. This might be related to the error you're encountering with JSON serialization [1].

In your case, the error "Object of type File is not JSON serializable" suggests that the file object is not being properly converted into a JSON-compatible format during the "Preview" operation. This could be due to differences in how the "Preview" feature handles file objects compared to regular execution. Unfortunately, I couldn't find specific details on how the "Preview" feature handles file objects differently, but it might be worth checking if there are any updates or patches related to this in the repository or documentation.

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Nov 5, 2024): <!-- Answer --> I found a similar closed issue related to file handling in Dify. The issue titled "[Error message received when I upload PDF](https://github.com/langgenius/dify/issues/10127)" was closed by a pull request that addressed the problem by cleaning un-allowed special characters during indexing estimation. This might be related to the error you're encountering with JSON serialization <sup>[[1]](https://github.com/langgenius/dify/issues/10127)</sup>. In your case, the error "Object of type File is not JSON serializable" suggests that the file object is not being properly converted into a JSON-compatible format during the "Preview" operation. This could be due to differences in how the "Preview" feature handles file objects compared to regular execution. Unfortunately, I couldn't find specific details on how the "Preview" feature handles file objects differently, but it might be worth checking if there are any updates or patches related to this in the repository or documentation. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@crazywoola commented on GitHub (Nov 5, 2024):

Check the actual inputs to the code block.

@crazywoola commented on GitHub (Nov 5, 2024): Check the actual inputs to the code block.
Author
Owner

@laipz8200 commented on GitHub (Nov 8, 2024):

File typed variable is not allowed in the Code node for now.

@laipz8200 commented on GitHub (Nov 8, 2024): `File` typed variable is not allowed in the Code node for now.
Author
Owner

@lovingliferwj commented on GitHub (Jan 9, 2025):

hi, I want to process an upload PDF file in Code node and perform OCR recognition by my own. When I pass the file parameter to the Code node,it throws an error 'Object of type File is not JSON serializable'. How can I achieve above function?

@lovingliferwj commented on GitHub (Jan 9, 2025): hi, I want to process an upload PDF file in Code node and perform OCR recognition by my own. When I pass the file parameter to the Code node,it throws an error 'Object of type File is not JSON serializable'. How can I achieve above function?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#6553