# Function withModelNotFoundFallback

Adapters exports - Model adapters (Claude, OpenAI, Gemini, Ollama) Split from index.ts for file size compliance (Issue #285)

- withModelNotFoundFallback(
  inner: IModelAdapter,
  options?: ModelNotFoundFallbackOptions,
  ): IModelAdapter

  Decorate an IModelAdapter with retire-and-retry. Behaviour:

  1. Forward `complete(request)` to the inner adapter.
  2. On `MODEL_NOT_FOUND`: refresh the cache, find the closest same-family alternative, retry once with `request.model = <fallback>`. Original error is returned if no fallback found.
  3. The wrapper returns the SECOND error if the retry also fails.
  4. `stream()` retries via restart-from-zero (#2550): when the inner stream throws a `MODEL_NOT_FOUND` ModelError, the wrapper closes the failed stream, picks a same-family fallback via the cache + registry, builds a new adapter through `adapterFactory`, and yields chunks from the fallback stream. The consumer sees a clean second stream — partial content already delivered by the first stream is NOT replayed; that's a known trade-off (the alternative resume-with-reconciliation strategy is heavier and only useful for tool-use loops; not implemented per the deliberate scoping in #2550). Without `adapterFactory`, the original throw propagates unchanged.
  5. `countTokens`, `validateConfig`, `listModels` are passthrough.

  #### Parameters

  - inner: IModelAdapter
  - options: ModelNotFoundFallbackOptions = {}

  #### Returns IModelAdapter

  - Defined in packages/nexus-agents/src/adapters/model-not-found-fallback.ts:99