Skip to content

fix: allow Constraints.set_monitoring to disable monitoring for non-string features (v2) - #6325

Merged
mohamedzeidan2021 merged 1 commit into
aws:master-v2from
mohamedzeidan2021:fix/issue-2745-set-monitoring-non-string-v2
Sep 28, 2026
Merged

mohamedzeidan2021 merged 1 commit into
aws:master-v2from
mohamedzeidan2021:fix/issue-2745-set-monitoring-non-string-v2

Conversation

@mohamedzeidan2021

Copy link
Copy Markdown
Collaborator

Issue

Fixes #2745 (v2 backport — companion to #6312 which fixes the same bug in v3/sagemaker-core).

sagemaker.model_monitor.Constraints.set_monitoring(enable_monitoring, feature_name=...) only worked for string-type features. For a non-string feature (e.g. binary/Integral like Churn) it raised KeyError: 'string_constraints', and even for string features it nested the override one level too deep — inside string_constraints rather than at the feature level, contrary to the Model Monitor constraints.json schema.

Fix

Per the constraints.json schema, monitoring_config_overrides is a feature-level key (a sibling of name, inferred_type, and the type-specific num_constraints/string_constraints blocks). The method now reads/writes monitoring_config_overrides directly on the feature dict, so it works for any feature type and places the override where the schema (and the docs) say it belongs. The top-level (feature_name=None) path is unchanged.

Before:

string_constraints = feature["string_constraints"]        # KeyError for non-string features
string_constraints.setdefault("monitoring_config_overrides", {})["evaluate_constraints"] = flag

After:

if feature.get("monitoring_config_overrides") is None:
    feature["monitoring_config_overrides"] = {}
feature["monitoring_config_overrides"]["evaluate_constraints"] = flag

Testing

Added tests/unit/sagemaker/monitor/test_monitoring_files.py (7 tests): non-string feature (the reported case), string feature, enable/disable mapping, preservation of existing overrides, and the top-level no-feature_name path. Verified they fail against the unpatched source and pass against the fix. black and flake8 clean.

Backwards compatibility

No public signature/return/exception change. No other readers of monitoring_config_overrides / string_constraints and no other callers of set_monitoring exist in the codebase. Output for string features moves the override from inside string_constraints to the feature level — this corrects the reported bug and matches the documented schema.

…tring features

Per the constraints.json schema, monitoring_config_overrides is a
feature-level key (a sibling of name, inferred_type, and the type-specific
num_constraints/string_constraints blocks). set_monitoring previously read
and wrote it inside feature["string_constraints"], which raised
KeyError: 'string_constraints' for any non-string feature and, even for
string features, nested the override one level too deep. Read/write the
override directly on the feature dict so it works for any feature type and
matches the documented schema.

Adds tests/unit/sagemaker/monitor/test_monitoring_files.py.

Fixes aws#2745
@mohamedzeidan2021
mohamedzeidan2021 merged commit ce118d2 into aws:master-v2 Sep 28, 2026
9 of 11 checks passed

This branch was successfully deployed

1 active deployment
auto-approve — 761484a6 Deployed Sep 25, 2026 by mohamedzeidan2021 via wait-for-approval #229
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