omp packages

pi-custom-model-provider0.1.3

Discover models from the OpenAI-compatible providers in your models.json and keep them refreshed.

zaypen Published 2 days ago MIT ~661 downloads/mo

Untested on omp

This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.

Did it work for you?

Result

One report per person. A new report replaces your old one.

Reports 0

No reports yet.

Readme

pi-custom-model-provider

Discover the model catalog of the OpenAI-compatible providers you already configured in ~/.pi/agent/models.json, instead of maintaining the list twice.

For every provider there whose api is openai-completions and that has a baseUrl, the extension:

  1. reads the model list from GET {baseUrl}/models at startup, including the context window, max output tokens, and pricing each entry carries,
  2. registers the result through pi.registerProvider(), so the models appear in /model and in pi --list-models,
  3. implements refreshModels, so Pi's own model refresh re-reads the endpoint with the effective credential.

Install

pi install npm:pi-custom-model-provider
# or from git
pi install git:github.com/zaypen/pi-custom-model-provider

Run /reload if Pi is already running.

Configuration

No config file of its own. This models.json entry is the whole setup:

{
  "providers": {
    "bifrost": {
      "baseUrl": "https://gateway.example.com/v1",
      "apiKey": "$BIFROST_API_KEY",
      "models": [
        { "id": "deepseek-r1", "reasoning": true, "contextWindow": 163840 }
      ]
    }
  }
}

With 30 models on the endpoint you write one of them.

Key resolution

~/.pi/agent/auth.json first (the entry Pi writes for that provider id when you run /login, type: "api_key"), then the apiKey value in models.json, which is expanded for $ENV_VAR and ${ENV_VAR}. Providers that resolve no key are registered from their models entries only, and the fetch is reported as no API key.

Live refreshes pass the credential Pi resolved to refreshModels, so a stored or refreshed credential is used in preference to the startup key.

Merge policy

The endpoint decides which models exist; models.json decides their metadata. If deepseek-r1 is in your models array and the endpoint returns it too, your contextWindow, cost, reasoning, input, name, api, and baseUrl win, field by field. A model you never wrote takes what the endpoint's catalog entry carries — context_length, or max_input_tokens when that is absent, becomes the context window, max_output_tokens the max tokens, and pricing the cost — and falls back to 128000 context window, 8192 max tokens, and zero cost for whatever the entry does not carry. input and reasoning are taken from the entry too when it carries them, see Input modalities and Reasoning.

Input modalities

Pi refuses an attached image for a model whose input is text-only, before the request reaches the gateway, so getting this wrong in either direction is user-visible. The extension reads it off the catalog entry instead of keeping a model-name table:

source shape example
architecture.input_modalities string array ["text", "image"]
architecture.modality string "text+image+video->text"
modalities.input string array ["text", "image", "video"]

Modalities Pi cannot express — video, audio, file — are dropped rather than folded into text/image. An entry with no modality data stays text-only.

This is Bifrost's own shape: its schemas.Model carries architecture.{modality,input_modalities,output_modalities}, and its pending custom-provider passthrough (maximhq/bifrost#9201, open) maps a raw upstream modalities.{input,output} onto input_modalities/output_modalities. Until that ships, Bifrost serves none of these fields for custom OpenAI-compatible providers — that is maximhq/bifrost#8914 — so models on those lanes stay text-only here, and no client-side config is a real substitute for data the gateway never sends.

Native provider packages are the exception, and it is a wide one. A measured Bifrost openrouter lane served architecture, reasoning, name, description, supported_parameters, top_provider and canonical_slug on every one of 8 entries, because the provider package parses its own list rather than going through the shared OpenAI-compatible decode.

Reasoning

Read from the entry, not from a name table:

endpoint value Pi reasoning
{"mandatory":true,"default_enabled":true} true
{"mandatory":false,"default_enabled":false} true
{"supported_efforts":["high","low"],"default_effort":"high"} true
true / false as given
{}, null, or absent false — or your models.json value

An object is read as capability, not as a default. In OpenRouter's catalog the field is omitted entirely for models without reasoning (mistralai/pixtral-large-2502, scaledown/toolcall:free) and sent with both flags false for models that support it but start with it off (google/gemma-4-26b-a4b-it:free). supported_parameters listing reasoning is deliberately not read: it lists accepted request parameters, not capability.

Pricing

Endpoint catalogs price per token, Pi prices per million tokens. prompt, completion, input_cache_read, and input_cache_write are scaled by 1e6, so "pricing": { "prompt": "0.0000003000" } registers as cost.input: 0.3. An empty pricing object is not a cost and leaves it at zero.

This assumes per-token pricing. A gateway that already reports USD per million tokens would read a million times too high; write cost in models.json for those.

Models that are in models.json but not returned by the endpoint stay registered. A refresh failure keeps the previously known list, metadata included.

Commands

  • /custom-models — re-fetch every matching provider and report.
  • /custom-models status — show model counts, how many came from the endpoint, how many of those brought a context window, and where the key came from.

Behaviour

  • PI_OFFLINE set: no startup fetch. models.json entries are still registered, and Pi's refresh callback is told to stay off the network.
  • Each fetch is capped at 8 seconds by default; the caller's signal is honoured on top. Set PI_CUSTOM_MODELS_TIMEOUT_MS to change it — raise it for a gateway that proxies a remote provider's model list live, lower it if a slow endpoint is holding up Pi's startup.

Limitations

  • Only openai-completions providers are fetched. Providers on other APIs are left alone, including openai-responses ones.
  • A gateway whose /models is slower than the fetch budget yields models.json entries only, silently keeping the defaults. Bifrost proxying a remote provider's list (openrouter.ai measured 3.6-7.4s aggregate) is the realistic case; raise PI_CUSTOM_MODELS_TIMEOUT_MS rather than removing the provider.
  • Endpoint numbers must be positive. 0, negatives, and non-numeric strings are treated as absent and fall through to the models.json value or the default.
  • pricing is assumed to be USD per token (see Pricing).
  • Vision is only as good as the endpoint catalog. An endpoint that reports no modality fields yields text-only models; input in models.json is the override, not the norm.
  • reasoning reads the presence of a recognised reasoning object as a capability claim. A gateway that emits that object for models which cannot reason would light up Pi's thinking toggle for them; models.json overrides either way.

Compared to its siblings

Three other Pi extensions discover models from OpenAI-compatible endpoints, so pick the one that matches your setup:

config source refresh metadata authority
this one models.json startup seed + native refreshModels models.json wins, endpoint fills the gaps
pi-model-discovery models.json its own commands endpoint wins
pi-dynamic-models separate dynamic-models.json startup + session_start its defaults

Differences that actually matter: this one finds keys that /login stored in auth.json, and hands discovery to Pi's refresh machinery rather than running on its own clock.

Development

Try it without installing:

pi -e ./extensions/index.ts

Tests:

node --experimental-strip-types test/discovery.mjs  # hermetic: fixture agent dir, stubbed fetch
node --experimental-strip-types test/run.mjs        # live: reads ~/.pi/agent, calls the real endpoints

test/discovery.mjs is self-contained. test/run.mjs runs against your real ~/.pi/agent/models.json and auth.json, so it expects to find a bifrost provider with a key and a keyless local provider there.

License

MIT