[PR #24563] [CLOSED] fix: guard metadata chat_id None before startswith in middleware #115035

Closed
opened 2026-05-18 15:57:52 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/24563
Author: @MikeWang0316tw
Created: 5/11/2026
Status: Closed

Base: devHead: fix/chat-id-none-startswith-error-dev


📝 Commits (1)

  • 6884d59 fix: guard metadata chat_id None before startswith in middleware

📊 Changes

1 file changed (+9 additions, -9 deletions)

View changed files

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

📄 Description

Before submitting, make sure you've checked the following:

  • Linked Issue/Discussion: Closes #24553 / Relates to #24550, [PR #17801] [MERGED] feat: add permission toggle for public sharing of notes (#24554)
  • Target branch: dev
  • Description: Bug fix for 'NoneType' object has no attribute 'startswith' regression in v0.9.5
  • Changelog: See below
  • Documentation: No user-facing behaviour change
  • Dependencies: None
  • Testing: Manually verified — calling POST /api/v1/chat/completions without chat_id now works; web UI chat sessions unaffected
  • Agentic AI Code: This PR has been reviewed and manually tested by the human author
  • Code review: Self-reviewed
  • Git Hygiene: Single atomic commit

Changelog Entry

Description

v0.9.5 introduced channel: prefix support in middleware.py, adding multiple metadata['chat_id'].startswith('channel:') calls. When /api/v1/chat/completions is called from an external client without a chat_id field (IDE extensions, CLI tools, API scripts), metadata['chat_id'] is None, causing:

{"detail":"'NoneType' object has no attribute 'startswith'"}

The web UI is unaffected because browser sessions always supply chat_id. Only headless / programmatic API callers are impacted.

Fixed

  • middleware.py: replaced unguarded metadata['chat_id'].startswith(...) with metadata.get('chat_id') and not metadata['chat_id'].startswith(...) at 9 call sites in non_streaming_chat_response_handler and streaming_chat_response_handler. A None chat_id now correctly skips DB persistence, matching the behaviour for requests without a session.

Reproduce before fix

curl -X POST http://localhost:3000/api/v1/chat/completions \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"model":"<model>","messages":[{"role":"user","content":"hi"}]}'
# Returns: {"detail":"'NoneType' object has no attribute 'startswith'"}

After fix: returns a valid completion response.


Contributor License Agreement


🔄 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/24563 **Author:** [@MikeWang0316tw](https://github.com/MikeWang0316tw) **Created:** 5/11/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/chat-id-none-startswith-error-dev` --- ### 📝 Commits (1) - [`6884d59`](https://github.com/open-webui/open-webui/commit/6884d59ac209b7a80eafe688ec44d890207881e7) fix: guard metadata chat_id None before startswith in middleware ### 📊 Changes **1 file changed** (+9 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/utils/middleware.py` (+9 -9) </details> ### 📄 Description **Before submitting, make sure you've checked the following:** - [x] **Linked Issue/Discussion:** Closes #24553 / Relates to #24550, #24554 - [x] **Target branch:** `dev` - [x] **Description:** Bug fix for `'NoneType' object has no attribute 'startswith'` regression in v0.9.5 - [ ] **Changelog:** See below - [ ] **Documentation:** No user-facing behaviour change - [ ] **Dependencies:** None - [x] **Testing:** Manually verified — calling `POST /api/v1/chat/completions` without `chat_id` now works; web UI chat sessions unaffected - [x] **Agentic AI Code:** This PR has been reviewed and manually tested by the human author - [x] **Code review:** Self-reviewed - [x] **Git Hygiene:** Single atomic commit # Changelog Entry ### Description `v0.9.5` introduced `channel:` prefix support in `middleware.py`, adding multiple `metadata['chat_id'].startswith('channel:')` calls. When `/api/v1/chat/completions` is called from an external client without a `chat_id` field (IDE extensions, CLI tools, API scripts), `metadata['chat_id']` is `None`, causing: ``` {"detail":"'NoneType' object has no attribute 'startswith'"} ``` The web UI is unaffected because browser sessions always supply `chat_id`. Only headless / programmatic API callers are impacted. ### Fixed - `middleware.py`: replaced unguarded `metadata['chat_id'].startswith(...)` with `metadata.get('chat_id') and not metadata['chat_id'].startswith(...)` at 9 call sites in `non_streaming_chat_response_handler` and `streaming_chat_response_handler`. A `None` `chat_id` now correctly skips DB persistence, matching the behaviour for requests without a session. ### Reproduce before fix ```bash curl -X POST http://localhost:3000/api/v1/chat/completions \ -H "Authorization: Bearer <token>" \ -H "Content-Type: application/json" \ -d '{"model":"<model>","messages":[{"role":"user","content":"hi"}]}' # Returns: {"detail":"'NoneType' object has no attribute 'startswith'"} ``` After fix: returns a valid completion response. --- ### Contributor License Agreement - [x] 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-18 15:57:52 -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#115035