Skip to content

Refactor: Additional files with duplicate call_type Literal definitions (21 files) #16495

@Chesars

Description

@Chesars

Summary

While reviewing issue #16362, I found 21 additional files that have the same problem: duplicate inline Literal definitions for call_type parameters instead of using the centralized CallTypesLiteral type alias.

The original issue identified 17 files, and PR #16408 successfully updated 16 of them. However, there are 21 more files throughout the codebase with the same pattern that were not included in the original list.

Current Status

From Original Issue #16362:

  • 16 files updated in main
  • 1 file still pending: litellm/proxy/guardrails/guardrail_hooks/aporia_ai/aporia_ai.py (line 184)

Newly Identified Files (21):

Guardrails (12 files):

  1. litellm/proxy/guardrails/guardrail_hooks/azure/prompt_shield.py (line 117)
  2. litellm/proxy/guardrails/guardrail_hooks/azure/text_moderation.py (line 210)
  3. litellm/proxy/guardrails/guardrail_hooks/custom_guardrail.py (multiple lines)
  4. litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py (line 113)
  5. litellm/proxy/guardrails/guardrail_hooks/guardrails_ai/guardrails_ai.py (line 201)
  6. litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py (line 128)
  7. litellm/proxy/guardrails/guardrail_hooks/lasso/lasso.py (line 130)
  8. litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py (line 363)
  9. litellm/proxy/guardrails/guardrail_hooks/openai/moderations.py (line 187)
  10. litellm/proxy/guardrails/guardrail_hooks/panw_prisma_airs/panw_prisma_airs.py (line 567)
  11. litellm/proxy/guardrails/guardrail_hooks/pillar/pillar.py (line 191)
  12. litellm/proxy/guardrails/guardrail_hooks/tool_permission.py (line 274)

Proxy/Hooks (2 files):

  1. litellm/proxy/hooks/prompt_injection_detection.py (line 222)
  2. litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py

Core Library (6 files):

  1. litellm/router.py
  2. litellm/llms/openai/cost_calculation.py
  3. litellm/llms/vertex_ai/cost_calculator.py
  4. litellm/llms/huggingface/embedding/handler.py
  5. litellm/integrations/langfuse/langfuse_prompt_management.py
  6. litellm/litellm_core_utils/llm_response_utils/get_formatted_prompt.py

Others

  1. litellm/cost_calculator.py (line 1202) - This file uses CallTypesLiteral in most places but still has one inline Literal definition

Total: 22 files that need the same refactoring

Same as issue #16362:

  1. DRY - Adding new call types requires updating multiple files
  2. Liskov Substitution Principle violation - Subclasses may have more restrictive types than base class
  3. Maintainability - Centralized type definition is easier to maintain
  4. Consistency - All files follow the same pattern

Proposed Solution

Apply the same fix to these 21 files:

  1. Add import: from litellm.types.utils import CallTypesLiteral
  2. Replace inline Literal[...] with CallTypesLiteral

Example:

Before:

from typing import Literal

async def async_pre_call_hook(
    call_type: Literal[
        "completion",
        "embeddings",
        "image_generation",
        # ... more types
    ],
):
    pass

After:

from litellm.types.utils import CallTypesLiteral

async def async_pre_call_hook(
    call_type: CallTypesLiteral,
):
    pass

Verification Command

To find all remaining files with inline Literal definitions:

find litellm -name "*.py" -type f -print0 | xargs -0 grep -l "call_type.*:.*Literal\[" 2>/dev/null

Checklist


Note: Happy to submit a PR for this if helpful!

Related to

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions