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

Closed
opened 2026-04-20 06:32:04 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

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

Base: mainHead: fix/citation-parser-type-guard


📝 Commits (1)

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

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 (pass), leaving tool_result as a plain string. The code then tries to iterate over the string and call .get() on individual characters.

Changes

Added isinstance type guards before accessing parsed data in three tool branches:

  • search_web: checks tool_result is a list before iterating
  • view_knowledge_file: checks tool_result is a dict before calling .get()
  • query_knowledge_files: checks tool_result is a list before iterating

Returns an empty source list on type mismatch instead of crashing.

Checklist

  • Bug fix (non-breaking change which fixes an issue)
  • Changes are minimal and focused on the reported issue

Fixes #21070


🔄 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/22117 **Author:** [@umut-polat](https://github.com/umut-polat) **Created:** 3/1/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/citation-parser-type-guard` --- ### 📝 Commits (1) - [`7ba2500`](https://github.com/open-webui/open-webui/commit/7ba250071060703d23926a60ef51a412995945ee) 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 ### 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 (`pass`), leaving `tool_result` as a plain string. The code then tries to iterate over the string and call `.get()` on individual characters. ### Changes Added `isinstance` type guards before accessing parsed data in three tool branches: - **`search_web`**: checks `tool_result` is a `list` before iterating - **`view_knowledge_file`**: checks `tool_result` is a `dict` before calling `.get()` - **`query_knowledge_files`**: checks `tool_result` is a `list` before iterating Returns an empty source list on type mismatch instead of crashing. ### Checklist - [x] Bug fix (non-breaking change which fixes an issue) - [x] Changes are minimal and focused on the reported issue Fixes #21070 --- <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-20 06:32:04 -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#26500