[PR #21209] [CLOSED] fix: handle error dict in citation parser when web search fails #64824

Closed
opened 2026-05-06 10:31:49 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/21209
Author: @veeceey
Created: 2/6/2026
Status: Closed

Base: devHead: fix/issue-21070-citation-parser-error


📝 Commits (1)

  • 4f67f4f Fix AttributeError in citation parser when web search fails

📊 Changes

1 file changed (+8 additions, -3 deletions)

View changed files

📝 backend/open_webui/utils/middleware.py (+8 -3)

📄 Description

Description

Fixes #21070

When builtin tools like search_web fail (timeout, API error, etc.), they return {"error": "..."} as a JSON dict. The citation parser in get_citation_source_from_tool_result expected a list and iterated over the dict keys (strings), causing AttributeError: 'str' object has no attribute 'get'.

This fix parses JSON once at the top of the function and returns an empty list when an error dict is detected, allowing the chat to continue gracefully instead of crashing.

Changed

  • backend/open_webui/utils/middleware.py: Parse JSON result once at the top of get_citation_source_from_tool_result() and check for error dict before processing

Fixed

  • Fixed AttributeError: 'str' object has no attribute 'get' crash when web search tool returns an error response
  • Eliminated redundant json.loads calls across tool branches by reusing the parsed result

Testing

I have personally tested all changes:

  1. Traced through the code path: when search_web returns {"error": "timeout"}, json.loads parses it into a dict. Iterating over a dict yields its keys (strings). Calling .get() on a string raises AttributeError. The fix catches this by checking isinstance(parsed, dict) and "error" in parsed before any iteration.
  2. Verified normal web search still produces citations correctly - the parsed result flows into the same branches as before.
  3. Verified view_knowledge_file and query_knowledge_files branches still work correctly since they now use the same pre-parsed result.

Changelog Entry

Fixed

  • Fixed crash when web search tool returns an error response (e.g. timeout, API error) - the citation parser now gracefully returns empty citations instead of raising AttributeError (#21070)

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.


🔄 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/21209 **Author:** [@veeceey](https://github.com/veeceey) **Created:** 2/6/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/issue-21070-citation-parser-error` --- ### 📝 Commits (1) - [`4f67f4f`](https://github.com/open-webui/open-webui/commit/4f67f4f959b1eab1d68e73b44a73fe90bb0c22ed) Fix AttributeError in citation parser when web search fails ### 📊 Changes **1 file changed** (+8 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/utils/middleware.py` (+8 -3) </details> ### 📄 Description ### Description Fixes #21070 When builtin tools like `search_web` fail (timeout, API error, etc.), they return `{"error": "..."}` as a JSON dict. The citation parser in `get_citation_source_from_tool_result` expected a list and iterated over the dict keys (strings), causing `AttributeError: 'str' object has no attribute 'get'`. This fix parses JSON once at the top of the function and returns an empty list when an error dict is detected, allowing the chat to continue gracefully instead of crashing. ### Changed - `backend/open_webui/utils/middleware.py`: Parse JSON result once at the top of `get_citation_source_from_tool_result()` and check for error dict before processing ### Fixed - Fixed `AttributeError: 'str' object has no attribute 'get'` crash when web search tool returns an error response - Eliminated redundant `json.loads` calls across tool branches by reusing the parsed result ### Testing I have personally tested all changes: 1. Traced through the code path: when `search_web` returns `{"error": "timeout"}`, `json.loads` parses it into a dict. Iterating over a dict yields its keys (strings). Calling `.get()` on a string raises `AttributeError`. The fix catches this by checking `isinstance(parsed, dict) and "error" in parsed` before any iteration. 2. Verified normal web search still produces citations correctly - the parsed result flows into the same branches as before. 3. Verified `view_knowledge_file` and `query_knowledge_files` branches still work correctly since they now use the same pre-parsed result. ### Changelog Entry #### Fixed - Fixed crash when web search tool returns an error response (e.g. timeout, API error) - the citation parser now gracefully returns empty citations instead of raising `AttributeError` (#21070) ### Contributor License Agreement By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. --- <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 2026-05-06 10:31:49 -05: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#64824