fix: graceful error handling for malformed tool call arguments#21984
fix: graceful error handling for malformed tool call arguments#21984sxu75374 wants to merge 20 commits intoopen-webui:devfrom
Conversation
chore: format
* refac * fix: python pip install dep issue * Merge pull request open-webui#20085 from joaoback/patch-19 Update translation.json (pt-BR) * fix/refac: stt default content type * fix/refac: temp chat image handling * fix/refac: image action button * chore: bump --------- Co-authored-by: joaoback <156559121+joaoback@users.noreply.github.com>
…ments
When an LLM produces truncated or invalid JSON for tool call arguments
(e.g., incomplete streaming output), both ast.literal_eval and json.loads
fail. Previously, this silently left tool_function_params as {} and
continued to execute the tool with empty params, which could crash the
handler and terminate the SSE stream — showing users a confusing
'Network connection lost' error.
Now, when argument parsing fails:
- A tool_args_parse_error flag is set
- Tool execution is skipped entirely
- A descriptive error message is returned as the tool result, informing
the LLM that the arguments were malformed and it should retry
- The SSE stream continues normally, allowing the conversation to recover
Also changed the log level from error to warning since this is a
recoverable condition caused by LLM output, not a system error.
Closes open-webui#21927
Signed-off-by: sxu75374 <imshuaixu@gmail.com>
Signed-off-by: Small <imshuaixu@gmail.com>
🚫 STOP: MISSING CLA@sxu75374, your PR description is missing the Contributor License Agreement confirmation. 👋 Welcome and Thank You for Contributing!We appreciate you taking the time to submit a pull request to Open WebUI!
|
|
Addressed in dev. |
Summary
When an LLM produces truncated or malformed JSON in tool call arguments (e.g., during long tool calls that exceed token limits), the SSE stream crashes with an unhandled exception. This PR adds graceful error handling: on parse failure, tool execution is skipped and a descriptive error message is returned to the LLM, allowing it to retry or respond appropriately.
Changes
middleware.py: Addtool_args_parse_errorflag whenjson.loads()fails on tool arguments. Guard tool execution behind this flag. On error, return a descriptive error message to the LLM instead of crashing.test_tool_call_parsing.py: 11 unit tests covering malformed JSON (truncated, empty, null, non-dict), valid JSON, and edge cases.Testing
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.
Signed-off-by: sxu75374 imshuaixu@gmail.com