Skip to content

fix: graceful error handling for malformed tool call arguments#21984

Closed
sxu75374 wants to merge 20 commits intoopen-webui:devfrom
sxu75374:fix/graceful-malformed-tool-args
Closed

fix: graceful error handling for malformed tool call arguments#21984
sxu75374 wants to merge 20 commits intoopen-webui:devfrom
sxu75374:fix/graceful-malformed-tool-args

Conversation

@sxu75374
Copy link

@sxu75374 sxu75374 commented Feb 28, 2026

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: Add tool_args_parse_error flag when json.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

  • ✅ Personally tested with intentionally truncated tool call JSON — stream continues, LLM receives error and retries
  • ✅ All 11 new unit tests pass
  • ✅ Existing tool call flows unaffected (normal JSON parsing path unchanged)

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

tjbck and others added 20 commits November 23, 2025 22:10
* 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>
@pr-validator-bot
Copy link

🚫 STOP: MISSING CLA

@sxu75374, your PR description is missing the Contributor License Agreement confirmation.
Please use the PR template when creating your pull request. The template includes the required CLA text and helps ensure you provide all necessary information.
The CLA is absolutely required for ALL PRs. Your PR will be closed due to the missing CLA.
You can find more information about our CLA in the repository documentation, the /docs repository, the PR template and the contributing guideline.


👋 Welcome and Thank You for Contributing!

We appreciate you taking the time to submit a pull request to Open WebUI!

⚠️ Important: Testing Requirements

We've recently seen an increase in PRs that have significant issues:

  • PRs that don't actually fix the bug they claim to fix
  • PRs that don't implement the feature they describe
  • PRs that break existing functionality
  • PRs that are clearly AI-generated without proper testing being done by the author
  • PRs that simply don't work as intended

These untested PRs consume significant time from maintainers and volunteer contributors who review and test PRs in their free time.
Time that could be spent testing other PRs or improving Open WebUI in other ways.

Before marking your PR as "Ready for Review":

Please explicitly confirm:

  1. ✅ You have personally tested ALL changes in this PR
  2. How you tested it (specific steps you took to verify it works)
  3. Visual evidence where applicable (screenshots or videos showing the feature/fix working) - if applicable to your specific PR

If you're not certain your PR works exactly as intended, please leave it in DRAFT mode until you've thoroughly tested it.

Thank you for helping us maintain quality and respecting the time of our community! 🙏

@tjbck
Copy link
Contributor

tjbck commented Mar 1, 2026

Addressed in dev.

@tjbck tjbck closed this Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants