Skip to content

Fix AI assistant crash when the AI server errors mid-stream#4942

Open
elias-ba wants to merge 1 commit into
mainfrom
4941-ai-assistant-streaming-error
Open

Fix AI assistant crash when the AI server errors mid-stream#4942
elias-ba wants to merge 1 commit into
mainfrom
4941-ai-assistant-streaming-error

Conversation

@elias-ba

@elias-ba elias-ba commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Description

The AI assistant talks to the Apollo AI server over a streaming connection. When that server replied with an error status, the code that handles errors assumed it was dealing with an ordinary JSON response and tried to read a field off it. Over a streaming connection the response body isn't a plain object, so this crashed, the background job was discarded, and the real error was hidden from the user.

This fixes the error handler so an error response over a streaming connection now returns a clean error message instead of crashing. The failing HTTP status is also included in the log so the underlying cause is easier to trace.

Closes #4941

Validation steps

  1. With the AI assistant enabled, open the assistant on a job (or the workflow chat) in a project you can edit.
  2. Put the AI server into a failing state so a request comes back unsuccessful, for example point the Apollo endpoint at an unreachable or misconfigured URL.
  3. Send a message to the assistant.
  4. Confirm the assistant shows a clean error message and the chat stays usable: the message should not hang on a loading state and the editor session should not drop. Before this fix, an error returned mid-stream crashed the handler and the message silently failed.

Additional notes for the reviewer

AI Usage

Please disclose whether you've used AI anywhere in this PR (it's cool, we just
want to know!):

  • I have used Claude Code
  • I have used another model
  • I have not used AI

You can read more details in our
Responsible AI Policy

Pre-submission checklist

  • I have performed an AI review of my code (we recommend using /review with Claude Code)
  • I have implemented and tested all related authorization policies. (e.g., :owner, :admin, :editor, :viewer)
  • I have updated the changelog.
  • I have ticked a box in "AI usage" in this PR

@github-project-automation github-project-automation Bot moved this to New Issues in Core Jul 6, 2026
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

Security Review ✅

  • S0 (project scoping): N/A — the change only alters how an error message is extracted from an HTTP response body in lib/lightning/ai_assistant/ai_assistant.ex:1437; no queries or data-access paths were introduced or modified.
  • S1 (authorization): N/A — no new controller actions, LiveView events, or channel handlers; the diff refactors a private helper (error_message_from_body/1) inside an already-gated flow.
  • S2 (audit trail): N/A — no config-resource writes (workflows, credentials, project settings, etc.); the change touches only error-response handling.

@elias-ba elias-ba requested a review from midigofrank July 6, 2026 16:05
@elias-ba elias-ba force-pushed the 4941-ai-assistant-streaming-error branch 2 times, most recently from 2b63c6c to 8bab657 Compare July 7, 2026 03:35
Streaming AI requests keep the response body as a lazy stream, but the
error handler assumed a decoded map and keyed into it directly. A
non-success status from a streaming endpoint therefore crashed the
handler and discarded the background job, masking the real error.

Return a clean error message unless the body is a plain map with a
string "message"; any other shape (a stream body, or a nested or
non-string message) falls back to a generic message, and the failing
HTTP status is logged.

Fixes #4941
@elias-ba elias-ba force-pushed the 4941-ai-assistant-streaming-error branch from 8bab657 to 4c9f21d Compare July 7, 2026 03:57
@elias-ba elias-ba requested a review from stuartc July 7, 2026 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New Issues

Development

Successfully merging this pull request may close these issues.

AI assistant error handler crashes on non-2xx streaming responses

1 participant