[Bug]: [Python] VertexAIFeatureStoreEnrichmentHandler raises UnboundLocalError on NotFound when exception_level=ExceptionLevel.QUIET
What happened?
VertexAIFeatureStoreEnrichmentHandler (sdks/python/apache_beam/transforms/enrichment_handlers/vertex_ai_feature_store.py) has three related bugs around NotFound handling and client initialization:
UnboundLocalErrorwhenexception_level=ExceptionLevel.QUIET(lines164-182): When an entity key is not present in Vertex AI Feature Store andfetch_feature_valuesraisesgoogle.api_core.exceptions.NotFound, theexcept NotFound:block handlesExceptionLevel.WARN(return request, beam.Row()) andExceptionLevel.RAISE, but has no branch forExceptionLevel.QUIET. Execution falls through to line181(response_dict = dict(response.proto_struct)) and crashes with:UnboundLocalError: cannot access local variable 'response' where it is not associated with a value.VertexAIFeatureStoreLegacyEnrichmentHandlerignoresexception_levelonNotFound(lines302-305): Unconditionally raisesValueErroronNotFoundeven whenexception_levelisExceptionLevel.WARN(the default) orExceptionLevel.QUIET.- Live network RPCs inside
__init__(lines115-125and258-268):__init__instantiates an admin client and callsget_feature_online_store/aiplatform.Featurestore(...)during pipeline construction rather than inside__enter__, breaking offline pipeline validation, CI unit tests without GCP credentials, and template builds.
Minimal reproducer for (1):
from unittest import mock
import apache_beam as beam
from google.api_core.exceptions import NotFound
from apache_beam.transforms.enrichment_handlers.utils import ExceptionLevel
from apache_beam.transforms.enrichment_handlers.vertex_ai_feature_store import (
VertexAIFeatureStoreEnrichmentHandler,
)
handler = VertexAIFeatureStoreEnrichmentHandler.__new__(
VertexAIFeatureStoreEnrichmentHandler)
handler.row_key = 'user_id'
handler.feature_store_name = 'fs'
handler.feature_view_name = 'fv'
handler.feature_view_path = 'projects/p/locations/us-central1/featureOnlineStores/fs/featureViews/fv'
handler.exception_level = ExceptionLevel.QUIET
handler.client = mock.MagicMock()
handler.client.fetch_feature_values.side_effect = NotFound('missing entity')
handler(beam.Row(user_id='u_missing'))
# Raises UnboundLocalError: cannot access local variable 'response'
Cause
Missing return request, beam.Row() for ExceptionLevel.QUIET inside except NotFound: at lines 171-180, and eager admin existence checks in __init__ instead of __enter__.
Issue Priority
Priority: 2 (default / most bugs should be filed as P2)
Issue Components
- [x] Component: Python SDK
- [ ] Component: Java SDK
- [ ] Component: Go SDK
- [ ] Component: Typescript SDK
- [ ] Component: IO connector
- [ ] Component: Beam YAML
- [x] Component: Beam ML
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Infrastructure
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Prism Runner
- [ ] Component: Hazelcast Jet Runner
- [ ] Component: Google Cloud Dataflow Runner
.take-issue
I reproduced the missing entity failures and have a focused fix for the exception handling in both Vertex AI handlers. The tests check that QUIET returns an empty row without logging, WARN logs and returns an empty row, and RAISE still raises. I’m keeping the client initialization change out of this PR so it can be reviewed separately.
Thanks for the clear repro. I opened #41419 for the two NotFound behaviors. I verified both handlers with mocked responses: QUIET stays silent, WARN logs and returns an empty row, and RAISE still errors. The constructor RPCs are unchanged, so this PR won’t close the full issue.