Repository navigation
fix: explain shape mismatches and invalid params in model validation errors - #418
Conversation
…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
Automated review summaryTriage: enhancement | Recommendation: ready-for-maintainer Findings
Conventions
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 |
Description
A layer that rejected its input surfaced the raw Keras
ValueError(Keras layer name andndim), a non-numeric layer parameter surfaced the bareint()/float()error with no node or parameter name (andNoneescaped as aTypeError, 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'sinput_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 KerasValueErrorwith that message (original kept as__cause__). It takes the upstreamsource_ids, passed from the BFS, to name the feeding node._coerce_param()replaces the bareint()/float()calls forunits,filter,kernelX/Y,strideX/Y,pool_size,stride,rateand the inputdim-*values, raisingValueErrorwith the node id, parameter and value. The unknown node type error listssorted(LAYER_REGISTRY).app/generators/tensorflow_generator.py:build_model()wrapslayer(input_tensor)and raisesTensorFlowGeneratorErrorwith the same message. The unknown layer type error names the node and listssorted(LAYER_REGISTRY).tests/test_model_generation.py: 14 tests inTestShapeMismatchMessages,TestParameterCoercionMessagesandTestUnknownTypeMessages, covering Conv2D after Dense through both generators andmodel_validate_service, a layer fed more dimensions than it accepts, a kernel larger than the input, non-numericunits,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 nondim, 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 existingerr_msgsmapping.Fixes #146
Type of Change
How Has This Been Tested?
uv run pytest tests/test_model_generation.py: 73 passed (14 new). With theapp/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.pyalone: 39 passed. Running both in one process aborts the interpreter on macOS at the same point onmain(test_cp1.pydropstensorflowfromsys.modules), unrelated to this change.uv run ruff check .anduv run ruff format --check .: clean.Called
model_validate_servicedirectly with Conv2D after Dense and withunits: "abc": both return 400 withsuccess: falseand the new message.Existing tests pass
New tests added
Manual testing
Screenshots (if applicable)
Not applicable, backend only.
Checklist