Skip to content

fix: preserve agents-extras hint when memory backends are missing#3388

Closed
ioleksiuk wants to merge 1 commit into
openai:mainfrom
ioleksiuk:fix/preserve-memory-extras-error-message
Closed

fix: preserve agents-extras hint when memory backends are missing#3388
ioleksiuk wants to merge 1 commit into
openai:mainfrom
ioleksiuk:fix/preserve-memory-extras-error-message

Conversation

@ioleksiuk
Copy link
Copy Markdown
Contributor

Summary

`src/agents/extensions/memory/init.py` wraps each lazy import so the user gets a helpful `pip install openai-agents[]` message when an extra is missing. The outer wrappers caught `ModuleNotFoundError`, but `redis_session.py`, `dapr_session.py`, and `mongodb_session.py` each have their own `try/except ImportError` that re-raises a plain `ImportError` (not `ModuleNotFoundError`).

Python's class hierarchy: `ModuleNotFoundError` is a subclass of `ImportError`. So `except ModuleNotFoundError` does NOT catch a bare `ImportError` re-raised by the inner wrapper. The intended outer hint was therefore unreachable for the three backends with their own `try/except`, and users saw the less helpful inner message.

Concretely (reproduced against current main):

```

from agents.extensions.memory import RedisSession # redis missing
ImportError: RedisSession requires the 'redis' package. Install it with: pip install redis
```

Expected:

```
ImportError: RedisSession requires the 'redis' extra. Install it with: pip install openai-agents[redis]
```

Widen the outer `except` clauses from `ModuleNotFoundError` to `ImportError`. Since `ModuleNotFoundError` is a subclass of `ImportError`, this only adds coverage; it doesn't change behavior for the backends that already raised `ModuleNotFoundError` directly.

Test plan

  • New `tests/extensions/memory/test_memory_imports.py` parametrized over every extras-backed export (`RedisSession`, `DaprSession`, `DAPR_CONSISTENCY_EVENTUAL`, `DAPR_CONSISTENCY_STRONG`, `MongoDBSession`, `EncryptedSession`, `SQLAlchemySession`) and both `ImportError`/`ModuleNotFoundError` subclasses — 14 cases, all passing.
  • `uv run pytest tests/extensions/memory/test_memory_imports.py tests/mcp/test_mcp_imports.py` — 16 passed.
  • `uv run ruff check` on the touched files — All checks passed.

Issue number

N/A — found while auditing the lazy-import pattern from #3048 for analogous gaps.

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation (no doc changes needed)
  • I've run `make lint` and `make format`
  • I've made sure tests pass

`src/agents/extensions/memory/__init__.py` wraps each lazy import so the
user gets a `pip install openai-agents[<extra>]` message when an extra
is missing. The wrappers caught `ModuleNotFoundError`, but
`redis_session.py`, `dapr_session.py`, and `mongodb_session.py` each
have their own `try/except ImportError` that re-raises a plain
`ImportError` (not `ModuleNotFoundError`). `ImportError` is the
superclass, so the outer `except ModuleNotFoundError` clause never
matched and the helpful hint was effectively dead code.

Concretely, `from agents.extensions.memory import RedisSession` with
`redis` missing produced `"RedisSession requires the 'redis' package.
Install it with: pip install redis"` (the inner wrapper) instead of
`"RedisSession requires the 'redis' extra. Install it with: pip install
openai-agents[redis]"` (the outer wrapper).

Widen the outer `except` clauses to `ImportError` so they catch both
`ImportError` and `ModuleNotFoundError`. Add a parametrized regression
test covering every extras-backed export plus both error subclasses.
@seratch
Copy link
Copy Markdown
Member

seratch commented May 13, 2026

Thanks for sharing this feedback. We'll improve it this way: #3389

@seratch seratch closed this May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants