Python
Error Handling
Debugging
Programming
Data Science

ValueError Feature not in features dictionary

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

ValueError: Feature not in features dictionary almost always means the model or preprocessing code expects a key that your input pipeline did not provide. The fix is rarely complicated, but you have to compare the expected feature names and the actual data keys at the exact boundary where the failure happens.

What the Features Dictionary Represents

In TensorFlow and similar pipelines, a "features dictionary" is usually a mapping from feature names to tensors or parsing specs. For example:

python
1FEATURE_SPEC = {
2    "age": tf.io.FixedLenFeature([], tf.int64),
3    "income": tf.io.FixedLenFeature([], tf.float32),
4    "country": tf.io.FixedLenFeature([], tf.string),
5}

Later, the input pipeline parses serialized examples using those exact keys:

python
parsed = tf.io.parse_single_example(serialized_example, FEATURE_SPEC)

If downstream code asks for "gender" but the parsed dictionary only contains "age", "income", and "country", you get the error.

The Most Common Cause: Name Drift

This error usually appears after one of these changes:

  • A feature was renamed in the dataset but not in the model code
  • A preprocessing step dropped a column
  • Training and serving input schemas diverged
  • A typo slipped into a feature column or transform function

Here is a simple mismatch:

python
1features = {
2    "age": tf.constant([25, 31]),
3    "income": tf.constant([50000.0, 82000.0]),
4}
5
6print(features["country"])

That raises a key-related failure because "country" is not present.

Debug by Printing the Real Keys

Do not guess. Print the keys right before the failing line:

python
print("Expected keys:", ["age", "income", "country"])
print("Actual keys:", sorted(features.keys()))

In dataset pipelines, insert the check inside the mapping function:

python
1def debug_map(features, label):
2    tf.print("feature keys:", list(features.keys()))
3    return features, label
4
5dataset = dataset.map(debug_map)

Once you see the actual keys, the bug is often obvious.

Keep the Schema in One Place

The best prevention is a single source of truth for feature names. Instead of scattering string literals across parsing code, feature engineering, and model construction, define the schema once and reuse it.

python
1FEATURE_NAMES = ["age", "income", "country"]
2
3def validate_features(features):
4    missing = [name for name in FEATURE_NAMES if name not in features]
5    if missing:
6        raise ValueError(f"Missing features: {missing}")

This turns a vague runtime failure into a precise, early validation step.

You can go one step further and add a test around the schema contract. A tiny unit test that builds one example input and checks the feature keys is far cheaper than discovering the mismatch after a long training job or during serving deployment.

Watch Out for Preprocessing Pipelines

The error is especially common when using tools such as tf.feature_column, tf.Transform, or custom preprocessing functions. A feature may exist in the raw example but disappear after transformation because:

  • It was renamed
  • It was bucketized into a new key
  • It was excluded from the returned dictionary

For example:

python
1def preprocess(features):
2    return {
3        "age_bucket": tf.cast(features["age"] // 10, tf.int32),
4        "income": features["income"],
5    }

Any later code still expecting raw "age" will fail unless you deliberately keep it.

Common Pitfalls

  • Fixing the training schema but forgetting the serving or inference schema.
  • Looking at the raw input record instead of the transformed dictionary that the model actually receives.
  • Chasing TensorFlow internals when the real problem is a typo in a feature name.
  • Renaming a feature in one notebook or module without updating the rest of the pipeline.

Summary

  • The error means a required feature key is missing from the actual dictionary being used.
  • Print the keys at the failing boundary instead of guessing.
  • Centralize feature names so parsing, preprocessing, and modeling stay aligned.
  • Pay special attention to transformed pipelines where keys may be renamed or dropped.
  • Most fixes come down to schema consistency, not framework bugs.

Course illustration
Course illustration

All Rights Reserved.