Skip to content

fix: skip OpenAI model registration when no API key is configured#1453

Open
pintaste wants to merge 1 commit into
simonw:mainfrom
pintaste:fix/skip-openai-models-without-key
Open

fix: skip OpenAI model registration when no API key is configured#1453
pintaste wants to merge 1 commit into
simonw:mainfrom
pintaste:fix/skip-openai-models-without-key

Conversation

@pintaste
Copy link
Copy Markdown

Problem

When no OpenAI API key is set, llm still registers ~50 OpenAI models (and several embedding models) at startup. This causes several issues:

  • llm models output is cluttered with models the user cannot use
  • Model IDs like gpt-4o-mini collide with identically-named models from other plugins (e.g. a LiteLLM proxy)
  • The default model resolves to an OpenAI model, so bare llm invocations fail with No key found for users who don't use OpenAI at all

Fix

Add a guard at the top of register_models and register_embedding_models in the built-in OpenAI plugin. If no key is available (neither in keys.json nor OPENAI_API_KEY env var), registration is skipped entirely.

if not llm.get_key(alias="openai", env="OPENAI_API_KEY"):
    return

Users who later configure a key will see the models appear automatically on the next invocation.

Tests

  • Added test_openai_models_not_registered_without_key — verifies OpenAI Chat models are absent from llm models when no key is set
  • Added test_openai_models_registered_with_key — verifies OpenAI Chat models appear when a key is present
  • Updated conftest.py to set a fake OPENAI_API_KEY in the global env_setup fixture so existing tests that exercise OpenAI model features continue to pass
  • Added a file-level autouse fixture in test_cli_openai_models.py for explicitness

Fixes #1445

When no OpenAI API key is set (via keys.json or OPENAI_API_KEY env var),
register_models and register_embedding_models now return early, keeping
the model list clean for users who only use other providers.

Fixes simonw#1445
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.

register_models should skip model registration when no key is configured

1 participant