[PR #20590] [CLOSED] fix: code interpreter images fail with JSON serialization error #64548

Closed
opened 2026-05-06 10:09:17 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/20590
Author: @Classic298
Created: 1/11/2026
Status: Closed

Base: devHead: fix-code-interpreter


📝 Commits (2)

  • 0acc927 fix: code interpreter images fail with JSON serialization error on PostgreSQL
  • 096468a Merge branch 'dev' into fix-code-interpreter

📊 Changes

3 files changed (+46 additions, -2 deletions)

View changed files

📝 backend/open_webui/routers/images.py (+5 -1)
📝 backend/open_webui/utils/files.py (+4 -1)
📝 backend/open_webui/utils/misc.py (+37 -0)

📄 Description

fix: code interpreter images fail with JSON serialization error

When code interpreter generates matplotlib/plotly charts, the image upload
to the database fails on PostgreSQL with:

sqlalchemy.exc.StatementError: (builtins.TypeError) 
Object of type function is not JSON serializable

Root cause: The metadata dict passed from middleware to upload_image and
upload_audio contained non-JSON-serializable Python objects including:

  • tools: dict containing callable function references
  • mcp_clients: dict containing MCP client objects

These were passed through to upload_file_handler where they were stored
in the meta.data JSON field. PostgreSQL strictly enforces JSON validity,
causing the insert to fail.

Solution: Added sanitize_metadata helper function in utils/misc.py that
filters out:

  • Callable values (functions, methods)
  • Known non-serializable keys (tools, mcp_clients)
  • Non-primitive types that cannot be JSON serialized

The function recursively sanitizes nested dicts and lists while preserving
essential metadata like chat_id and message_id needed for file-to-chat linking.

Applied sanitization in:

  • routers/images.py upload_image function
  • utils/files.py upload_audio function

Regression tested on SQLite:

  • Regular image upload via attachment
  • Image generation with DALL-E
  • Audio upload and transcription
  • File upload with RAG processing
  • Standard chat without code interpreter

Fixes #20561

Contributor License Agreement

By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.

Note

Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/20590 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 1/11/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix-code-interpreter` --- ### 📝 Commits (2) - [`0acc927`](https://github.com/open-webui/open-webui/commit/0acc927198d41b712808cf90a9126f3696426bcd) fix: code interpreter images fail with JSON serialization error on PostgreSQL - [`096468a`](https://github.com/open-webui/open-webui/commit/096468a5ade9c2bd9e8de090f7239c4019f29fbd) Merge branch 'dev' into fix-code-interpreter ### 📊 Changes **3 files changed** (+46 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/routers/images.py` (+5 -1) 📝 `backend/open_webui/utils/files.py` (+4 -1) 📝 `backend/open_webui/utils/misc.py` (+37 -0) </details> ### 📄 Description fix: code interpreter images fail with JSON serialization error When code interpreter generates matplotlib/plotly charts, the image upload to the database fails on PostgreSQL with: sqlalchemy.exc.StatementError: (builtins.TypeError) Object of type function is not JSON serializable Root cause: The metadata dict passed from middleware to upload_image and upload_audio contained non-JSON-serializable Python objects including: - tools: dict containing callable function references - mcp_clients: dict containing MCP client objects These were passed through to upload_file_handler where they were stored in the meta.data JSON field. PostgreSQL strictly enforces JSON validity, causing the insert to fail. Solution: Added sanitize_metadata helper function in utils/misc.py that filters out: - Callable values (functions, methods) - Known non-serializable keys (tools, mcp_clients) - Non-primitive types that cannot be JSON serialized The function recursively sanitizes nested dicts and lists while preserving essential metadata like chat_id and message_id needed for file-to-chat linking. Applied sanitization in: - routers/images.py upload_image function - utils/files.py upload_audio function **Regression tested on SQLite:** - Regular image upload via attachment - Image generation with DALL-E - Audio upload and transcription - File upload with RAG processing - Standard chat without code interpreter Fixes #20561 ### Contributor License Agreement By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. > [!NOTE] > Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-05-06 10:09:17 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#64548