Skip to content

fix: explain shape mismatches and invalid params in model validation errors - #418

Merged
ivantha merged 1 commit into
c2siorg:mainfrom
rishiraj38:fix/146-explain-validation-errors
Oct 13, 2026
Merged

ivantha merged 1 commit into
c2siorg:mainfrom
rishiraj38:fix/146-explain-validation-errors

Conversation

@rishiraj38

Copy link
Copy Markdown
Contributor

Description

A layer that rejected its input surfaced the raw Keras ValueError (Keras layer name and ndim), a non-numeric layer parameter surfaced the bare int()/float() error with no node or parameter name (and None escaped as a TypeError, which the service reports as a 500), and an unknown node type gave no hint of what is supported.

  • app/shared/errors.py: layer_shape_mismatch_message() builds the message from the incoming tensor shape and the layer's input_spec (via _expected_input_rank()), not from the Keras exception text. It names the two nodes, the expected rank, the shape received and what to change: place the layer before any Flatten/Dense or give the Input a 3-D shape when the rank is too low, add a Flatten or Reshape when it is too high, and check kernel/pool/stride parameters against the shape for other failures.
  • app/services/model_generation.py: _build_layer() now constructs the layer and applies it through _apply_layer(), which re-raises a Keras ValueError with that message (original kept as __cause__). It takes the upstream source_ids, passed from the BFS, to name the feeding node. _coerce_param() replaces the bare int()/float() calls for units, filter, kernelX/Y, strideX/Y, pool_size, stride, rate and the input dim-* values, raising ValueError with the node id, parameter and value. The unknown node type error lists sorted(LAYER_REGISTRY).
  • app/generators/tensorflow_generator.py: build_model() wraps layer(input_tensor) and raises TensorFlowGeneratorError with the same message. The unknown layer type error names the node and lists sorted(LAYER_REGISTRY).
  • tests/test_model_generation.py: 14 tests in TestShapeMismatchMessages, TestParameterCoercionMessages and TestUnknownTypeMessages, covering Conv2D after Dense through both generators and model_validate_service, a layer fed more dimensions than it accepts, a kernel larger than the input, non-numeric units, filter=None, a non-numeric input dimension and dropout rate, the unknown type lists for both generators, and that Conv -> Flatten -> Dense and Conv -> Dense still build. A shared check asserts no ndim, Keras class name, path or traceback appears in any message.

Keras 3 applies Dense to the last axis, so Conv2D followed directly by Dense is a valid model and stays accepted; the mismatch users hit is a 4-D layer after a 2-D one. The service response shape ({"success": false, "message": ...}) and status codes are unchanged, so the frontend needs no change. Concatenate shape mismatches still use the existing err_msgs mapping.

Fixes #146

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

How Has This Been Tested?

  • uv run pytest tests/test_model_generation.py: 73 passed (14 new). With the app/ changes stashed, 12 of the new tests fail and the two valid-model guards pass.

  • uv run pytest tests/ --ignore=tests/test_cp1.py: 527 passed, 5 skipped; tests/test_cp1.py alone: 39 passed. Running both in one process aborts the interpreter on macOS at the same point on main (test_cp1.py drops tensorflow from sys.modules), unrelated to this change.

  • uv run ruff check . and uv run ruff format --check .: clean.

  • Called model_validate_service directly with Conv2D after Dense and with units: "abc": both return 400 with success: false and the new message.

  • Existing tests pass

  • New tests added

  • Manual testing

Screenshots (if applicable)

Not applicable, backend only.

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review
  • I have added/updated documentation as needed
  • My changes generate no new warnings
  • Tests pass locally

…errors

A layer that rejected its input surfaced the raw Keras ValueError (layer
name, ndim), a non-numeric parameter surfaced the bare int()/float()
error with no node or parameter name, and an unknown node type gave no
hint of what is supported.

Both generators now report a shape mismatch by naming the two nodes, the
expected rank, the incoming shape and what to change, derived from the
layer's input_spec rather than the Keras message. The legacy generator
coerces parameters through a helper that names the node, the parameter
and the value received. Unknown type errors list the registry's layer
types. The service response shape and status codes are unchanged.

Fixes c2siorg#146
@ivantha ivantha added enhancement New feature or request ready-for-maintainer Reviewed; looks mergeable, awaiting maintainer labels Oct 13, 2026
@ivantha

ivantha commented Oct 13, 2026

Copy link
Copy Markdown
Member

Automated review summary

Triage: enhancement | Recommendation: ready-for-maintainer

Findings

  • [minor/bug] tensormap-backend/app/services/model_generation.py:293 The legacy _build_layer unknown-type error lists sorted(LAYER_REGISTRY) keys (registry names such as dense or conv2d), but the legacy path only accepts customdense, customflatten, custommaxpool, customglobalavgpool, customconv and customdropout. The hint therefore suggests types this code path rejects. List the node types the legacy branch actually handles, or state which generator the list belongs to.
  • [minor/quality] tensormap-backend/app/shared/errors.py:53 Both catch sites treat every ValueError raised by layer(input_tensor) as a shape mismatch. A ValueError from an invalid parameter, such as a bad padding string, gets the generic 'Check the parameters ... (kernel size, pool size, strides)' text with the real cause hidden in cause. Narrowing the except, or appending str(e) for the fallback branch, would keep useful detail.
  • [nit/quality] tensormap-backend/app/generators/tensorflow_generator.py:152 The same try/except-and-reraise is written twice, once in _apply_layer in model_generation.py and once inline in TensorFlowGenerator.build_model. A shared helper next to layer_shape_mismatch_message would keep the two paths from drifting.
  • [nit/test] tensormap-backend/tests/test_model_generation.py:1004 Several tests assert exact Keras-derived shape text such as '(None, 32)' and '(None, 8, 8, 1)', and _assert_no_framework_internals forbids '/' in messages. These couple the tests to the Keras version's shape repr and could break on harmless message changes. The 'Check the parameters' branch is also hit only by the kernel-too-large case.

Conventions

  • PR title: follows Conventional Commits
  • Description: clear and complete
  • Commits (1): clean, conventional history
  • Clean hygiene overall: one conventional commit, a detailed description with 'Fixes [Feature] Explain shape mismatches and bad params in validation errors #146', and a thorough testing section. The documentation checkbox is unchecked, which is acceptable for a backend error-message change.

This PR turns raw Keras shape and parameter errors into messages that name the canvas nodes, the incoming shape and what to change. It covers both the legacy and registry generators, adds _coerce_param so int()/float() failures name the node and parameter, and has 14 focused tests including the model_validate_service 400 path. The design is sound: messages are built from input_spec and the tensor shape, and the Keras error stays as cause. Before merge, fix the legacy unknown-type message so it lists the customXxx types the legacy path accepts, not the registry names. Also consider narrowing the broad ValueError catch so non-shape errors keep their real cause visible.

Generated by /review-prs. A maintainer will follow up.

@ivantha
ivantha merged commit 1b215b2 into c2siorg:main Oct 13, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request ready-for-maintainer Reviewed; looks mergeable, awaiting maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Explain shape mismatches and bad params in validation errors

2 participants