Skip to content

Commit ae024e2

Browse files
authored
Deprecating openai assistant agent. Apply version conditioned import for open ai version < 1.83 (#6827)
1 parent 413d8f1 commit ae024e2

4 files changed

Lines changed: 102 additions & 8 deletions

File tree

python/packages/autogen-ext/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ exclude = ["src/autogen_ext/agents/web_surfer/*.js", "src/autogen_ext/runtimes/g
175175
[tool.pyright]
176176
extends = "../../pyproject.toml"
177177
include = ["src", "tests"]
178-
exclude = ["src/autogen_ext/runtimes/grpc/protos", "tests/protos"]
178+
exclude = ["src/autogen_ext/runtimes/grpc/protos", "tests/protos", "src/autogen_ext/agents/openai/_openai_assistant_agent.py", "tests/test_openai_assistant_agent.py"]
179179

180180
[tool.pytest.ini_options]
181181
minversion = "6.0"
@@ -195,7 +195,7 @@ test.sequence = [
195195
test.default_item_type = "cmd"
196196
test-grpc = "pytest -n 1 --cov=src --cov-report=term-missing --cov-report=xml --grpc"
197197
test-windows = "pytest -n 1 --cov=src --cov-report=term-missing --cov-report=xml -m 'windows'"
198-
mypy = "mypy --config-file ../../pyproject.toml --exclude src/autogen_ext/runtimes/grpc/protos --exclude tests/protos src tests"
198+
mypy = "mypy --config-file ../../pyproject.toml --exclude src/autogen_ext/runtimes/grpc/protos --exclude tests/protos --exclude src/autogen_ext/agents/openai/_openai_assistant_agent.py --exclude tests/test_openai_assistant_agent.py --ignore-missing-imports src tests"
199199

200200
[tool.mypy]
201201
[[tool.mypy.overrides]]

python/packages/autogen-ext/src/autogen_ext/agents/openai/__init__.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
try:
22
from ._openai_agent import OpenAIAgent
3-
from ._openai_assistant_agent import OpenAIAssistantAgent
3+
4+
# Check OpenAI version to conditionally import OpenAIAssistantAgent
5+
try:
6+
from openai import __version__ as openai_version
7+
8+
def _parse_openai_version(version_str: str) -> tuple[int, int, int]:
9+
"""Parse a semantic version string into a tuple of integers."""
10+
try:
11+
parts = version_str.split(".")
12+
major = int(parts[0])
13+
minor = int(parts[1]) if len(parts) > 1 else 0
14+
patch = int(parts[2].split("-")[0]) if len(parts) > 2 else 0 # Handle pre-release versions
15+
return (major, minor, patch)
16+
except (ValueError, IndexError):
17+
# If version parsing fails, assume it's a newer version
18+
return (999, 999, 999)
19+
20+
_current_version = _parse_openai_version(openai_version)
21+
_target_version = (1, 83, 0)
22+
23+
# Only import OpenAIAssistantAgent if OpenAI version is less than 1.83
24+
if _current_version < _target_version:
25+
from ._openai_assistant_agent import OpenAIAssistantAgent # type: ignore[import]
26+
27+
__all__ = ["OpenAIAssistantAgent", "OpenAIAgent"]
28+
else:
29+
__all__ = ["OpenAIAgent"]
30+
except ImportError:
31+
# If OpenAI is not available, skip OpenAIAssistantAgent import
32+
__all__ = ["OpenAIAgent"]
33+
434
except ImportError as e:
535
raise ImportError(
636
"Dependencies for OpenAI agents not found. "
737
'Please install autogen-ext with the "openai" extra: '
838
'pip install "autogen-ext[openai]"'
939
) from e
10-
11-
__all__ = ["OpenAIAssistantAgent", "OpenAIAgent"]

python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_assistant_agent.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
"""
2+
OpenAI Assistant Agent implementation.
3+
4+
This module is deprecated starting v0.7.0 and will be removed in a future version.
5+
"""
6+
# pyright: ignore
7+
# mypy: ignore-errors
8+
19
import asyncio
210
import json
311
import logging
412
import os
13+
import warnings
14+
from functools import wraps
515
from typing import (
616
Any,
717
AsyncGenerator,
@@ -57,9 +67,33 @@
5767
from openai.types.shared_params.function_definition import FunctionDefinition
5868
from openai.types.vector_store import VectorStore
5969

70+
# Deprecation warning
71+
warnings.warn(
72+
"The OpenAIAssistantAgent module is deprecated and will be removed in a future version, use OpenAIAgent instead.",
73+
DeprecationWarning,
74+
stacklevel=2,
75+
)
76+
6077
event_logger = logging.getLogger(EVENT_LOGGER_NAME)
6178

6279

80+
def deprecated_class(reason: str) -> Callable[[type], type]:
81+
"""Decorator to mark a class as deprecated."""
82+
83+
def decorator(cls: type) -> type:
84+
original_init = cls.__init__
85+
86+
@wraps(original_init)
87+
def new_init(self, *args, **kwargs) -> None:
88+
warnings.warn(f"{cls.__name__} is deprecated: {reason}", DeprecationWarning, stacklevel=2)
89+
original_init(self, *args, **kwargs)
90+
91+
cls.__init__ = new_init
92+
return cls
93+
94+
return decorator
95+
96+
6397
def _convert_tool_to_function_param(tool: Tool) -> "FunctionToolParam":
6498
"""Convert an autogen Tool to an OpenAI Assistant function tool parameter."""
6599

@@ -90,14 +124,22 @@ class OpenAIAssistantAgentState(BaseModel):
90124
uploaded_file_ids: List[str] = Field(default_factory=list)
91125

92126

127+
@deprecated_class(
128+
"This class is deprecated starting v0.7.0 and will be removed in a future version. Use OpenAIAgent instead."
129+
)
93130
class OpenAIAssistantAgent(BaseChatAgent):
94131
"""An agent implementation that uses the Assistant API to generate responses.
95132
133+
.. warning::
134+
135+
This module is deprecated starting v0.7.0 and will be removed in a future version.
136+
Please use :class:`~autogen_ext.agents.openai.OpenAIAgent` instead.
137+
96138
Installation:
97139
98140
.. code-block:: bash
99141
100-
pip install "autogen-ext[openai]"
142+
pip install "autogen-ext[openai]" # For OpenAI Assistant
101143
# pip install "autogen-ext[openai,azure]" # For Azure OpenAI Assistant
102144
103145
@@ -146,7 +188,7 @@ async def example():
146188
147189
# Create an assistant with code interpreter
148190
assistant = OpenAIAssistantAgent(
149-
name="Python Helper",
191+
name="PythonHelper",
150192
description="Helps with Python programming",
151193
client=client,
152194
model="gpt-4",
@@ -197,7 +239,7 @@ async def example():
197239
198240
# Create an assistant with code interpreter
199241
assistant = OpenAIAssistantAgent(
200-
name="Python Helper",
242+
name="PythonHelper",
201243
description="Helps with Python programming",
202244
client=client,
203245
model="gpt-4o",

python/packages/autogen-ext/tests/test_openai_assistant_agent.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,33 @@
1414
from autogen_ext.agents.openai import OpenAIAssistantAgent
1515
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
1616
from openai import AsyncAzureOpenAI, AsyncOpenAI
17+
from openai import __version__ as openai_version
1718
from pydantic import BaseModel
1819

1920

21+
def _parse_openai_version(version_str: str) -> tuple[int, int, int]:
22+
"""Parse a semantic version string into a tuple of integers."""
23+
try:
24+
parts = version_str.split(".")
25+
major = int(parts[0])
26+
minor = int(parts[1]) if len(parts) > 1 else 0
27+
patch = int(parts[2].split("-")[0]) if len(parts) > 2 else 0 # Handle pre-release versions
28+
return (major, minor, patch)
29+
except (ValueError, IndexError):
30+
# If version parsing fails, assume it's a newer version
31+
return (999, 999, 999)
32+
33+
34+
# Skip all tests if OpenAI version is less than 1.83
35+
_current_version = _parse_openai_version(openai_version)
36+
_target_version = (1, 83, 0)
37+
if _current_version < _target_version:
38+
pytest.skip(
39+
f"OpenAI version {openai_version} is less than 1.83. OpenAIAssistantAgent tests are skipped for older versions.",
40+
allow_module_level=True,
41+
)
42+
43+
2044
class QuestionType(str, Enum):
2145
MULTIPLE_CHOICE = "MULTIPLE_CHOICE"
2246
FREE_RESPONSE = "FREE_RESPONSE"

0 commit comments

Comments
 (0)