Skip to content

Commit 67fb85a

Browse files
authored
feat(extensions): add MongoDB session backend (openai#2902)
1 parent f84ef7f commit 67fb85a

File tree

5 files changed

+1237
-1
lines changed

5 files changed

+1237
-1
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ sqlalchemy = ["SQLAlchemy>=2.0", "asyncpg>=0.29.0"]
4444
encrypt = ["cryptography>=45.0, <46"]
4545
redis = ["redis>=7"]
4646
dapr = ["dapr>=1.16.0", "grpcio>=1.60.0"]
47+
mongodb = ["pymongo>=4.14"]
4748
docker = ["docker>=6.1"]
4849
blaxel = ["blaxel>=0.2.50", "aiohttp>=3.12,<4"]
4950
daytona = ["daytona>=0.155.0"]
@@ -90,6 +91,7 @@ dev = [
9091
"grpcio>=1.60.0",
9192
"testcontainers==4.12.0", # pinned to 4.12.0 because 4.13.0 has a warning bug in wait_for_logs, see https://github.com/testcontainers/testcontainers-python/issues/874
9293
"pyright==1.1.408",
94+
"pymongo>=4.14",
9395
]
9496

9597
[tool.uv.workspace]

src/agents/extensions/memory/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
DaprSession,
2020
)
2121
from .encrypt_session import EncryptedSession
22+
from .mongodb_session import MongoDBSession
2223
from .redis_session import RedisSession
2324
from .sqlalchemy_session import SQLAlchemySession
2425

@@ -29,6 +30,7 @@
2930
"DAPR_CONSISTENCY_STRONG",
3031
"DaprSession",
3132
"EncryptedSession",
33+
"MongoDBSession",
3234
"RedisSession",
3335
"SQLAlchemySession",
3436
]
@@ -117,4 +119,15 @@ def __getattr__(name: str) -> Any:
117119
"Install it with: pip install openai-agents[dapr]"
118120
) from e
119121

122+
if name == "MongoDBSession":
123+
try:
124+
from .mongodb_session import MongoDBSession # noqa: F401
125+
126+
return MongoDBSession
127+
except ModuleNotFoundError as e:
128+
raise ImportError(
129+
"MongoDBSession requires the 'mongodb' extra. "
130+
"Install it with: pip install openai-agents[mongodb]"
131+
) from e
132+
120133
raise AttributeError(f"module {__name__} has no attribute {name}")

0 commit comments

Comments
 (0)