[PR #22118] [CLOSED] fix: add type guards in citation parser to prevent AttributeError #49549

Closed
opened 2026-04-30 01:50:49 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/22118
Author: @umut-polat
Created: 3/1/2026
Status: Closed

Base: devHead: fix/citation-parser-type-guard-v2


📝 Commits (1)

  • 02682c6 fix: add type guards in citation parser to prevent AttributeError

📊 Changes

1 file changed (+6 additions, -0 deletions)

View changed files

📝 backend/open_webui/utils/middleware.py (+6 -0)

📄 Description

Pull Request Checklist

  • Target branch: dev
  • Description: Type guards added to citation parser to prevent AttributeError when tools return error strings
  • Testing: Manually verified that passing a string to each tool branch now returns [] instead of crashing
  • Code review: Self-reviewed, minimal 6-line change

Changelog Entry

Description

When a tool (e.g. search_web) returns an error string instead of the expected data structure, get_citation_source_from_tool_result crashes with AttributeError: 'str' object has no attribute 'get'. This happens because the JSON parse failure silently falls through, leaving tool_result as a plain string.

Fixed

  • Added isinstance type guards in get_citation_source_from_tool_result for three tool branches:
    • search_web: checks result is a list before iterating
    • view_knowledge_file: checks result is a dict before calling .get()
    • query_knowledge_files: checks result is a list before iterating
  • Returns empty source list on type mismatch instead of crashing

Fixes #21070


Additional Information

Previous PRs (#21638, #21209, #21207, #21244, #21250) attempted to fix this but were closed. The root cause is still present in the current codebase — the isinstance guards are the minimal defensive fix.

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/22118 **Author:** [@umut-polat](https://github.com/umut-polat) **Created:** 3/1/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/citation-parser-type-guard-v2` --- ### 📝 Commits (1) - [`02682c6`](https://github.com/open-webui/open-webui/commit/02682c6481abc2e0c36e84cd6b3a313b2254f082) fix: add type guards in citation parser to prevent AttributeError ### 📊 Changes **1 file changed** (+6 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/utils/middleware.py` (+6 -0) </details> ### 📄 Description <!-- ⚠️ CRITICAL CHECKS FOR CONTRIBUTORS (READ, DON'T DELETE) ⚠️ 1. Target the `dev` branch. PRs targeting `main` will be automatically closed. 2. Do NOT delete the CLA section at the bottom. It is required for the bot to accept your PR. --> # Pull Request Checklist - [x] **Target branch:** `dev` - [x] **Description:** Type guards added to citation parser to prevent AttributeError when tools return error strings - [x] **Testing:** Manually verified that passing a string to each tool branch now returns `[]` instead of crashing - [x] **Code review:** Self-reviewed, minimal 6-line change # Changelog Entry ### Description When a tool (e.g. `search_web`) returns an error string instead of the expected data structure, `get_citation_source_from_tool_result` crashes with `AttributeError: 'str' object has no attribute 'get'`. This happens because the JSON parse failure silently falls through, leaving `tool_result` as a plain string. ### Fixed - Added `isinstance` type guards in `get_citation_source_from_tool_result` for three tool branches: - `search_web`: checks result is a `list` before iterating - `view_knowledge_file`: checks result is a `dict` before calling `.get()` - `query_knowledge_files`: checks result is a `list` before iterating - Returns empty source list on type mismatch instead of crashing Fixes #21070 --- ### Additional Information Previous PRs (#21638, #21209, #21207, #21244, #21250) attempted to fix this but were closed. The root cause is still present in the current codebase — the `isinstance` guards are the minimal defensive fix. ### 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-04-30 01:50: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#49549