Skip to content

fix(daemon): gracefully handle unrecognized custom models without throwing undefined API errors#175

Open
douo wants to merge 1 commit intosteipete:mainfrom
douo:fix/daemon-api-undefined
Open

fix(daemon): gracefully handle unrecognized custom models without throwing undefined API errors#175
douo wants to merge 1 commit intosteipete:mainfrom
douo:fix/daemon-api-undefined

Conversation

@douo
Copy link

@douo douo commented Mar 22, 2026

Description:

What does this PR do?
This PR fixes a bug in the daemon where chat requests would crash with a No API provider registered for api: undefined error if the user configured a custom, unrecognized model ID (e.g. openai/Qwen3-Coder-480B-A35B-Instruct) alongside a custom Base URL.

Why did this happen?
When @mariozechner/pi-ai's getModel function receives an unknown model ID, it safely returns undefined. Previously, the resolveModelWithFallback function in src/daemon/agent-model.ts was passing this undefined object directly to overrideModelBaseUrl. When a baseUrl was present, the destructured object { ...model, baseUrl } yielded an object that literally consisted of just { baseUrl: "..." }. The streamSimple engine then choked during execution because the critical .api property on this mock model object was entirely missing (undefined).

How was it fixed?
Added an explicit check (if (!model) throw new Error(...)) immediately after fetching the model from the PI-AI registry. By voluntarily throwing an error when the model is undefined, we correctly redirect the execution flow to the existing designated catch block for fallback logic. This catch block safely generates a comprehensive synthetic model containing the necessary api: "openai-completions" identifier, allowing conversations using local/custom endpoints to resume working flawlessly.

Copilot AI review requested due to automatic review settings March 22, 2026 13:38
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a daemon crash when a user configures an unrecognized/custom model ID (especially alongside a custom Base URL) by ensuring model resolution never returns an “incomplete” model object that’s missing required fields like api.

Changes:

  • Avoid passing an undefined model result into overrideModelBaseUrl by storing the getModel(...) result in a local variable.
  • Explicitly throw when getModel(...) returns no model so the existing fallback logic reliably generates a valid synthetic model.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +57 to +60
const model = getModel(provider as never, modelId as never);
if (!model) throw new Error(`Model not found: ${provider}/${modelId}`);
return overrideModelBaseUrl(
getModel(provider as never, modelId as never) as Model<Api>,
model as Model<Api>,
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new branch where getModel(...) returns a falsy value and we intentionally throw to enter the fallback path isn’t covered by tests. Please add a Vitest case (likely in tests/daemon.agent.test.ts, which already mocks getModel) that simulates getModel returning undefined for an unknown model ID while a custom base URL is configured, and assert completeAgentResponse still calls completeSimple with a model that has a defined api (and uses the overridden baseUrl). This guards against regressions of the original api: undefined crash.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

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.

2 participants