When the generate_chat_completion functions in openai.py and ollama.py are called internally from chat.py (not via HTTP), the db parameter defaults to a Depends(get_session) object rather than an actual Session. This Depends object is truthy, so get_db_context attempted to use it as a session, causing Models.get_model_by_id to fail silently and return None.
With model_info being None, the base_model_id resolution was skipped, and the workspace model ID was passed directly to the API instead of its underlying base model.
Solution
Added a check in both openai.py and ollama.py to verify that db is an actual Session instance before using it. If not, db is set to None so get_db_context creates a fresh session.
Changes
openai.py: Added isinstance check for db parameter in generate_chat_completion
ollama.py: Added isinstance check for db parameter in generate_chat_completion
@tjbck
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.
Note
Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.
🔄 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/20344
**Author:** [@Classic298](https://github.com/Classic298)
**Created:** 1/3/2026
**Status:** ❌ Closed
**Base:** `dev` ← **Head:** `fix-db`
---
### 📝 Commits (1)
- [`6a17cee`](https://github.com/open-webui/open-webui/commit/6a17cee84629d63792b92d36a624d5852d3b17bf) fix
### 📊 Changes
**2 files changed** (+6 additions, -0 deletions)
<details>
<summary>View changed files</summary>
📝 `backend/open_webui/routers/ollama.py` (+3 -0)
📝 `backend/open_webui/routers/openai.py` (+3 -0)
</details>
### 📄 Description
### Problem
Workspace models (custom models created via Workspace > Models) were returning "Model not found" (404) errors when attempting to chat with them.
https://github.com/open-webui/open-webui/issues/20340
### Root Cause
When the generate_chat_completion functions in openai.py and ollama.py are called internally from chat.py (not via HTTP), the db parameter defaults to a Depends(get_session) object rather than an actual Session. This Depends object is truthy, so get_db_context attempted to use it as a session, causing Models.get_model_by_id to fail silently and return None.
With model_info being None, the base_model_id resolution was skipped, and the workspace model ID was passed directly to the API instead of its underlying base model.
### Solution
Added a check in both openai.py and ollama.py to verify that db is an actual Session instance before using it. If not, db is set to None so get_db_context creates a fresh session.
### Changes
- openai.py: Added isinstance check for db parameter in generate_chat_completion
- ollama.py: Added isinstance check for db parameter in generate_chat_completion
@tjbck
### 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.
> [!NOTE]
> Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.
---
<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/20344
Author: @Classic298
Created: 1/3/2026
Status: ❌ Closed
Base:
dev← Head:fix-db📝 Commits (1)
6a17ceefix📊 Changes
2 files changed (+6 additions, -0 deletions)
View changed files
📝
backend/open_webui/routers/ollama.py(+3 -0)📝
backend/open_webui/routers/openai.py(+3 -0)📄 Description
Problem
Workspace models (custom models created via Workspace > Models) were returning "Model not found" (404) errors when attempting to chat with them.
https://github.com/open-webui/open-webui/issues/20340
Root Cause
When the generate_chat_completion functions in openai.py and ollama.py are called internally from chat.py (not via HTTP), the db parameter defaults to a Depends(get_session) object rather than an actual Session. This Depends object is truthy, so get_db_context attempted to use it as a session, causing Models.get_model_by_id to fail silently and return None.
With model_info being None, the base_model_id resolution was skipped, and the workspace model ID was passed directly to the API instead of its underlying base model.
Solution
Added a check in both openai.py and ollama.py to verify that db is an actual Session instance before using it. If not, db is set to None so get_db_context creates a fresh session.
Changes
@tjbck
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.