Skip to content

fix(models): use /v1/models?provider=<slug> and drop hardcoded fallbacks#7

Open
siddharthsambharia-portkey wants to merge 1 commit into
mainfrom
feat/models-fix
Open

fix(models): use /v1/models?provider=<slug> and drop hardcoded fallbacks#7
siddharthsambharia-portkey wants to merge 1 commit into
mainfrom
feat/models-fix

Conversation

@siddharthsambharia-portkey
Copy link
Copy Markdown
Collaborator

Summary

Fixes two issues with model selection during portkey setup:

  1. The CLI was showing more models than the API actually returns. We were merging a hardcoded MODEL_DEFAULTS table (anthropic, openai, azure, google, mistral, cohere, groq, deepseek) on top of the gateway's /v1/models response, so stale or invented models leaked into the picker.

  2. The provider slug was being sent as the x-portkey-provider header, but Portkey's /v1/models expects it as a query parameter (?provider=<slug>) — same shape as:

    curl 'https://api.portkey.ai/v1/models?provider=cohere-main-77fe39' \
         -H 'x-portkey-api-key: …'
    

Changes

  • src/api.js
    • fetchModels now hits GET /v1/models?provider=<slug> and no longer sends x-portkey-provider. Leading @ is stripped from the slug.
    • testGatewayConnection requires the caller to pass a model; the hardcoded claude-haiku-4-20250514 default is gone.
  • src/commands/claude-code/setup.js
    • Removed the MODEL_DEFAULTS map, getModelOptions(), and the merge step that appended curated suggestions to the API response. The picker now shows exactly what /v1/models?provider=<slug> returns.
    • Dropped the now-unused providerType tracking variable.
    • Softened hardcoded placeholder/initialValue strings (e.g. gpt-4o, e.g. claude-sonnet-4-20250514) in the Codex/alias flows.
    • In --yes mode with a Config (no single provider), the CLI now errors if --model isn't supplied instead of defaulting to gpt-4o.
    • Writes CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1 into the env block of settings.json (and the shell-rc env block) so Claude Code asks the Portkey gateway for the model list at runtime instead of using its hardcoded Anthropic list.
  • src/commands/claude-code/verify.js
    • No more claude-haiku-4-20250514 fallback — if no model is configured the CLI tells the user to run portkey setup.
  • src/commands/claude-code/uninstall.js
    • Adds CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY to ENV_KEYS_TO_REMOVE so uninstall cleans it up from settings.json and process.env.

Net effect

  • Only models the workspace can actually route are shown.
  • Request shape matches the documented Portkey API.
  • CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1 is set automatically, so Claude Code's /model picker also reflects the gateway catalog instead of Anthropic's bundled list.

Test plan

  • npm test — all 121 tests pass locally.
  • Manual: run npx portkey setup against a Cohere virtual key and confirm the picker shows exactly the 33 models returned by GET /v1/models?provider=cohere-main-77fe39 (no extras).
  • Manual: open ~/.claude/settings.json (or chosen scope) after setup and confirm env.CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY === "1".
  • Manual: run portkey uninstall and confirm the key is removed.
  • Manual: in Claude Code, run /model and confirm gateway-discovered models appear.

Made with Cursor

- Removed the normalization of provider headers and updated the fetchModels function to use the provider query parameter for model retrieval.
- Updated testGatewayConnection to require a model parameter, ensuring explicit model specification for connection tests.
- Removed fallback model suggestions in setup.js, enforcing exclusive use of models from the Portkey catalog.
- Enhanced error handling for missing model configurations in the verify function.
Copy link
Copy Markdown

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

This PR updates Claude Code setup/verification to rely exclusively on Portkey’s Model Catalog API (GET /v1/models?provider=<slug>) rather than merging in hardcoded model fallbacks, aligning CLI behavior with the gateway’s actual routable model set.

Changes:

  • Switch model discovery to /v1/models?provider=<slug> and remove x-portkey-provider usage for the models endpoint.
  • Remove bundled MODEL_DEFAULTS/curated model suggestions so the picker reflects only the API response.
  • Require an explicit configured model for verification/connection testing; add gateway model discovery env flag and ensure uninstall removes it.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/api.js Updates fetchModels request shape to use provider query param; makes testGatewayConnection require a caller-supplied model.
src/commands/claude-code/setup.js Removes hardcoded model fallback lists/merging; updates prompts; enforces --model in certain --yes config flows; writes CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1.
src/commands/claude-code/verify.js Drops hardcoded model fallback and now requires a configured model to run verification.
src/commands/claude-code/uninstall.js Ensures the new gateway model discovery env var is removed during uninstall.

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

const existing = readExistingConfig();
const gateway = process.env.ANTHROPIC_BASE_URL || existing.gateway || PORTKEY_GATEWAY;
const testModel = existing.model || "claude-haiku-4-20250514";
const testModel = existing.model;
Comment thread src/api.js
Comment on lines 107 to 111
try {
const data = await fetchJSON(`${base(gateway)}/v1/models`, {
"x-portkey-api-key": portkeyKey,
"x-portkey-provider": providerHeader,
const url = `${base(gateway)}/v1/models?provider=${encodeURIComponent(slug)}`;
const data = await fetchJSON(url, {
"x-portkey-api-key": portkeyKey,
});
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