fix: preserve agents-extras hint when memory backends are missing#3388
Closed
ioleksiuk wants to merge 1 commit into
Closed
fix: preserve agents-extras hint when memory backends are missing#3388ioleksiuk wants to merge 1 commit into
ioleksiuk wants to merge 1 commit into
Conversation
`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.
Member
|
Thanks for sharing this feedback. We'll improve it this way: #3389 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
```
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
Issue number
N/A — found while auditing the lazy-import pattern from #3048 for analogous gaps.
Checks