Skip to content

Commit 1c99128

Browse files
committed
make consistency constants part of public interface again
Signed-off-by: Filinto Duran <[email protected]>
1 parent 40a65dc commit 1c99128

File tree

7 files changed

+49
-29
lines changed

7 files changed

+49
-29
lines changed

docs/sessions/dapr_session.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ Consistency levels control how Dapr handles read/write operations across distrib
244244
Use the provided constants to avoid typos:
245245

246246
```python
247-
from agents.extensions.memory.dapr_session import (
248-
CONSISTENCY_EVENTUAL,
249-
CONSISTENCY_STRONG,
247+
from agents.extensions.memory import (
248+
DAPR_CONSISTENCY_EVENTUAL,
249+
DAPR_CONSISTENCY_STRONG,
250250
DaprSession,
251251
)
252252

@@ -255,19 +255,19 @@ session = DaprSession.from_address(
255255
session_id="user-123",
256256
state_store_name="statestore",
257257
dapr_address="localhost:50001",
258-
consistency=CONSISTENCY_EVENTUAL, # or "eventual"
258+
consistency=DAPR_CONSISTENCY_EVENTUAL, # or "eventual"
259259
)
260260

261261
# Strong consistency (guarantees read-after-write consistency)
262262
session = DaprSession.from_address(
263263
session_id="user-123",
264264
state_store_name="statestore",
265265
dapr_address="localhost:50001",
266-
consistency=CONSISTENCY_STRONG, # or "strong"
266+
consistency=DAPR_CONSISTENCY_STRONG, # or "strong"
267267
)
268268
```
269269

270-
**Important**: Consistency levels apply to both read and write operations. When using `CONSISTENCY_STRONG`, the session ensures that reads always reflect the most recent writes, preventing stale data after updates.
270+
**Important**: Consistency levels apply to both read and write operations. When using `DAPR_CONSISTENCY_STRONG`, the session ensures that reads always reflect the most recent writes, preventing stale data after updates.
271271

272272
Support varies by state store. See [Dapr consistency documentation](https://docs.dapr.io/developing-applications/building-blocks/state-management/state-management-overview/#consistency).
273273

docs/sessions/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ session = DaprSession.from_address(
211211
)
212212

213213
# Optional: configure TTL and consistency
214-
from agents.extensions.memory import CONSISTENCY_STRONG
214+
from agents.extensions.memory import DAPR_CONSISTENCY_STRONG
215215

216216
session = DaprSession.from_address(
217217
"user_123",
218218
state_store_name="statestore",
219219
dapr_address="localhost:50001", # The default Dapr gRPC port is 50001 and can be omitted.
220220
ttl=3600, # 1 hour
221-
consistency=CONSISTENCY_STRONG, # or CONSISTENCY_EVENTUAL
221+
consistency=DAPR_CONSISTENCY_STRONG, # or DAPR_CONSISTENCY_EVENTUAL
222222
)
223223
```
224224

examples/memory/dapr_session_example.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
from agents import Agent, Runner
2121
from agents.extensions.memory import (
22-
CONSISTENCY_EVENTUAL,
23-
CONSISTENCY_STRONG,
22+
DAPR_CONSISTENCY_EVENTUAL,
23+
DAPR_CONSISTENCY_STRONG,
2424
DaprSession,
2525
)
2626

@@ -183,15 +183,15 @@ async def demonstrate_advanced_features():
183183
"eventual_session",
184184
state_store_name="statestore",
185185
dapr_address="localhost:50001",
186-
consistency=CONSISTENCY_EVENTUAL,
186+
consistency=DAPR_CONSISTENCY_EVENTUAL,
187187
)
188188

189189
# Strong consistency (guaranteed read-after-write)
190190
strong_session = DaprSession.from_address(
191191
"strong_session",
192192
state_store_name="statestore",
193193
dapr_address="localhost:50001",
194-
consistency=CONSISTENCY_STRONG,
194+
consistency=DAPR_CONSISTENCY_STRONG,
195195
)
196196

197197
if await eventual_session.ping():

src/agents/extensions/memory/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
__all__: list[str] = [
1414
"AdvancedSQLiteSession",
15+
"DAPR_CONSISTENCY_EVENTUAL",
16+
"DAPR_CONSISTENCY_STRONG",
1517
"DaprSession",
1618
"EncryptedSession",
1719
"RedisSession",
@@ -72,4 +74,26 @@ def __getattr__(name: str) -> Any:
7274
"Install it with: pip install openai-agents[dapr]"
7375
) from e
7476

77+
if name == "DAPR_CONSISTENCY_EVENTUAL":
78+
try:
79+
from .dapr_session import DAPR_CONSISTENCY_EVENTUAL # noqa: F401
80+
81+
return DAPR_CONSISTENCY_EVENTUAL
82+
except ModuleNotFoundError as e:
83+
raise ImportError(
84+
"DAPR_CONSISTENCY_EVENTUAL requires the 'dapr' extra. "
85+
"Install it with: pip install openai-agents[dapr]"
86+
) from e
87+
88+
if name == "DAPR_CONSISTENCY_STRONG":
89+
try:
90+
from .dapr_session import DAPR_CONSISTENCY_STRONG # noqa: F401
91+
92+
return DAPR_CONSISTENCY_STRONG
93+
except ModuleNotFoundError as e:
94+
raise ImportError(
95+
"DAPR_CONSISTENCY_STRONG requires the 'dapr' extra. "
96+
"Install it with: pip install openai-agents[dapr]"
97+
) from e
98+
7599
raise AttributeError(f"module {__name__} has no attribute {name}")

src/agents/extensions/memory/dapr_session.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
ConsistencyLevel = Literal["eventual", "strong"]
4444

4545
# Consistency level constants
46-
CONSISTENCY_EVENTUAL: ConsistencyLevel = "eventual"
47-
CONSISTENCY_STRONG: ConsistencyLevel = "strong"
46+
DAPR_CONSISTENCY_EVENTUAL: ConsistencyLevel = "eventual"
47+
DAPR_CONSISTENCY_STRONG: ConsistencyLevel = "strong"
4848

4949

5050
class DaprSession(SessionABC):
@@ -57,7 +57,7 @@ def __init__(
5757
state_store_name: str,
5858
dapr_client: DaprClient,
5959
ttl: int | None = None,
60-
consistency: ConsistencyLevel = CONSISTENCY_EVENTUAL,
60+
consistency: ConsistencyLevel = DAPR_CONSISTENCY_EVENTUAL,
6161
):
6262
"""Initializes a new DaprSession.
6363
@@ -125,9 +125,9 @@ def _get_read_metadata(self) -> dict[str, str]:
125125

126126
def _get_state_options(self) -> StateOptions | None:
127127
"""Get StateOptions for write/delete consistency level."""
128-
if self._consistency == CONSISTENCY_STRONG:
128+
if self._consistency == DAPR_CONSISTENCY_STRONG:
129129
return StateOptions(consistency=Consistency.strong)
130-
elif self._consistency == CONSISTENCY_EVENTUAL:
130+
elif self._consistency == DAPR_CONSISTENCY_EVENTUAL:
131131
return StateOptions(consistency=Consistency.eventual)
132132
return None
133133

tests/extensions/memory/test_dapr_redis_integration.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626

2727
from agents import Agent, Runner, TResponseInputItem
2828
from agents.extensions.memory import (
29+
DAPR_CONSISTENCY_EVENTUAL,
30+
DAPR_CONSISTENCY_STRONG,
2931
DaprSession,
3032
)
31-
from agents.extensions.memory.dapr_session import (
32-
CONSISTENCY_EVENTUAL,
33-
CONSISTENCY_STRONG,
34-
)
3533
from tests.fake_model import FakeModel
3634
from tests.test_responses import get_text_message
3735

@@ -386,15 +384,15 @@ async def test_dapr_consistency_levels(dapr_container, monkeypatch):
386384
session_id="eventual_consistency_test",
387385
state_store_name="statestore",
388386
dapr_address=dapr_address,
389-
consistency=CONSISTENCY_EVENTUAL,
387+
consistency=DAPR_CONSISTENCY_EVENTUAL,
390388
)
391389

392390
# Test strong consistency
393391
session_strong = DaprSession.from_address(
394392
session_id="strong_consistency_test",
395393
state_store_name="statestore",
396394
dapr_address=dapr_address,
397-
consistency=CONSISTENCY_STRONG,
395+
consistency=DAPR_CONSISTENCY_STRONG,
398396
)
399397

400398
try:

tests/extensions/memory/test_dapr_session.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010

1111
from agents import Agent, Runner, TResponseInputItem
1212
from agents.extensions.memory import (
13+
DAPR_CONSISTENCY_EVENTUAL,
14+
DAPR_CONSISTENCY_STRONG,
1315
DaprSession,
1416
)
15-
from agents.extensions.memory.dapr_session import (
16-
CONSISTENCY_EVENTUAL,
17-
CONSISTENCY_STRONG,
18-
)
1917
from tests.fake_model import FakeModel
2018
from tests.test_responses import get_text_message
2119

@@ -455,15 +453,15 @@ async def test_consistency_levels(fake_dapr_client: FakeDaprClient):
455453
session_id="eventual_test",
456454
state_store_name="statestore",
457455
dapr_client=fake_dapr_client,
458-
consistency=CONSISTENCY_EVENTUAL,
456+
consistency=DAPR_CONSISTENCY_EVENTUAL,
459457
)
460458

461459
# Test strong consistency
462460
session_strong = DaprSession(
463461
session_id="strong_test",
464462
state_store_name="statestore",
465463
dapr_client=fake_dapr_client,
466-
consistency=CONSISTENCY_STRONG,
464+
consistency=DAPR_CONSISTENCY_STRONG,
467465
)
468466

469467
try:

0 commit comments

Comments
 (0)