Skip to content

Refactor: Create standardized provider interfaces with type-safe protocols #1534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jxnl opened this issue May 16, 2025 · 0 comments
Open
Labels
documentation Improvements or additions to documentation enhancement New feature or request python Pull requests that update python code

Comments

@jxnl
Copy link
Collaborator

jxnl commented May 16, 2025

Currently, each provider implements similar but slightly different interfaces, making it difficult to ensure consistent behavior and understand what each provider supports. We should create a standardized provider interface that all implementations must adhere to.

Current problems:

  • Inconsistent implementation across providers
  • No clear contract for what methods/features each provider must implement
  • Difficult to document provider capabilities
  • Type checkers can't verify provider compatibility
  • Hard to determine which modes are supported by each provider

Proposed solution:

Define a clear provider interface using Protocol/ABC:

# providers/base.py
from typing import Protocol, TypeVar, Generic, List, Dict, Any, Optional, Union, Iterator
from pydantic import BaseModel
from ..mode import Mode

T = TypeVar('T', bound=BaseModel)

class ProviderProtocol(Protocol, Generic[T]):
    """Protocol defining what all providers must implement."""
    
    @property
    def supported_modes(self) -> List[Mode]:
        """Modes supported by this provider."""
        ...
    
    @property
    def capabilities(self) -> Dict[str, bool]:
        """Provider capabilities (streaming, function_calling, etc.)"""
        ...
    
    def create(self, 
               response_model: Type[T], 
               messages: List[Dict[str, Any]], 
               mode: Mode,
               **kwargs) -> T:
        """Create structured output from messages."""
        ...
        
    def create_stream(self, 
                     response_model: Type[T], 
                     messages: List[Dict[str, Any]],
                     mode: Mode,
                     **kwargs) -> Iterator[T]:
        """Stream structured output from messages."""
        ...
    
    # Additional methods for function calling, async variants, etc.

Additionally, provide a base class implementation with default behaviors and utility methods that providers can inherit from.

Implementation steps:

  1. Define the ProviderProtocol in providers/base.py
  2. Implement ProviderBase with common functionality
  3. Convert each provider to implement this interface
  4. Add capability introspection
  5. Update documentation to reflect standardized interface

Benefits:

  1. Clear contract for what each provider must implement
  2. Type-safe provider usage
  3. Self-documenting provider capabilities
  4. Easier to implement new providers
  5. Better IDE support and autocompletion
@jxnl jxnl added the enhancement New feature or request label May 16, 2025
@github-actions github-actions bot added documentation Improvements or additions to documentation python Pull requests that update python code labels May 16, 2025
jxnl added a commit that referenced this issue May 16, 2025
Addresses issue #1534 by creating:
- Provider protocol and base class implementation
- Provider registry system for registration and discovery
- Sample implementation for OpenAI provider
- Documentation for provider system

This is the first step toward reorganizing the provider architecture.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request python Pull requests that update python code
Projects
None yet
Development

No branches or pull requests

1 participant