EnvConfigMixin.from_env raises an empty ValueError for absent optional fields
Bug Description
EnvConfigMixin.from_env in src/memos/mem_scheduler/general_modules/misc.py hits a bare raise ValueError() (no message) when a field declared as field: int | None = None has no environment variable set, making any config with plain optional fields impossible to build from the environment:
class RabbitMQConfig(EnvConfigMixin, BaseModel):
host: str = "localhost"
port: Optional[int] = None
RabbitMQConfig.from_env() # ValueError (empty message)
This is on the scheduler init path (memos/configs/mem_scheduler.py calls RabbitMQConfig.from_env()).
Secondary: _parse_env_value only parses when the annotation is exactly bool/int/float, so Optional[int] fields keep the raw string.
Expected Behavior
Absent optional fields should fall back to the model default, and Optional[int] env vars should parse as int.
Environment
MemOS main (a7367d0), python 3.14, pydantic v2.
Additional Context
Fix + red/green-verified tests ready; PR to follow referencing this issue.
🤖 AutoDev has picked up this issue and started working on it.
Task ID: 2ab67b9f19f05d28
Working branch: bugfix/autodev-2424-20260928140127315
Target branch: latest dev* branch
Workflow: opsp (analysis → coding → testing → PR)
I will post the PR link here once done. If I need more information, I will ask in the comments.
✅ AutoDev task 2ab67b9f19f05d28 completed.
Summary: Fixed EnvConfigMixin.from_env raising an empty ValueError for absent optional fields (issue #2424). The bug was in src/memos/mem_scheduler/general_modules/misc.py: the loop compared field_info.default is not None, which incorrectly treated a legitimate default=None (as on port: int | None = None) as "no default set" and fell through to raise ValueError(). Also fixed the secondary _parse_env_value bug where Optional[int] annotations kept env vars as raw strings because the type dispatch never unwrapped Union[T, None].
The fix reworks from_env so that required-and-missing fields raise a descriptive ValueError naming the env var, and non-required absent fields are simply omitted from the constructor kwargs — letting pydantic apply the declared default or default_factory uniformly for None, non-None, and factory-produced defaults. _parse_env_value now unwraps both legacy typing.Optional[T] and PEP 604 T | None before dispatching to the bool/int/float branches.
Added 5 regression tests in tests/mem_scheduler/test_config.py: absent optional-None field falls back to default, Optional[int] env var parses as int, required-field missing env var raises with a descriptive message, default_factory field uses the factory, and _parse_env_value unwrap works for both syntaxes. Verified the exact repro from the issue (RabbitMQConfig(EnvConfigMixin, BaseModel) with port: Optional[int] = None) now returns port=None instead of raising.
Test evidence: pytest tests/mem_scheduler/ → 71 passed, 1 skipped; ruff format and ruff check on both changed files → clean.
Base branch: main
Branch: bugfix/autodev-2424-20260928140127315
Commit: 8f4daf6
PR: https://github.com/MemTensor/MemOS/pull/2426
Assigned to: @syzsunshine219
Reviewers: @wustzdy
This issue has been automatically marked as stale due to inactivity.
This issue has been automatically closed due to inactivity.