Skip to content

Support passing api_key via kwargs instead of only environment variables #190

@avirajsingh7

Description

@avirajsingh7

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions