Skip to content

refactor: Reduce code duplication in files/main.py using provider credential helpers #17268

@Chesars

Description

@Chesars

Summary

litellm/files/main.py has significant code duplication. Each function (create_file, file_retrieve, file_delete, file_list, file_content) repeats nearly identical credential/configuration logic for each provider.

Provider create_file file_retrieve file_delete file_list file_content
OpenAI ~20 lines ~20 lines ~20 lines ~20 lines ~20 lines
Azure ~20 lines ~20 lines ~20 lines ~20 lines ~20 lines
Vertex ~15 lines - - - ~15 lines
Anthropic ~5 lines ✓ ~5 lines ✓ ~5 lines ✓ ~5 lines ✓ ~5 lines ✓

Estimated ~400+ lines of repeated code that could be reduced significantly with helpers.

Proposed Solution

Create get_api_credentials() helpers in each provider's common_utils.py, similar to what was done for Anthropic in PR #16594:

# Example: litellm/llms/openai/common_utils.py
class OpenAIModelInfo:
    @staticmethod
    def get_api_credentials(api_base=None, api_key=None, organization=None):
        # centralized logic here
        return api_base, api_key, organization

In PR #16594, Anthropic's file operations were refactored:

  • Before: ~60 lines of repeated credential logic
  • After: ~20 lines using AnthropicModelInfo.get_api_credentials()

The same approach could reduce OpenAI and Azure code by similar amounts.

Files

  • litellm/files/main.py
  • litellm/llms/openai/common_utils.py
  • litellm/llms/azure/common_utils.py

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