#190·zerox

Support passing api_key via kwargs instead of only environment variables

Author: avirajsingh7Created Sep 1, 2025Updated Apr 12, 2026

Currently, the litellmmodel class in the zerox module requires API keys (e.g., OPENAI_API_KEY) to be set in environment variables.

Even if you pass an api_key via kwargs at initialization, it is not forwarded to validate_environment or validate_access. This results in errors like:

Required environment variable (keys) from the model are Missing. Please set the required environment variables for the model provider.
missing_keys: ['OPENAI_API_KEY']

This is unexpected, because litellm.validate_environment() already supports an api_key argument:

def validate_environment(
    model: Optional[str] = None,
    api_key: Optional[str] = None,
    api_base: Optional[str] = None,
) -> dict:
    ...

But in litellmmodel.init, we never forward the provided api_key:

def validate_environment(self) -> None:
    env_config = litellm.validate_environment(model=self.model)  # api_key is dropped

This makes it impossible to use zerox in environments where setting global environment variables is not desirable (e.g., multi-tenant services, per-request credentials, testing).

Proposed Fix

Capture api_key from kwargs and Pass it into both validate_environment and validate_access.

Why this matters

  • validate_environment explicitly accepts api_key, so skipping it in litellmmodel seems like a bug or oversight.
  • This would allow per-request API key injection (critical for multi-tenant apps).
  • It aligns the behavior with what the function signature already suggests should be supported.