21e309f 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
Summary
Fixes crash when web search or other builtin tools fail
Detects error responses early and returns empty sources list
Improves performance by parsing JSON only once
Problem
When search_web or other builtin tools encounter errors (timeout, API failure, etc.), they return {"error": "..."} instead of the expected list format. The citation parser in middleware.py was iterating over this dict, which yields string keys like "error". When the code tried to call .get() on these strings, it crashed with:
AttributeError: 'str' object has no attribute 'get'
Solution
Added early error detection at the start of get_citation_source_from_tool_result():
Parse JSON once and reuse the parsed result (performance improvement)
Check if the parsed result is a dict with an "error" key
If error detected, return empty list to allow chat to continue gracefully
Updated all tool handlers to use the pre-parsed result
Manual Testing
Test Setup
Open WebUI v0.7.2 (Docker)
Configure web search with any search engine
Simulated web search failures by:
Disabling network connectivity temporarily
Setting invalid API credentials
Using timeout values that cause search to fail
Before Fix
Triggered web search while API was unavailable
Result: Server error with stack trace in logs
ERROR | open_webui.utils.middleware:get_citation_source_from_tool_result:279
- Error parsing tool result for search_web: 'str' object has no attribute 'get'
Chat interaction failed, user saw error message
After Fix
Triggered web search while API was unavailable
Result: No crash, chat continues normally
Error logged cleanly, no stack trace
Search tool returns empty results, LLM responds without citations
UI remains functional
Additional Tests
Tested successful web search: citations display correctly
Tested query_knowledge_files error handling: works correctly
Tested view_knowledge_file error handling: works correctly
Performance: Confirmed JSON is parsed only once per tool call
Root Cause
In backend/open_webui/tools/builtin.py:178-180:
exceptExceptionase:log.exception(f"search_web error: {e}")returnjson.dumps({"error":str(e)})# Returns dict, not list!
In backend/open_webui/utils/middleware.py:163-170:
iftool_name=="search_web":results=json.loads(tool_result)# {"error": "..."} on failureforresultinresults:# Iterates over dict keys → "error"title=result.get("title","")# "error".get() → AttributeError!
🔄 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/21244
**Author:** [@veeceey](https://github.com/veeceey)
**Created:** 2/8/2026
**Status:** ❌ Closed
**Base:** `main` ← **Head:** `fix/issue-21070-citation-parser-error`
---
### 📝 Commits (1)
- [`21e309f`](https://github.com/open-webui/open-webui/commit/21e309ff549a3545948c6b70e1ca3778b6c5f241) 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
## Summary
- Fixes crash when web search or other builtin tools fail
- Detects error responses early and returns empty sources list
- Improves performance by parsing JSON only once
## Problem
When `search_web` or other builtin tools encounter errors (timeout, API failure, etc.), they return `{"error": "..."}` instead of the expected list format. The citation parser in `middleware.py` was iterating over this dict, which yields string keys like `"error"`. When the code tried to call `.get()` on these strings, it crashed with:
```
AttributeError: 'str' object has no attribute 'get'
```
## Solution
Added early error detection at the start of `get_citation_source_from_tool_result()`:
1. Parse JSON once and reuse the parsed result (performance improvement)
2. Check if the parsed result is a dict with an "error" key
3. If error detected, return empty list to allow chat to continue gracefully
4. Updated all tool handlers to use the pre-parsed result
## Manual Testing
### Test Setup
- Open WebUI v0.7.2 (Docker)
- Configure web search with any search engine
- Simulated web search failures by:
- Disabling network connectivity temporarily
- Setting invalid API credentials
- Using timeout values that cause search to fail
### Before Fix
1. Triggered web search while API was unavailable
2. Result: Server error with stack trace in logs
```
ERROR | open_webui.utils.middleware:get_citation_source_from_tool_result:279
- Error parsing tool result for search_web: 'str' object has no attribute 'get'
```
3. Chat interaction failed, user saw error message
### After Fix
1. Triggered web search while API was unavailable
2. Result: No crash, chat continues normally
3. Error logged cleanly, no stack trace
4. Search tool returns empty results, LLM responds without citations
5. UI remains functional
### Additional Tests
- Tested successful web search: citations display correctly
- Tested `query_knowledge_files` error handling: works correctly
- Tested `view_knowledge_file` error handling: works correctly
- Performance: Confirmed JSON is parsed only once per tool call
## Root Cause
In `backend/open_webui/tools/builtin.py:178-180`:
```python
except Exception as e:
log.exception(f"search_web error: {e}")
return json.dumps({"error": str(e)}) # Returns dict, not list!
```
In `backend/open_webui/utils/middleware.py:163-170`:
```python
if tool_name == "search_web":
results = json.loads(tool_result) # {"error": "..."} on failure
for result in results: # Iterates over dict keys → "error"
title = result.get("title", "") # "error".get() → AttributeError!
```
Fixes #21070
Generated with [Claude Code](https://claude.com/claude-code)
---
<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/21244
Author: @veeceey
Created: 2/8/2026
Status: ❌ Closed
Base:
main← Head:fix/issue-21070-citation-parser-error📝 Commits (1)
21e309fFix 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
Summary
Problem
When
search_webor other builtin tools encounter errors (timeout, API failure, etc.), they return{"error": "..."}instead of the expected list format. The citation parser inmiddleware.pywas iterating over this dict, which yields string keys like"error". When the code tried to call.get()on these strings, it crashed with:Solution
Added early error detection at the start of
get_citation_source_from_tool_result():Manual Testing
Test Setup
Before Fix
After Fix
Additional Tests
query_knowledge_fileserror handling: works correctlyview_knowledge_fileerror handling: works correctlyRoot Cause
In
backend/open_webui/tools/builtin.py:178-180:In
backend/open_webui/utils/middleware.py:163-170:Fixes #21070
Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.