keras-team/keras · error · ValueError
Must specify exactly two of left_padding, right_padding, tar
Error message
Must specify exactly two of left_padding, right_padding, target_width. Received: left_padding={left_padding}, right_padding={right_padding}, target_width={target_width} What it means
Horizontal counterpart of the pad_images triple rule: exactly two of left_padding, right_padding, target_width must be specified; otherwise the width is over- or under-determined and the validator raises.
Source
Thrown at keras/src/ops/image.py:1556
def _validate_pad_images_args(
top_padding,
left_padding,
bottom_padding,
right_padding,
target_height,
target_width,
):
if [top_padding, bottom_padding, target_height].count(None) != 1:
raise ValueError(
"Must specify exactly two of "
"top_padding, bottom_padding, target_height. "
f"Received: top_padding={top_padding}, "
f"bottom_padding={bottom_padding}, "
f"target_height={target_height}"
)
if [left_padding, right_padding, target_width].count(None) != 1:
raise ValueError(
"Must specify exactly two of "
"left_padding, right_padding, target_width. "
f"Received: left_padding={left_padding}, "
f"right_padding={right_padding}, "
f"target_width={target_width}"
)
_validate_non_negative(top_padding, "top_padding")
_validate_non_negative(bottom_padding, "bottom_padding")
_validate_non_negative(target_height, "target_height")
_validate_non_negative(left_padding, "left_padding")
_validate_non_negative(right_padding, "right_padding")
_validate_non_negative(target_width, "target_width")
def _validate_crop_images_args(
top_cropping,
left_cropping,View on GitHub (pinned to 7a34a03db6)
Solutions
- Give exactly two of the three: e.g. target_width=224, right_padding=4
- Symmetric case: left_padding=p, right_padding=p
- Check both axes independently — height passing does not imply width is valid
Example fix
# before out = pad_images(img, target_width=224) # after out = pad_images(img, target_width=224, right_padding=0)
Defensive patterns
Strategy: validation
Validate before calling
assert [left_padding, right_padding, target_width].count(None) == 1
Type guard
def width_triple_ok(l, r, tw) -> bool:
return [l, r, tw].count(None) == 1 Prevention
- Check width independently of height
- Lint config keys against the expected exactly-two rule
When it happens
Trigger: pad_images where the height triple is fine but width has 0, 1, or 3 specified values, e.g. only target_width=224 given.
Common situations: Asymmetric configs copied from a symmetric example; forgetting the second width argument when pad-to-target width; YAML keys typos (e.g. width_padding) silently leaving values None.
Related errors
- {name} must be >= 0. Received: {name}={value}
- Must specify exactly two of top_padding, bottom_padding, tar
- The `weights` argument should be either `None` (random initi
- `padding` should have two elements. Received: padding={paddi
- Invalid padding: {padding}
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/9652eccb10dbc100.
Report an issue: GitHub.