When an LLM produces truncated or malformed JSON in tool call arguments (common with streaming or context-length limits), the SSE stream crashes with an unhandled exception.
This PR adds graceful error handling:
Catches json.JSONDecodeError during tool argument parsing
Sets a tool_args_parse_error flag to skip tool execution
Returns a descriptive error message back to the LLM so it can retry or explain
The chat stream continues normally instead of crashing
🔄 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/21983
**Author:** [@sxu75374](https://github.com/sxu75374)
**Created:** 2/28/2026
**Status:** ❌ Closed
**Base:** `main` ← **Head:** `fix/graceful-malformed-tool-args`
---
### 📝 Commits (1)
- [`bdcafe9`](https://github.com/open-webui/open-webui/commit/bdcafe9d8f9c2c4881c59892f9a7748238b719b9) fix(middleware): graceful error handling for malformed tool call arguments
### 📊 Changes
**2 files changed** (+138 additions, -3 deletions)
<details>
<summary>View changed files</summary>
➕ `backend/open_webui/test/utils/test_tool_call_parsing.py` (+130 -0)
📝 `backend/open_webui/utils/middleware.py` (+8 -3)
</details>
### 📄 Description
## What this PR does
When an LLM produces truncated or malformed JSON in tool call arguments (common with streaming or context-length limits), the SSE stream crashes with an unhandled exception.
This PR adds graceful error handling:
- Catches `json.JSONDecodeError` during tool argument parsing
- Sets a `tool_args_parse_error` flag to skip tool execution
- Returns a descriptive error message back to the LLM so it can retry or explain
- The chat stream continues normally instead of crashing
## Changes
- `backend/open_webui/utils/middleware.py`: Add parse error flag, guard tool execution, return descriptive error to LLM
- `backend/open_webui/test/utils/test_tool_call_parsing.py`: 11 unit tests covering malformed JSON, truncated args, valid args, and edge cases
## Testing
All 11 new tests pass. Manually verified with truncated JSON payloads — stream continues and LLM receives the error context.
Signed-off-by: sxu75374 <imshuaixu@gmail.com>
---
<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/21983
Author: @sxu75374
Created: 2/28/2026
Status: ❌ Closed
Base:
main← Head:fix/graceful-malformed-tool-args📝 Commits (1)
bdcafe9fix(middleware): graceful error handling for malformed tool call arguments📊 Changes
2 files changed (+138 additions, -3 deletions)
View changed files
➕
backend/open_webui/test/utils/test_tool_call_parsing.py(+130 -0)📝
backend/open_webui/utils/middleware.py(+8 -3)📄 Description
What this PR does
When an LLM produces truncated or malformed JSON in tool call arguments (common with streaming or context-length limits), the SSE stream crashes with an unhandled exception.
This PR adds graceful error handling:
json.JSONDecodeErrorduring tool argument parsingtool_args_parse_errorflag to skip tool executionChanges
backend/open_webui/utils/middleware.py: Add parse error flag, guard tool execution, return descriptive error to LLMbackend/open_webui/test/utils/test_tool_call_parsing.py: 11 unit tests covering malformed JSON, truncated args, valid args, and edge casesTesting
All 11 new tests pass. Manually verified with truncated JSON payloads — stream continues and LLM receives the error context.
Signed-off-by: sxu75374 imshuaixu@gmail.com
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.