Skip to content

Commit bca9737

Browse files
authored
docs: add documentation for extension feature EncryptedSession (#1872)
1 parent 095496e commit bca9737

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# `Encrypt Session`
1+
# `EncryptedSession`
22

3-
::: agents.extensions.memory.encrypt_session
3+
::: agents.extensions.memory.encrypt_session.EncryptedSession

docs/sessions.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,57 @@ if __name__ == "__main__":
242242
asyncio.run(main())
243243
```
244244

245+
### Encrypted sessions
246+
247+
For applications requiring encryption of conversation data at rest, you can use `EncryptedSession` to wrap any session backend with transparent encryption and automatic TTL-based expiration. This requires the `encrypt` extra: `pip install openai-agents[encrypt]`.
248+
249+
The `EncryptedSession` uses Fernet encryption with per-session key derivation (HKDF) and supports automatic expiration of old messages. When items exceed the TTL, they are silently skipped during retrieval.
250+
251+
**Example: Encrypting SQLAlchemy session data**
252+
253+
```python
254+
import asyncio
255+
from agents import Agent, Runner
256+
from agents.extensions.memory import EncryptedSession, SQLAlchemySession
257+
258+
async def main():
259+
# Create underlying session (works with any SessionABC implementation)
260+
underlying_session = SQLAlchemySession.from_url(
261+
session_id="user-123",
262+
url="postgresql+asyncpg://app:secret@db.example.com/agents",
263+
create_tables=True,
264+
)
265+
266+
# Wrap with encryption and TTL-based expiration
267+
session = EncryptedSession(
268+
session_id="user-123",
269+
underlying_session=underlying_session,
270+
encryption_key="your-encryption-key", # Use a secure key from your secrets management
271+
ttl=600, # 10 minutes - items older than this are silently skipped
272+
)
273+
274+
agent = Agent("Assistant")
275+
result = await Runner.run(agent, "Hello", session=session)
276+
print(result.final_output)
277+
278+
if __name__ == "__main__":
279+
asyncio.run(main())
280+
```
281+
282+
**Key features:**
283+
284+
- **Transparent encryption**: Automatically encrypts all session items before storage and decrypts on retrieval
285+
- **Per-session key derivation**: Uses HKDF with the session ID as salt to derive unique encryption keys
286+
- **TTL-based expiration**: Automatically expires old messages based on configurable time-to-live (default: 10 minutes)
287+
- **Flexible key input**: Accepts either Fernet keys or raw strings as encryption keys
288+
- **Wraps any session**: Works with SQLite, SQLAlchemy, or custom session implementations
289+
290+
!!! warning "Important security notes"
291+
292+
- Store your encryption key securely (e.g., environment variables, secrets manager)
293+
- Expired tokens are rejected based on the application server's system clock - ensure all servers are time-synchronized with NTP to avoid valid tokens being rejected due to clock drift
294+
- The underlying session still stores encrypted data, so you maintain control over your database infrastructure
295+
245296

246297
## Custom memory implementations
247298

@@ -304,6 +355,7 @@ Use meaningful session IDs that help you organize conversations:
304355
- Use file-based SQLite (`SQLiteSession("session_id", "path/to/db.sqlite")`) for persistent conversations
305356
- Use SQLAlchemy-powered sessions (`SQLAlchemySession("session_id", engine=engine, create_tables=True)`) for production systems with existing databases supported by SQLAlchemy
306357
- Use OpenAI-hosted storage (`OpenAIConversationsSession()`) when you prefer to store history in the OpenAI Conversations API
358+
- Use encrypted sessions (`EncryptedSession(session_id, underlying_session, encryption_key)`) to wrap any session with transparent encryption and TTL-based expiration
307359
- Consider implementing custom session backends for other production systems (Redis, Django, etc.) for more advanced use cases
308360

309361
### Session management
@@ -402,3 +454,4 @@ For detailed API documentation, see:
402454
- [`SQLiteSession`][agents.memory.SQLiteSession] - SQLite implementation
403455
- [`OpenAIConversationsSession`](ref/memory/openai_conversations_session.md) - OpenAI Conversations API implementation
404456
- [`SQLAlchemySession`][agents.extensions.memory.sqlalchemy_session.SQLAlchemySession] - SQLAlchemy-powered implementation
457+
- [`EncryptedSession`][agents.extensions.memory.encrypt_session.EncryptedSession] - Encrypted session wrapper with TTL

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ plugins:
145145
- ref/extensions/handoff_prompt.md
146146
- ref/extensions/litellm.md
147147
- ref/extensions/memory/sqlalchemy_session.md
148+
- ref/extensions/memory/encrypt_session.md
148149

149150
- locale: ja
150151
name: 日本語

0 commit comments

Comments
 (0)