[PR #5777] [MERGED] Fix: sanitize function calling json #8549

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

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/5777
Author: @smonux
Created: 9/28/2024
Status: Merged
Merged: 9/28/2024
Merged by: @tjbck

Base: devHead: dev


📝 Commits (2)

  • d8f71e1 Some models produce almost correct json during function calling, but with additional data before of after it. This solves it.
  • e039b4e Merge branch 'open-webui:dev' into dev

📊 Changes

1 file changed (+1 additions, -0 deletions)

View changed files

📝 backend/open_webui/main.py (+1 -0)

📄 Description

Pull Request Checklist

Opened a discussion here:
https://github.com/open-webui/open-webui/discussions/5528

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

  • Target branch: Please verify that the pull request targets the dev branch.
  • Description: Provide a concise description of the changes made in this pull request.
  • Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
  • Documentation: Have you updated relevant documentation Open WebUI Docs, or other documentation sources?
  • Dependencies: Are there any new dependencies? Have you updated the dependency versions in the documentation?
  • Testing: Have you written and run sufficient tests for validating the changes?
  • Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
  • Prefix: To cleary categorize this pull request, prefix the pull request title, using one of the following:

Changelog Entry

Description

Some "weaker" models with function support tend to generate "almost" correct json, only with additional characters before of after the valid code. Removing everything before the first "{" and last "}" solves it.

Other tools like instructor already do this:

c3ff3356cb/instructor/utils.py (L78)

Before the fix:

open-webui  | DEBUG [main] content='```json\n{\n  "name": "run_python_code",\n  "parameters": {\n    "code": "import os\\nprint(os.listdir(\'.\'))"\n  }\n}\n```'
open-webui  | ERROR [main] Error: Expecting value: line 1 column 1 (char 0)
open-webui  | Traceback (most recent call last):
open-webui  |   File "/app/backend/main.py", line 421, in chat_completion_tools_handler
open-webui  |     result = json.loads(content)
open-webui  |              ^^^^^^^^^^^^^^^^^^^
open-webui  |   File "/usr/local/lib/python3.11/json/__init__.py", line 346, in loads
open-webui  |     return _default_decoder.decode(s)
open-webui  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
open-webui  |   File "/usr/local/lib/python3.11/json/decoder.py", line 337, in decode
open-webui  |     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
open-webui  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
open-webui  |   File "/usr/local/lib/python3.11/json/decoder.py", line 355, in raw_decode
open-webui  |     raise JSONDecodeError("Expecting value", s, err.value) from None
open-webui  | json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

After the fix:

open-webui  | DEBUG [main] content='```json\n{\n  "name": "run_python_code",\n  "parameters": {\n    "code": "import os\\nprint(os.listdir(\'.\'))"\n  }\n}\n```'
open-webui  | DEBUG [main] tool_contexts: ['\n<interpreter_output>\n<description>\nThis is the output of the tool called "DockerInterpreter", appended here for\nreference in the response. Use it to answer the query of the user.\n\nThe user know use have access to the tool and can inspect your calls, don\'t \ntry to hide it or avoid talking about it.\n</description>\n<executed_code>\nimport os\nprint(os.listdir(\'.\'))\n</executed_code>\n<output>\n[\'archive.zip\', \'athletes.csv\', \'coaches.csv\', \'events.csv\', \'medallists.csv\', \'medals.csv\', \'medals_total.csv\', \'nocs.csv\', \'results\', \'schedules.csv\', \'schedules_preliminary.csv\', \'teams.csv\', \'technical_officials.csv\', \'torch_route.csv\', \'venues.csv\']\n\n</output>\n</interpreter_output>\n']

Added

N/A

Changed

  • One line change to backend/main.py

Deprecated

N/A

Removed

N/A

Fixed

  • [List any fixes, corrections, or bug fixes]

Security

N/A

Breaking Changes

N/A

Additional Information

Other fix suggested here :
https://github.com/open-webui/open-webui/discussions/3522

Screenshots or Videos

N/A


🔄 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/5777 **Author:** [@smonux](https://github.com/smonux) **Created:** 9/28/2024 **Status:** ✅ Merged **Merged:** 9/28/2024 **Merged by:** [@tjbck](https://github.com/tjbck) **Base:** `dev` ← **Head:** `dev` --- ### 📝 Commits (2) - [`d8f71e1`](https://github.com/open-webui/open-webui/commit/d8f71e1d7ff8fbe8e2a79c91bdb6e58ff27f6ace) Some models produce almost correct json during function calling, but with additional data before of after it. This solves it. - [`e039b4e`](https://github.com/open-webui/open-webui/commit/e039b4ec545942c2a08f40b401beecdf53cce308) Merge branch 'open-webui:dev' into dev ### 📊 Changes **1 file changed** (+1 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/main.py` (+1 -0) </details> ### 📄 Description # Pull Request Checklist Opened a discussion here: https://github.com/open-webui/open-webui/discussions/5528 **Before submitting, make sure you've checked the following:** - [X] **Target branch:** Please verify that the pull request targets the `dev` branch. - [X] **Description:** Provide a concise description of the changes made in this pull request. - [ ] **Changelog:** Ensure a changelog entry following the format of [Keep a Changelog](https://keepachangelog.com/) is added at the bottom of the PR description. - [ ] **Documentation:** Have you updated relevant documentation [Open WebUI Docs](https://github.com/open-webui/docs), or other documentation sources? - [ ] **Dependencies:** Are there any new dependencies? Have you updated the dependency versions in the documentation? - [ ] **Testing:** Have you written and run sufficient tests for validating the changes? - [X] **Code review:** Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards? - [X] **Prefix:** To cleary categorize this pull request, prefix the pull request title, using one of the following: # Changelog Entry ### Description Some "weaker" models with function support tend to generate "almost" correct json, only with additional characters before of after the valid code. Removing everything before the first "{" and last "}" solves it. Other tools like instructor already do this: https://github.com/jxnl/instructor/blob/c3ff3356cbfdac648b1800dd7e308182fdfe61d4/instructor/utils.py#L78 Before the fix: ``` open-webui | DEBUG [main] content='```json\n{\n "name": "run_python_code",\n "parameters": {\n "code": "import os\\nprint(os.listdir(\'.\'))"\n }\n}\n```' open-webui | ERROR [main] Error: Expecting value: line 1 column 1 (char 0) open-webui | Traceback (most recent call last): open-webui | File "/app/backend/main.py", line 421, in chat_completion_tools_handler open-webui | result = json.loads(content) open-webui | ^^^^^^^^^^^^^^^^^^^ open-webui | File "/usr/local/lib/python3.11/json/__init__.py", line 346, in loads open-webui | return _default_decoder.decode(s) open-webui | ^^^^^^^^^^^^^^^^^^^^^^^^^^ open-webui | File "/usr/local/lib/python3.11/json/decoder.py", line 337, in decode open-webui | obj, end = self.raw_decode(s, idx=_w(s, 0).end()) open-webui | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ open-webui | File "/usr/local/lib/python3.11/json/decoder.py", line 355, in raw_decode open-webui | raise JSONDecodeError("Expecting value", s, err.value) from None open-webui | json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ``` After the fix: ``` open-webui | DEBUG [main] content='```json\n{\n "name": "run_python_code",\n "parameters": {\n "code": "import os\\nprint(os.listdir(\'.\'))"\n }\n}\n```' open-webui | DEBUG [main] tool_contexts: ['\n<interpreter_output>\n<description>\nThis is the output of the tool called "DockerInterpreter", appended here for\nreference in the response. Use it to answer the query of the user.\n\nThe user know use have access to the tool and can inspect your calls, don\'t \ntry to hide it or avoid talking about it.\n</description>\n<executed_code>\nimport os\nprint(os.listdir(\'.\'))\n</executed_code>\n<output>\n[\'archive.zip\', \'athletes.csv\', \'coaches.csv\', \'events.csv\', \'medallists.csv\', \'medals.csv\', \'medals_total.csv\', \'nocs.csv\', \'results\', \'schedules.csv\', \'schedules_preliminary.csv\', \'teams.csv\', \'technical_officials.csv\', \'torch_route.csv\', \'venues.csv\']\n\n</output>\n</interpreter_output>\n'] ``` ### Added N/A ### Changed - One line change to backend/main.py ### Deprecated N/A ### Removed N/A ### Fixed - [List any fixes, corrections, or bug fixes] ### Security N/A ### Breaking Changes N/A ### Additional Information Other fix suggested here : https://github.com/open-webui/open-webui/discussions/3522 ### Screenshots or Videos N/A --- <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:59:29 -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#8549