Fix GRU functional API model with return_state=True throwing KeyError #102791
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #102598
When using GRU (or other RNN layers) with return_state=True in a Functional API model, various KeyError exceptions were thrown during model compilation when only one output was used.
The issue occurred when a model had a single output from a multi-output layer (e.g., only sequences from GRU with return_state=True), but the loss/metrics were provided as a nested structure (list or tuple).
This fix adds handling in map_to_output_names() to unwrap single-element nested structures when the model has a single output, matching the expected structure for loss and metric routing.