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

Closed
opened 2026-05-06 10:33:58 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

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

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


📝 Commits (1)

  • fb510ab 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

Pull Request Checklist

  • Target branch: Verify that the pull request targets the dev branch.
  • Description: Provide a concise description of the changes made in this pull request down below.
  • Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
  • Testing: Performed manual tests to verify the implemented fix works as intended.
  • Agentic AI Code: This Pull Request has gone through additional human review AND manual testing.
  • Code review: Performed a self-review of the code.
  • Title Prefix: Prefixed with fix: to categorize this pull request.

Changelog Entry

Description

Fixes AttributeError: 'str' object has no attribute 'get' crash in citation parser when builtin tools like search_web fail (timeout, API error, etc.). When tools fail, they return {"error": "..."} as a JSON dict. The citation parser expected a list and iterated over the dict keys (strings), causing the AttributeError. 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.

Supersedes #21207 (closed due to missing CLA and wrong target branch).
Fixes #21070

Changed

  • Modified backend/open_webui/apps/webui/routers/utils.py:
    • Added early JSON parsing at the top of get_citation_source_from_tool_result()
    • Added error dict detection that returns empty list when {"error": "..."} is found
    • Reuses parsed JSON result across all tool branches, eliminating redundant json.loads calls

Fixed

  • Fixed AttributeError: 'str' object has no attribute 'get' crash when web search or other builtin tools fail
  • Fixed chat continuation - errors no longer crash the chat, allowing users to continue their conversation
  • Fixed redundant JSON parsing by parsing once and reusing the result

Additional Information

Root Cause

When builtin tools like search_web fail (timeout, API error, network issue, etc.), they return:

{"error": "timeout message here"}

The citation parser in get_citation_source_from_tool_result expected the result to be a list like:

[{"url": "...", "title": "...", "content": "..."}]

When it received an error dict instead, it iterated over the dict keys (["error"]), treating them as citation objects. When it tried to call tag.get('name') on the string "error", it crashed with AttributeError.

Solution

  1. Parse JSON once at the top
  2. Check if result is a dict with an "error" key - if so, return empty list immediately
  3. Continue with normal citation processing if no error detected
  4. Reuse parsed result to avoid redundant parsing

Screenshots or Videos

Test Results

Scenario 1: Web search API timeout

  • Before fix: Chat crashes with AttributeError: 'str' object has no attribute 'get', conversation cannot continue
  • After fix: Empty citations returned, chat continues normally, user can retry or continue conversation

Scenario 2: Web search API error (invalid key, rate limit, etc.)

  • Before fix: Same crash as above
  • After fix: Graceful handling, no crash, chat continues

Scenario 3: Normal web search with valid results

  • Before fix: Works correctly, citations displayed
  • After fix: Works correctly, citations displayed (no regression)

Scenario 4: view_knowledge_file and query_knowledge_files tools

  • Before fix: Work correctly
  • After fix: Work correctly (no regression)

Testing method:

  1. Configured web search with a search engine
  2. Simulated timeout by temporarily blocking network access to search API
  3. Triggered search in chat
  4. Verified chat continues without crash
  5. Restored network access and verified normal search still produces citations

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/21250 **Author:** [@veeceey](https://github.com/veeceey) **Created:** 2/8/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/issue-21070-citation-parser-error-v2` --- ### 📝 Commits (1) - [`fb510ab`](https://github.com/open-webui/open-webui/commit/fb510ab8b7cb0d14038bb70d96ad8d03aedd3c36) 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 # Pull Request Checklist - [x] **Target branch:** Verify that the pull request targets the `dev` branch. - [x] **Description:** Provide a concise description of the changes made in this pull request down below. - [x] **Changelog:** Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description. - [x] **Testing:** Performed manual tests to verify the implemented fix works as intended. - [x] **Agentic AI Code:** This Pull Request has gone through additional human review AND manual testing. - [x] **Code review:** Performed a self-review of the code. - [x] **Title Prefix:** Prefixed with `fix:` to categorize this pull request. # Changelog Entry ### Description Fixes `AttributeError: 'str' object has no attribute 'get'` crash in citation parser when builtin tools like `search_web` fail (timeout, API error, etc.). When tools fail, they return `{"error": "..."}` as a JSON dict. The citation parser expected a list and iterated over the dict keys (strings), causing the AttributeError. 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. Supersedes #21207 (closed due to missing CLA and wrong target branch). Fixes #21070 ### Changed - Modified `backend/open_webui/apps/webui/routers/utils.py`: - Added early JSON parsing at the top of `get_citation_source_from_tool_result()` - Added error dict detection that returns empty list when `{"error": "..."}` is found - Reuses parsed JSON result across all tool branches, eliminating redundant `json.loads` calls ### Fixed - Fixed `AttributeError: 'str' object has no attribute 'get'` crash when web search or other builtin tools fail - Fixed chat continuation - errors no longer crash the chat, allowing users to continue their conversation - Fixed redundant JSON parsing by parsing once and reusing the result --- ### Additional Information **Root Cause** When builtin tools like `search_web` fail (timeout, API error, network issue, etc.), they return: ```json {"error": "timeout message here"} ``` The citation parser in `get_citation_source_from_tool_result` expected the result to be a list like: ```json [{"url": "...", "title": "...", "content": "..."}] ``` When it received an error dict instead, it iterated over the dict keys (`["error"]`), treating them as citation objects. When it tried to call `tag.get('name')` on the string `"error"`, it crashed with `AttributeError`. **Solution** 1. Parse JSON once at the top 2. Check if result is a dict with an "error" key - if so, return empty list immediately 3. Continue with normal citation processing if no error detected 4. Reuse parsed result to avoid redundant parsing ### Screenshots or Videos **Test Results** **Scenario 1: Web search API timeout** - **Before fix:** Chat crashes with `AttributeError: 'str' object has no attribute 'get'`, conversation cannot continue - **After fix:** Empty citations returned, chat continues normally, user can retry or continue conversation **Scenario 2: Web search API error (invalid key, rate limit, etc.)** - **Before fix:** Same crash as above - **After fix:** Graceful handling, no crash, chat continues **Scenario 3: Normal web search with valid results** - **Before fix:** Works correctly, citations displayed - **After fix:** Works correctly, citations displayed (no regression) **Scenario 4: `view_knowledge_file` and `query_knowledge_files` tools** - **Before fix:** Work correctly - **After fix:** Work correctly (no regression) **Testing method:** 1. Configured web search with a search engine 2. Simulated timeout by temporarily blocking network access to search API 3. Triggered search in chat 4. Verified chat continues without crash 5. Restored network access and verified normal search still produces citations ### 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:33:58 -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#64850