Skip to content

Conversation

@yashwantbezawada
Copy link

Problem

The call_type parameter's Literal type hint was duplicated across 36+ files (CustomLogger subclasses), causing:

  1. DRY violation: Adding new call types (e.g., aembedding) required editing 36+ files manually
  2. Liskov Substitution Principle violation: Some subclasses had more restrictive types than the base class, causing MyPy errors
  3. Inconsistencies: Between CallTypes enum (singular: "embedding") and CustomLogger Literals (plural: "embeddings")
  4. Maintenance burden: PR fix: Use valid CallTypes enum value in embeddings endpoint #16328 couldn't update all files to add "aembedding" support

Solution

Created centralized TypeAliases in litellm/types/callbacks.py:

  • PreCallHookCallType: For async_pre_call_hook methods
  • ModerationHookCallType: For async_moderation_hook methods

Updated 36 files to use these TypeAliases instead of duplicating Literal definitions.

Impact

Code reduction: -439 lines (603 deletions, 164 insertions)

Files changed:

  • ✅ New: litellm/types/callbacks.py (centralized TypeAlias definitions)
  • ✅ Updated: litellm/integrations/custom_logger.py (base class)
  • ✅ Updated: 35 CustomLogger subclass files across:
    • litellm/proxy/guardrails/guardrail_hooks/ (24 files)
    • litellm/proxy/hooks/ (4 files)
    • litellm/proxy/example_config_yaml/ (2 files)
    • enterprise/ (6 files)

Benefits

Single source of truth: All call_type definitions in one place
Easier maintenance: Adding new call types requires editing 1 file instead of 36+
Fixes LSP violations: All subclasses now accept the same types as base class
Consistent type checking: MyPy will catch type errors consistently
Adds aembedding support: Async embeddings now supported (from PR #16328)
Future-proof: New call types can be added without touching 36+ files

Testing

This is a pure type system refactoring with no runtime changes:

  • Type hints in Python are not enforced at runtime (they're just hints for IDEs/MyPy)
  • All existing behavior is preserved
  • CI will validate MyPy type checking

Related Issues

Fixes #16362
Related: #16240 (guardrail validation bug), #16328 (aembedding PR)


Checklist:

  • Created centralized TypeAlias definitions
  • Updated base class (CustomLogger)
  • Updated all 36 subclass files
  • Verified no remaining duplicate Literals
  • Added aembedding to both TypeAliases
  • Commit message follows conventional commits format

@vercel
Copy link

vercel bot commented Nov 8, 2025

@yashwantbezawada is attempting to deploy a commit to the CLERKIEAI Team on Vercel.

A member of the Team first needs to authorize it.

…erriAI#16362)

## Problem
The `call_type` parameter's Literal type hint was duplicated across 36+ files
(CustomLogger subclasses), causing:
1. **DRY violation**: Adding new call types required editing 36+ files
2. **Liskov Substitution Principle violation**: Some subclasses had more restrictive
   types than the base class
3. **Inconsistencies**: Between CallTypes enum and CustomLogger Literals

## Solution
Created centralized TypeAliases in `litellm/types/callbacks.py`:
- `PreCallHookCallType`: For async_pre_call_hook methods
- `ModerationHookCallType`: For async_moderation_hook methods

Updated 36 files to use these TypeAliases instead of duplicating Literal definitions.

## Benefits
- ✅ Single source of truth for call_type values
- ✅ Easier maintenance: new types added in one place
- ✅ Fixes LSP violations: all subclasses accept same types as base
- ✅ Consistent type checking across all CustomLogger implementations
- ✅ Added support for "aembedding" (async embeddings) from PR BerriAI#16328

## Files Changed
- New: `litellm/types/callbacks.py` (TypeAlias definitions)
- Updated: `litellm/integrations/custom_logger.py` (base class)
- Updated: 35 CustomLogger subclass files across:
  - `litellm/proxy/guardrails/guardrail_hooks/`
  - `litellm/proxy/hooks/`
  - `enterprise/`

Fixes BerriAI#16362
Related: BerriAI#16240, BerriAI#16328
@yashwantbezawada yashwantbezawada force-pushed the refactor/call-type-literals-16362 branch from b26bcfe to f80ef7c Compare November 8, 2025 23:36
@krrishdholakia
Copy link
Contributor

i think i already fixed a lot of this on main @yashwantbezawada

@yashwantbezawada
Copy link
Author

Thanks for the heads up! Just checked and I see issue #16362 was already fixed via commit db378c4 using CallTypesLiteral from litellm.types.utils.

Closing this PR since the refactoring has been handled. Thanks for reviewing!

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.

Refactor: duplicate call_type Literal definitions aross +21 CustomLogger subclasses

2 participants