Skip to content

Fix handling of empty OpenAI API key - #4

Closed
quinneydavid wants to merge 1 commit into
LubergAlexander:masterfrom
quinneydavid:fix-empty-api-key-handling
Closed

Fix handling of empty OpenAI API key#4
quinneydavid wants to merge 1 commit into
LubergAlexander:masterfrom
quinneydavid:fix-empty-api-key-handling

Conversation

@quinneydavid

Copy link
Copy Markdown

Issue Fixed
This pull request addresses an issue where the application would attempt to use empty API keys for LLM services, causing runtime errors. Previously, the code only checked if environment variables were set (os.getenv("OPENAI_API_KEY")) but didn't validate whether they contained actual valid values.

Changes Made
The fix implements proper validation and fallback mechanisms in the LLMService class:

Added validation to check if API keys are empty, None, or just whitespace in all three main methods:

get_artist_recommendations()
get_track_recommendations()
generate_playlist_name()
Implemented a robust fallback mechanism:

When an OpenAI API key is empty or invalid, the service now logs a warning and automatically falls back to using Anthropic
Added model switching logic to ensure the appropriate model provider is used when falling back
Added a final validation check before API calls to ensure no empty API keys are used, raising a clear error message if no valid keys are available

Testing
The changes have been tested with various environment configurations:

With valid OpenAI API key
With empty OpenAI API key (falls back to Anthropic)
With no API keys set (raises appropriate error)
This fix improves application stability by preventing runtime errors when API keys are technically present in the environment but contain no usable value.

LubergAlexander added a commit that referenced this pull request Jul 21, 2026
…multi-library

Configurable LLM model/provider + multi-library support (supersedes #2, #3, #4)
@LubergAlexander

Copy link
Copy Markdown
Owner

Superseded by #5, which has been merged into master. The empty-API-key handling was reimplemented there: keys are validated before each call (empty/whitespace values are correctly treated as missing, which litellm.validate_environment alone misses), failing fast with a clear error instead of a hardcoded provider fallback. Thanks @quinneydavid — credited in the #5 commit. Closing as the work has landed.

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