0
# Version Compatibility
1
2
The version compatibility module provides utilities and constants for handling different versions of Apache Airflow. This module enables the OpenAI provider to maintain compatibility across Airflow 2.x and future 3.x versions by conditionally importing the appropriate base classes and providing version detection capabilities.
3
4
## Capabilities
5
6
### Version Detection
7
8
Functions and constants for detecting the current Airflow version and enabling version-specific behavior.
9
10
```python { .api }
11
def get_base_airflow_version_tuple() -> tuple[int, int, int]:
12
"""
13
Get the base Airflow version as a tuple of integers.
14
15
Returns:
16
Tuple containing (major, minor, micro) version numbers
17
18
Example:
19
For Airflow 2.10.2, returns (2, 10, 2)
20
"""
21
22
# Version detection constants
23
AIRFLOW_V_3_0_PLUS: bool
24
"""True if running on Airflow 3.0.0 or higher"""
25
26
AIRFLOW_V_3_1_PLUS: bool
27
"""True if running on Airflow 3.1.0 or higher"""
28
```
29
30
### Compatible Base Classes
31
32
Provides the appropriate base classes based on the detected Airflow version, ensuring compatibility across different Airflow versions.
33
34
```python { .api }
35
# Conditional imports based on Airflow version
36
if AIRFLOW_V_3_1_PLUS:
37
from airflow.sdk import BaseHook
38
else:
39
from airflow.hooks.base import BaseHook
40
41
if AIRFLOW_V_3_0_PLUS:
42
from airflow.sdk import BaseOperator
43
else:
44
from airflow.models import BaseOperator
45
46
class BaseHook:
47
"""
48
Base hook class compatible across Airflow versions.
49
Imported from airflow.sdk (3.1+) or airflow.hooks.base (older versions).
50
"""
51
52
class BaseOperator:
53
"""
54
Base operator class compatible across Airflow versions.
55
Imported from airflow.sdk (3.0+) or airflow.models (older versions).
56
"""
57
```
58
59
## Usage Examples
60
61
### Version-Aware Code
62
63
```python
64
from airflow.providers.openai.version_compat import (
65
AIRFLOW_V_3_0_PLUS,
66
AIRFLOW_V_3_1_PLUS,
67
BaseHook,
68
BaseOperator,
69
get_base_airflow_version_tuple
70
)
71
72
# Check current Airflow version
73
major, minor, micro = get_base_airflow_version_tuple()
74
print(f"Running on Airflow {major}.{minor}.{micro}")
75
76
# Use version-specific features
77
if AIRFLOW_V_3_0_PLUS:
78
print("Using Airflow 3.0+ features")
79
# Use new SDK-based BaseOperator
80
81
if AIRFLOW_V_3_1_PLUS:
82
print("Using Airflow 3.1+ features")
83
# Use new SDK-based BaseHook
84
85
# Always use compatible base classes
86
class MyHook(BaseHook):
87
"""This will work on all supported Airflow versions"""
88
pass
89
90
class MyOperator(BaseOperator):
91
"""This will work on all supported Airflow versions"""
92
pass
93
```
94
95
### Import Pattern
96
97
```python
98
# This is the recommended way to import base classes in the OpenAI provider
99
from airflow.providers.openai.version_compat import BaseHook, BaseOperator
100
101
# Instead of directly importing from airflow (which may not be compatible)
102
# from airflow.hooks.base import BaseHook # Don't do this
103
# from airflow.models import BaseOperator # Don't do this
104
```
105
106
## Technical Details
107
108
### Version Detection Logic
109
110
The module uses packaging.version to parse the Airflow version string and create comparable version tuples. This ensures accurate version comparisons across different Airflow release patterns (stable, alpha, beta, rc).
111
112
### Import Strategy
113
114
The conditional imports follow a future-forward strategy:
115
- **Airflow 3.1+**: Uses the new SDK-based BaseHook from `airflow.sdk`
116
- **Airflow 3.0**: Uses SDK-based BaseOperator but legacy BaseHook from `airflow.hooks.base`
117
- **Airflow 2.x**: Uses legacy imports from `airflow.hooks.base` and `airflow.models`
118
119
This approach ensures the provider works seamlessly across the transition from Airflow 2.x to 3.x while taking advantage of new SDK features when available.
120
121
## Types
122
123
```python { .api }
124
# Version tuple type
125
VersionTuple = tuple[int, int, int]
126
127
# Version constants
128
AIRFLOW_V_3_0_PLUS: bool
129
AIRFLOW_V_3_1_PLUS: bool
130
131
# Compatible base classes (actual type depends on Airflow version)
132
BaseHook: type
133
BaseOperator: type
134
```