fix: return upstream errors to direct API callers instead of 200 null

API clients calling /api/chat/completions with a message id and a chat_id (or parent_id null), but without a session_id, got HTTP 200 with a body of `null` whenever the upstream model failed. The error was saved into the chat, but the caller had nothing to branch on, so a failed request looked exactly like an empty answer.

These requests are answered synchronously, so the error handler now raises the same 400 with the upstream message that plain API calls already get. The error is still written to the chat message first. Requests with a session_id keep running as background tasks and report errors over the socket, so the web UI, automations, channels and subagents are unchanged.

Verified against a mock Ollama returning 400: plain calls, chat_id + id and new-chat requests now all return 400 with the provider message. Successful completions (streaming and non-streaming) and session-based requests return the same responses as before.

Fixes #28938
This commit is contained in:
Classic298 2026-09-24 10:36:57 +02:00
parent bbfa876afd
commit a87129eb61

View file

@ -1709,11 +1709,8 @@ async def chat_completion(
except Exception:
pass
else:
# No chat_id/message_id → legacy/direct API path with no
# WebSocket error channel. We must surface the error as
# a proper HTTP response; without this the function would
# return None which FastAPI serializes as null. #23924
# Legacy/direct callers await this response; returning None would send `null`. #23924
if not (metadata.get('session_id') and metadata.get('chat_id')):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=error_detail,