[PR #14949] [CLOSED] server: return 404 when model not found in ChatHandler #14937

Closed
opened 2026-04-13 01:06:14 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14949
Author: @haltandcatchwater
Created: 3/19/2026
Status: Closed

Base: mainHead: fix-chat-error-status


📝 Commits (2)

  • d810537 server: return 404 when model not found in ChatHandler
  • abc7182 server: add test for ChatHandler model-not-found response

📊 Changes

2 files changed (+14 additions, -1 deletions)

View changed files

📝 server/routes.go (+1 -1)
📝 server/routes_generate_test.go (+13 -0)

📄 Description

What

When getExistingName fails in ChatHandler, the error response is 400 Bad Request with the message "model is required". This is incorrect — at this point in the handler, the model name has already been parsed and validated by parseAndValidateModelRef. The failure is a lookup miss, not a missing parameter.

Fix

Return 404 Not Found with "model '<name>' not found", matching the behavior of GenerateHandler (line 228) and EmbedHandler (line 724) for the same failure.

Backward compatibility

I audited both official client libraries:

  • ollama-python: The client is status-code-agnostic — it wraps all HTTP errors into ResponseError(text, status_code) with no hardcoded status checks. The README's recommended error handling pattern explicitly checks e.status_code == 404 to trigger auto-pull for missing models. The current 400 response from /api/chat means this pattern silently fails to auto-pull, while the same model-not-found from /api/generate correctly triggers it.

  • ollama-js: Also status-code-agnostic — checkOk throws ResponseError for any non-2xx response without inspecting the code. No breakage.

  • LangChain: Uses a dedicated OllamaEndpointNotFoundError keyed on 404. This change would allow LangChain's error handling to work consistently for both /api/chat and /api/generate.

This change aligns the behavior with the decision in #6646, where 404 was explicitly kept for model-not-found because downstream consumers depend on it and OpenAI uses the same convention.

Before/After

Before (POST /api/chat with a model name that doesn't exist):

HTTP 400
{"error": "model is required"}

After:

HTTP 404
{"error": "model 'nonexistent-model' not found"}

🔄 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/ollama/ollama/pull/14949 **Author:** [@haltandcatchwater](https://github.com/haltandcatchwater) **Created:** 3/19/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix-chat-error-status` --- ### 📝 Commits (2) - [`d810537`](https://github.com/ollama/ollama/commit/d8105374d1972334f70a3a496c6f59f071fb4ed3) server: return 404 when model not found in ChatHandler - [`abc7182`](https://github.com/ollama/ollama/commit/abc718258eaddac3b9a24e2362c57f2bbc6901d4) server: add test for ChatHandler model-not-found response ### 📊 Changes **2 files changed** (+14 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `server/routes.go` (+1 -1) 📝 `server/routes_generate_test.go` (+13 -0) </details> ### 📄 Description ## What When `getExistingName` fails in `ChatHandler`, the error response is `400 Bad Request` with the message `"model is required"`. This is incorrect — at this point in the handler, the model name has already been parsed and validated by `parseAndValidateModelRef`. The failure is a lookup miss, not a missing parameter. ## Fix Return `404 Not Found` with `"model '<name>' not found"`, matching the behavior of `GenerateHandler` (line 228) and `EmbedHandler` (line 724) for the same failure. ## Backward compatibility I audited both official client libraries: - **ollama-python**: The client is status-code-agnostic — it wraps all HTTP errors into `ResponseError(text, status_code)` with no hardcoded status checks. The [README's recommended error handling pattern](https://github.com/ollama/ollama-python#errors) explicitly checks `e.status_code == 404` to trigger auto-pull for missing models. The current 400 response from `/api/chat` means this pattern silently fails to auto-pull, while the same model-not-found from `/api/generate` correctly triggers it. - **ollama-js**: Also status-code-agnostic — `checkOk` throws `ResponseError` for any non-2xx response without inspecting the code. No breakage. - **LangChain**: Uses a dedicated `OllamaEndpointNotFoundError` keyed on 404. This change would allow LangChain's error handling to work consistently for both `/api/chat` and `/api/generate`. This change aligns the behavior with the decision in #6646, where 404 was explicitly kept for model-not-found because downstream consumers depend on it and OpenAI uses the same convention. ## Before/After **Before** (`POST /api/chat` with a model name that doesn't exist): ```json HTTP 400 {"error": "model is required"} ``` **After**: ```json HTTP 404 {"error": "model 'nonexistent-model' not found"} ``` --- <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-13 01:06:14 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#14937