Skip to content

fix(hf): reject a non-mapping architecture/training in model_card.yaml - #134

Merged
AlexanderFengler merged 1 commit into
mainfrom
fix/model-card-mapping-validation
Sep 5, 2026
Merged

fix(hf): reject a non-mapping architecture/training in model_card.yaml#134
AlexanderFengler merged 1 commit into
mainfrom
fix/model-card-mapping-validation

Conversation

@AlexanderFengler

@AlexanderFengler AlexanderFengler commented Sep 3, 2026

Copy link
Copy Markdown
Member

What

ModelCardConfig.architecture and .training are typed dict | None and read with .get() in generate_readme, but load_model_card_yaml accepted whatever the YAML held.

A list — architecture: [100, 100, 1], which reads naturally enough to write by hand — did two bad things at once:

  1. It was not None, so _fill_from_pickle_configs skipped the fallback that would have supplied the correct dict from *_network_config.pickle.
  2. It surfaced only at config.architecture.get("network_type") inside write_readme — as AttributeError: 'list' object has no attribute 'get', after the artifact upload had already started, leaving a half-populated staging dir and a partial remote commit.

Change

One helper, called on the two dict-typed fields at load time:

ValueError: /path/model_card.yaml: 'architecture' must be a mapping, got list.
Omit it to have LANfactory fill it in from the pickled configs, which are the
authoritative record of what was trained.

The message points at the right fix rather than just the wrong type — omitting the field is better than restating it, since a hand-written copy can drift from the pickle.

Scope

Deliberately narrow: only the two fields that crash. tags / usage_example are formatted, not traversed, so a wrong type there degrades a string instead of raising.

Testing

tests/hf/test_model_card.py — one parametrized case over both fields. Verified it fails on main (AttributeError path) and passes with the change. Full file: 23 passed; ruff check + format clean.

Found while publishing a gamma_drift_angle LAN to franklab/HSSM_staging; the publisher's dry run does not reach README generation, so nothing caught it before the upload.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Model card loading now clearly reports invalid architecture or training YAML values instead of failing later during README generation.
    • Invalid list-based values are rejected before fallback processing occurs.

`architecture` and `training` are typed `dict | None` and consumed with
`.get()` during README generation, but nothing validated the YAML. A list --
`architecture: [100, 100, 1]`, the plausible mistake -- survived load, and
because it was not `None` it silently suppressed the pickle fallback that
would have supplied the right shape. The failure then surfaced as
`AttributeError: 'list' object has no attribute 'get'` deep inside
`write_readme`, after the artifact upload had already begun.

Validate both fields at load time instead, and say in the message that
omitting them is the correct fix: the pickled configs are the authoritative
record of what was trained.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings September 3, 2026 03:00
@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 00ef081e-3117-44d1-a8e8-1b9d5a871457

📥 Commits

Reviewing files that changed from the base of the PR and between 35fdad9 and 6612265.

📒 Files selected for processing (2)
  • src/lanfactory/hf/model_card.py
  • tests/hf/test_model_card.py

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

load_model_card_yaml now validates architecture and training as mappings. Invalid values raise field-specific ValueError exceptions during loading. Parameterized tests cover invalid list values for both fields.

Changes

Model Card Validation

Layer / File(s) Summary
Validate model card mappings
src/lanfactory/hf/model_card.py, tests/hf/test_model_card.py
The loader validates architecture and training with _require_mapping, documents the new ValueError, and tests invalid list-valued fields.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 66122

Model-card YAML now fails early with a clear error when architecture or training is not a mapping, preventing later README-generation failures and partial uploads. The change is covered for both fields and is ready to merge.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: rejecting non-mapping architecture and training values in model_card.yaml.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/model-card-mapping-validation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The change is narrowly scoped, prevents a confirmed runtime failure mode, and includes focused test coverage for the new validation behavior.

Pull request overview

This PR hardens HuggingFace model card loading so invalid YAML shapes for architecture/training fail early (at load time) with a targeted error, preventing late AttributeError failures during README generation after an upload has already begun.

Changes:

  • Add _require_mapping(...) helper to validate that architecture and training are either mappings or omitted/null, otherwise raise a descriptive ValueError.
  • Use _require_mapping(...) inside load_model_card_yaml for the two dict-typed fields.
  • Add a parametrized pytest covering both keys to ensure non-mapping values are rejected at load time.
File summaries
File Description
src/lanfactory/hf/model_card.py Validates architecture/training types during YAML load to prevent late .get() crashes and preserve pickle fallback behavior.
tests/hf/test_model_card.py Adds a parametrized test asserting a ValueError is raised for non-mapping architecture/training values.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/lanfactory/hf/model_card.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AlexanderFengler
AlexanderFengler merged commit c2aeec5 into main Sep 5, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants