Skip to content

KarimTalbi/llm-key-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LLM Key Validator

Tests License: MIT Python Version Code style: black

A lightweight, dependency-minimal Python library designed to validate API keys and retrieve available models across multiple LLM providers.

Why LLM Key Validator?

When building AI-integrated applications, managing multiple API keys can be error-prone. This tool provides a unified interface to:

  • Instantly Validate Keys: Verify if an API key is active and has the necessary permissions without sending complex prompts.
  • Auto-Detect Providers: Automatically identify whether a key belongs to OpenAI, Anthropic, or Groq based on its format.
  • List & Classify Models: Fetch all available models for a specific key and categorize them by type (Chat, Embedding, Image, Audio, etc.).
  • Minimal Overhead: Built with modern Python and httpx for high-performance asynchronous validation.

Supported Providers

Currently, the library supports:

  • OpenAI: Validates sk- format keys.
  • Anthropic: Validates sk-ant- format keys.
  • Google Gemini: Validates API keys for the Generative Language API.
  • Groq: Validates gsk_ format keys.

Installation

This project uses uv for modern, fast dependency management.

# Using uv (recommended)
uv pip install llm-key-validator

# Using standard pip
pip install llm-key-validator

Quick Start

1. Automatic Provider Detection

If you have a string and don't know which provider it belongs to, use detect_provider.

from llm_key_validator import detect_provider

key = "sk-ant-..."
provider = detect_provider(key)
print(f"Detected: {provider}") # Output: Provider.ANTHROPIC

2. Validating a Key and Fetching Models

Use the create_validator factory to get the correct validator and audit the key's capabilities.

import asyncio
from llm_key_validator import create_validator, Provider

async def check_key():
    # Initialize the validator
    validator = create_validator(Provider.OPENAI, "your-api-key-here")

    # Check if the key is valid
    if await validator.validate():
        print(f"Key for {validator.masked_key} is valid!")

        # Get models grouped by their purpose (Chat, Image, etc.)
        models = await validator.get_models_grouped()
        for model_type, names in models.items():
            print(f"{model_type.value}: {len(names)} models available")
    else:
        print("Invalid API Key.")

asyncio.run(check_key())

Project Structure

  • factory.py: The main entry point for creating validators and detecting types.
  • base.py: Abstract base class defining the validation interface.
  • providers/: Specific implementations for each API (OpenAI, Gemini, etc.).
  • enums.py: Strongly typed Provider and ModelType definitions.

Development

This project adheres to high code-quality standards using ruff for linting and black for formatting.

# Run linting
uv run ruff check .

# Run formatting
uv run black .

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A lightweight, asynchronous Python library to validate API keys and classify available models for OpenAI, Anthropic, Gemini, and Groq.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages