Skip to content

Add safe hparams passthrough to RunMeta#10

Closed
blizhan wants to merge 1 commit into
mainfrom
codex/extend-runmeta-with-params-field
Closed

Add safe hparams passthrough to RunMeta#10
blizhan wants to merge 1 commit into
mainfrom
codex/extend-runmeta-with-params-field

Conversation

@blizhan

@blizhan blizhan commented Apr 24, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Expose run hyperparameters in query outputs by extending run metadata so downstream metrics and image consumers can access hparams without mutating the Aim repo.
  • Make conversion robust so non-native mapping objects returned by run.hparams do not break queries.
  • Ensure both metric and image collection paths carry the new metadata field consistently.

Description

  • Extended RunMeta with params: dict[str, Any] | None and populated it from run.hparams in _extract_run_meta (file: src/aimx/aim_bridge/metric_stats.py).
  • Added _as_plain_dict() to perform best-effort conversion of mapping-like objects by checking for dict, calling to_dict() when available, then falling back to dict(...), and returning None on any conversion failure to avoid query errors.
  • Left the field as a read-only passthrough (no writes back to the Aim repository) and relied on _extract_run_meta being used by both collect_metric_series() and collect_image_series() so both paths include params.
  • Updated unit tests in tests/unit/test_metric_stats.py to cover native dict passthrough, to_dict() conversion, and graceful fallback when conversion raises.

Testing

  • Ran unit tests with uv run pytest tests/unit/test_metric_stats.py and all tests passed (21 passed).

Codex Task

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the extraction of hyperparameters (hparams) into the RunMeta dataclass, supported by a new helper function _as_plain_dict that converts various object types into plain dictionaries. Unit tests have been added to verify the extraction logic for different input types and error scenarios. Feedback was provided to simplify the _as_plain_dict function by removing redundant type checks and flattening the conversion logic for better readability.

Comment on lines +80 to +101
if value is None:
return None
if isinstance(value, dict):
return dict(value)

to_dict = getattr(value, "to_dict", None)
if callable(to_dict):
try:
converted = to_dict()
except Exception: # noqa: BLE001
return None
if isinstance(converted, dict):
return dict(converted)
try:
return dict(converted)
except Exception: # noqa: BLE001
return None

try:
return dict(value)
except Exception: # noqa: BLE001
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The _as_plain_dict function contains redundant logic and can be simplified while maintaining the same behavior. Specifically, the check if isinstance(converted, dict) followed by dict(converted) is redundant because dict() can be safely called on a dictionary. The logic can be flattened to improve readability.

Suggested change
if value is None:
return None
if isinstance(value, dict):
return dict(value)
to_dict = getattr(value, "to_dict", None)
if callable(to_dict):
try:
converted = to_dict()
except Exception: # noqa: BLE001
return None
if isinstance(converted, dict):
return dict(converted)
try:
return dict(converted)
except Exception: # noqa: BLE001
return None
try:
return dict(value)
except Exception: # noqa: BLE001
return None
if value is None:
return None
# If it's not already a dict, try to use to_dict() if available
if not isinstance(value, dict):
to_dict = getattr(value, "to_dict", None)
if callable(to_dict):
try:
value = to_dict()
except Exception: # noqa: BLE001
return None
# Final attempt to cast to a plain dict (handles dicts, mappings, or iterables of pairs)
try:
return dict(value)
except Exception: # noqa: BLE001
return None

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex 他review合理吗

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex Review: Didn't find any major issues. Breezy!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@blizhan blizhan closed this Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant