[PR #5511] [MERGED] fix: restore __file__ variable for imported toolkits and functions #8493

Closed
opened 2025-11-11 17:57:59 -06:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/5511
Author: @EtiennePerot
Created: 9/19/2024
Status: Merged
Merged: 9/19/2024
Merged by: @tjbck

Base: devHead: import-file


📝 Commits (1)

  • 6477bf3 fix: restore __file__ variable for imported toolkits and functions

📊 Changes

1 file changed (+18 additions, -2 deletions)

View changed files

📝 backend/open_webui/apps/webui/utils.py (+18 -2)

📄 Description

Commit cf86ba7786 changed the way toolkits and functions were imported to use exec, whereas they previously were written to files and imported. The use of exec means that module-global variables such as __file__ are no longer defined.

This change breaks this code execution tool for Open WebUI (issue: https://github.com/EtiennePerot/open-webui-code-execution/issues/11), as the module needs to re-execute its own code in a subprocess in order to properly sandbox itself. This is done using __file__ in order to know where the module's code is located.

This PR creates a temporary in-memory file that contains the imported toolkit or function's code and exists only during the import process. Then it injects the path to this in-memory file as the __file__ variable in the exec context. This restores the ability for the toolkit or function being imported to rely on __file__.

Pull Request Checklist

Before submitting, make sure you've checked the following:

  • First-time contributors: Created a discussion about the change: https://github.com/open-webui/open-webui/discussions/5510
  • Target branch: dev branch.
  • Description: See above.
  • Changelog: See below.
  • Documentation: No relevant documentation to update.
  • Dependencies: None.
  • Testing: Tested manually. Fairly trivial change.
  • Code review: Done.
  • Prefix: fix.

Changelog Entry

Fixed

  • Restored toolkits and functions' ability to use the __file__ variable to refer to their own code.

Screenshots or Videos

Without this fix, tools and functions do not have the __file__ variable defined.


🔄 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/5511 **Author:** [@EtiennePerot](https://github.com/EtiennePerot) **Created:** 9/19/2024 **Status:** ✅ Merged **Merged:** 9/19/2024 **Merged by:** [@tjbck](https://github.com/tjbck) **Base:** `dev` ← **Head:** `import-file` --- ### 📝 Commits (1) - [`6477bf3`](https://github.com/open-webui/open-webui/commit/6477bf37fec3f736aa659c3a9e1de1d09cad5365) fix: restore `__file__` variable for imported toolkits and functions ### 📊 Changes **1 file changed** (+18 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/apps/webui/utils.py` (+18 -2) </details> ### 📄 Description Commit cf86ba778605bf0e7494817e83dbf940c0086277 changed the way toolkits and functions were imported to use `exec`, whereas they previously were written to files and `import`ed. The use of `exec` means that module-global variables such as `__file__` are no longer defined. This change breaks [this code execution tool for Open WebUI](https://github.com/EtiennePerot/open-webui-code-execution) (issue: https://github.com/EtiennePerot/open-webui-code-execution/issues/11), as the module needs to re-execute its own code in a subprocess in order to properly sandbox itself. This is done using `__file__` in order to know where the module's code is located. This PR creates a temporary in-memory file that contains the imported toolkit or function's code and exists only during the import process. Then it injects the path to this in-memory file as the `__file__` variable in the `exec` context. This restores the ability for the toolkit or function being imported to rely on `__file__`. # Pull Request Checklist **Before submitting, make sure you've checked the following:** - [x] **First-time contributors**: Created a discussion about the change: https://github.com/open-webui/open-webui/discussions/5510 - [x] **Target branch:** `dev` branch. - [x] **Description:** See above. - [x] **Changelog:** See below. - [x] **Documentation:** No relevant documentation to update. - [x] **Dependencies:** None. - [x] **Testing:** Tested manually. Fairly trivial change. - [x] **Code review:** Done. - [x] **Prefix:** `fix`. # Changelog Entry ### Fixed - Restored toolkits and functions' ability to use the `__file__` variable to refer to their own code. ### Screenshots or Videos Without this fix, tools and functions do not have the `__file__` variable defined. ![](https://github.com/user-attachments/assets/5ab93a23-7570-40cd-b0ca-6a6865c67740) --- <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 2025-11-11 17:57:59 -06: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#8493