Skip to content

fix: publish session.error event for invalid model selection - #8451

Merged
rekram1-node merged 1 commit into
anomalyco:devfrom
surma-dump:surma/publish-invalid-model-errors
Feb 9, 2026
Merged

rekram1-node merged 1 commit into
anomalyco:devfrom
surma-dump:surma/publish-invalid-model-errors

Conversation

@surma

@surma surma commented Jan 14, 2026 •

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

Comment thread packages/opencode/src/session/prompt.ts Resolved
When sending a message via the API with an invalid provider/model ID,
the error was being thrown inside the loop() function but never
surfaced to API consumers. The HTTP response had already returned 200
and no session.error event was published.

This adds error handling around Provider.getModel() in loop() to
publish a session.error event before re-throwing, matching the
existing pattern in the command() function.
@rekram1-node

Copy link
Copy Markdown
Collaborator

/review

@github-actions

github-actions Bot commented Feb 9, 2026

Copy link
Copy Markdown
Contributor

lgtm

@rekram1-node
rekram1-node merged commit 832902c into anomalyco:dev Feb 9, 2026
3 checks passed
Colin4k1024 pushed a commit to Colin4k1024/opencode that referenced this pull request Feb 10, 2026
BryceRyan pushed a commit to BryceRyan/opencode that referenced this pull request Feb 10, 2026
relaxkeren pushed a commit to relaxkeren/opencode that referenced this pull request Feb 15, 2026
schneiderlo pushed a commit to schneiderlo/opencode that referenced this pull request Feb 17, 2026
Rwanbt pushed a commit to Rwanbt/unifia that referenced this pull request May 5, 2026
AIALRA-0 pushed a commit to AIALRA-0/opencode-turn-engine that referenced this pull request Jun 10, 2026
Rwanbt pushed a commit to Rwanbt/unifia that referenced this pull request Jun 14, 2026
meSingh pushed a commit to meSingh/opencode that referenced this pull request Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API doesn't report invalid model errors via event stream

2 participants