diff --git a/loopy/type_inference.py b/loopy/type_inference.py
index 4df619fea0fc965afd72964004c286e751aa8270..78d817ce73724d90a6cc6f380b24290971f6c1e7 100644
--- a/loopy/type_inference.py
+++ b/loopy/type_inference.py
@@ -332,13 +332,21 @@ class TypeInferenceMapper(CombineMapper):
         if not agg_result:
             return agg_result
 
-        fields = agg_result[0].numpy_dtype.fields
+        numpy_dtype = agg_result[0].numpy_dtype
+        fields = numpy_dtype.fields
         if fields is None:
             raise LoopyError("cannot look up attribute '%s' in "
                     "non-aggregate expression '%s'"
                     % (expr.aggregate, expr.name))
 
-        dtype = fields[0]
+        try:
+            field = fields[expr.name]
+        except KeyError:
+            raise LoopyError("cannot look up attribute '%s' in "
+                    "aggregate expression '%s' of dtype '%s'"
+                    % (expr.aggregate, expr.name, numpy_dtype))
+
+        dtype = field[0]
         return [NumpyType(dtype)]
 
     def map_comparison(self, expr):