Skip to content

EnvConfigMixin.from_env raises an empty ValueError for absent optional fields #2424

Closed as not planned

Description

@simpleqt

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.

Activity

  1. added
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    status:in-progressSomeone or AI is working on it | 人工或 AI 正在处理
    types:bugSomething isn't working | 功能异常
    on Sep 28, 2026
  2. self-assigned this
    on Sep 28, 2026
  3. Memtensor-AI commented on Sep 28, 2026

    @Memtensor-AI
    Collaborator

    🤖 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.

  4. Memtensor-AI commented on Sep 28, 2026

    @Memtensor-AI
    Collaborator

    ✅ 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: #2426
    Assigned to: @syzsunshine219
    Reviewers: @wustzdy

  5. added
    ai:testingAI agent is running tests | AI 正在运行测试
    ai:pr-readyAI-created PR is ready for review | AI 生成的 PR 已等待评审
    and removed
    ai:codingAI agent is coding | AI 正在编码
    ai:testingAI agent is running tests | AI 正在运行测试
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    on Sep 28, 2026
  6. github-actions commented on Oct 31, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale due to inactivity.

  7. added
    status:staleAuto-marked stale after 30d inactivity; closes 7d later unless exempt.
    on Oct 31, 2026
  8. github-actions commented on Nov 8, 2026

    @github-actions
    Contributor

    This issue has been automatically closed due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

ai:pr-readyAI-created PR is ready for review | AI 生成的 PR 已等待评审area:scheduler调度模块status:in-progressSomeone or AI is working on it | 人工或 AI 正在处理status:staleAuto-marked stale after 30d inactivity; closes 7d later unless exempt.types:bugSomething isn't working | 功能异常

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions