[PR #8451] fix: publish session.error event for invalid model selection #12747

Closed
opened 2026-02-16 18:17:38 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/8451

State: closed
Merged: Yes


Fix: Publish session.error event for invalid model selection

Problem

When sending a message via the API with an invalid provider or model ID, the error was silently swallowed. API consumers had no way to know something went wrong.

What happened:

  1. POST to /session/{id}/message with { model: { providerID: "invalid", ... } }
  2. HTTP response returns 200 immediately (before validation)
  3. User message is created successfully
  4. Session goes busyidle with no error indication
  5. No session.error event is published to /event

Root cause: The Provider.getModel() call happens inside loop() after the HTTP response has already started streaming. When it throws ModelNotFoundError, the error propagates up but is never converted to a session.error event.

Solution

Added error handling around Provider.getModel() in loop() to publish a session.error event before re-throwing. This matches the existing pattern used in the command() function.

After this fix:

data: {"type":"session.error","properties":{"sessionID":"ses_...","error":{"name":"UnknownError","data":{"message":"Model not found: invalid/some-model. Did you mean: anthropic, openai, ...?"}}}}

Changes

  • packages/opencode/src/session/prompt.ts: Wrap Provider.getModel() in try-catch, publish session.error on ModelNotFoundError

Fixes #8460

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/8451 **State:** closed **Merged:** Yes --- # Fix: Publish session.error event for invalid model selection ## Problem When sending a message via the API with an invalid provider or model ID, the error was silently swallowed. API consumers had no way to know something went wrong. **What happened:** 1. POST to `/session/{id}/message` with `{ model: { providerID: "invalid", ... } }` 2. HTTP response returns 200 immediately (before validation) 3. User message is created successfully 4. Session goes `busy` → `idle` with no error indication 5. No `session.error` event is published to `/event` **Root cause:** The `Provider.getModel()` call happens inside `loop()` after the HTTP response has already started streaming. When it throws `ModelNotFoundError`, the error propagates up but is never converted to a `session.error` event. ## Solution Added error handling around `Provider.getModel()` in `loop()` to publish a `session.error` event before re-throwing. This matches the existing pattern used in the `command()` function. **After this fix:** ``` data: {"type":"session.error","properties":{"sessionID":"ses_...","error":{"name":"UnknownError","data":{"message":"Model not found: invalid/some-model. Did you mean: anthropic, openai, ...?"}}}} ``` ## Changes - `packages/opencode/src/session/prompt.ts`: Wrap `Provider.getModel()` in try-catch, publish `session.error` on `ModelNotFoundError` Fixes #8460
yindo added the pull-request label 2026-02-16 18:17:38 -05:00
yindo closed this issue 2026-02-16 18:17:38 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12747