Python Adapter Generalization for DSPex
Executive Summary
This document presents a comprehensive analysis and design for generalizing the DSPex Python adapter to support multiple ML frameworks beyond DSPy. The proposed architecture maintains backward compatibility while enabling easy integration of LangChain, Transformers, or custom Python ML libraries.
Key Findings
- The core infrastructure is already quite generic - Port communication, protocol handling, and session management can be reused as-is
- DSPy-specific logic is well-contained - Mainly in Python command handlers and signature-related modules
- Minimal changes needed - The adapter pattern provides good abstraction for new frameworks
- Production-ready foundation - Robust error handling, monitoring, and performance optimizations benefit all bridges
Proposed Architecture
The design introduces:
- Python:
BaseBridge
abstract class for framework-agnostic protocol handling - Elixir:
BaseMLAdapter
behaviour for shared infrastructure - Configuration: Centralized bridge registry and selection
- Migration: 5-phase plan preserving backward compatibility
Table of Contents
- Current Architecture Analysis
- Generalization Requirements
- Proposed Modular Architecture
- Implementation Examples
- Migration Strategy
- Performance Considerations
- Testing Strategy
Quick Start
For developers wanting to create custom bridges:
- Python Side: Inherit from
BaseBridge
and implement your command handlers - Elixir Side: Use
BaseMLAdapter
behaviour for your adapter - Configuration: Register your bridge in the application config
- Usage: Access via adapter directly or unified
MLBridge
interface
See implementation examples for complete code samples.
Benefits of Generalization
- Flexibility: Support multiple ML frameworks in one application
- Reusability: Leverage existing infrastructure for new bridges
- Maintainability: Clear separation of concerns
- Performance: Shared pooling and optimization benefits
- Testing: Unified test architecture for all bridges
Next Steps
- Review the detailed architecture in modular-architecture.md
- See example implementations in implementation-examples.md
- Follow the migration plan in migration-strategy.md