:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(meta-ads): sync the ad creatives table past a creative meta refuses to serve (#120164)
Tom Owersed4df5445aaa
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.pyindex 691b36057473..c961e5adb899 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py@@ -211,7 +211,7 @@ class MetaAdsSchema:single_object: bool = False# Scalar columns to copy out of the nested objects in each row (see `HoistedColumn`).hoisted_columns: tuple[HoistedColumn, ...] = ()- # Fields Meta can refuse to serve for a single row (see `META_INVALID_DOF_CREATIVE_ERROR_SUBCODE`).+ # Fields Meta can refuse to serve for a single row (see `META_REFUSED_CREATIVE_ERROR_SUBCODES`).refusable_fields: tuple[str, ...] = ()@@ -466,12 +466,15 @@ META_RATE_LIMIT_ERROR_CODES = {4, 17, 32, 613}# scratch with a new cursor.META_INVALID_CURSOR_ERROR_CODE = 2642-# Meta error code 100, subcode 2446211: Meta validates an Advantage+ ("degrees of freedom") creative-# again when it serves the creative-backed fields of an ad, and refuses when the creative's asset feed-# spec no longer matches its object story spec. Meta refuses the whole page for one such ad, so-# without a fallback every sync of the table stops at that ad. The schema's `refusable_fields` name-# the fields to drop from the page request and then read again per ad (see `_restore_refusable_fields`).-META_INVALID_DOF_CREATIVE_ERROR_SUBCODE = 2446211+# Meta error code 100 with one of these subcodes: Meta validates a creative again when it serves the+# creative-backed fields of an ad or an ad creative, and refuses one it can no longer render.+# 2446211: an Advantage+ ("degrees of freedom") creative whose asset feed spec no longer matches+# its object story spec.+# 1443048: a creative whose object story spec is malformed, for example one with no Page.+# Meta refuses the whole page for one such row, so without a fallback every sync of the table stops+# at that row. The schema's `refusable_fields` name the fields to drop from the page request and then+# read again per row (see `_restore_refusable_fields`).+META_REFUSED_CREATIVE_ERROR_SUBCODES = frozenset({2446211, 1443048})# Meta's multi-id lookup (`GET /?ids=...`) accepts at most 50 ids in one request.META_NODE_LOOKUP_MAX_IDS = 50@@ -572,8 +575,8 @@ def _is_invalid_cursor_error(response: Response) -> bool:return _meta_error_code(response) == META_INVALID_CURSOR_ERROR_CODE-def _is_invalid_dof_creative_error(response: Response) -> bool:- return _meta_error_subcode(response) == META_INVALID_DOF_CREATIVE_ERROR_SUBCODE+def _is_refused_creative_error(response: Response) -> bool:+ return _meta_error_subcode(response) in META_REFUSED_CREATIVE_ERROR_SUBCODESdef _raise_meta_api_error(response: Response) -> typing.NoReturn:@@ -713,7 +716,8 @@ def _is_refused_node(node_id: str, node: typing.Any, fields: tuple[str, ...]) ->error = node.get("error") if isinstance(node, dict) else Noneif not isinstance(error, dict):return False- if error.get("error_subcode") != META_INVALID_DOF_CREATIVE_ERROR_SUBCODE:+ subcode = error.get("error_subcode")+ if not isinstance(subcode, int) or subcode not in META_REFUSED_CREATIVE_ERROR_SUBCODES:raise Exception(f"Meta API request failed: node lookup error - {json.dumps(error)}")logger.warning("meta_ads_refused_fields_for_node", node_id=node_id, fields=fields)return True@@ -741,7 +745,7 @@ def _fetch_refusable_fields(raisecontinuereturn {node_id: node for node_id, node in nodes.items() if not _is_refused_node(node_id, node, fields)}- if not _is_invalid_dof_creative_error(response):+ if not _is_refused_creative_error(response):_raise_meta_api_error(response)if len(ids) == 1:logger.warning("meta_ads_refused_fields_for_node", node_id=ids[0], fields=fields)@@ -793,7 +797,7 @@ def _iter_simple_pagination(then raised as retryable (see ``ENTITY_PAGE_REFUSED_ERROR_MESSAGE``).When Meta refuses a page for one row's ``refusable_fields`` (see- ``META_INVALID_DOF_CREATIVE_ERROR_SUBCODE``), the same page is fetched+ ``META_REFUSED_CREATIVE_ERROR_SUBCODES``), the same page is fetchedagain without those fields, and the fields are read back per row from``node_lookup_url``. The next page asks for every field again."""@@ -837,7 +841,7 @@ def _iter_simple_pagination(refusable_fieldsand node_lookup_url is not Noneand not without_refusable_fields- and _is_invalid_dof_creative_error(response)+ and _is_refused_creative_error(response)):without_refusable_fields = Trueresponse = _issue()diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/schemas.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/schemas.pyindex 49f024f48414..80c97e6f1b08 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/schemas.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/schemas.py@@ -510,6 +510,27 @@ RESOURCE_SCHEMAS: dict[MetaAdsResource, dict[str, Any]] = {"degrees_of_freedom_spec","effective_authorization_category",],+ # Meta does not say which field triggers a refusal, so every spec-derived field is refusable.+ "refusable_fields": (+ "object_type",+ "object_story_id",+ "effective_object_story_id",+ "object_story_spec",+ "thumbnail_url",+ "image_url",+ "image_hash",+ "video_id",+ "body",+ "title",+ "call_to_action_type",+ "link_url",+ "url_tags",+ "instagram_permalink_url",+ "actor_id",+ "asset_feed_spec",+ "degrees_of_freedom_spec",+ "effective_authorization_category",+ ),},# `hash` rather than `id` as the key: Meta documents it as AdImage's default field, and the# `id` it returns is just the account and hash joined.diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.pyindex 9406469838d7..7dba1ea7f1cf 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py@@ -262,6 +262,10 @@ class MetaAdsSource(ResumableSource[MetaAdsSourceConfig, MetaAdsResumeConfig], O"Meta refused to return an Advantage+ creative ad whose asset variations don't match ""its main creative. Fix that ad in Ads Manager; the next sync runs on schedule."),+ '"error_subcode":1443048': (+ "Meta refused to return an ad creative whose story is incomplete, such as one with no "+ "Page. Fix or delete that creative in Ads Manager; the next sync runs on schedule."+ ),}def get_schemas(diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.pyindex 83ec75acdae1..8bb2bd8284d0 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py@@ -356,10 +356,12 @@ class TestSimplePaginationRefusedFields:"error": {"message": "Invalid parameter", "code": 100, "error_subcode": 2446211, "is_transient": False}}- def test_refused_row_syncs_without_refusable_fields(self) -> None:+ @pytest.mark.parametrize("subcode", [2446211, 1443048])+ def test_refused_row_syncs_without_refusable_fields(self, subcode: int) -> None:manager = _build_manager()+ refused_body = {"error": {**self.REFUSED_BODY["error"], "error_subcode": subcode}}responses = [- _mock_response(400, self.REFUSED_BODY),+ _mock_response(400, refused_body),_mock_response(200,{@@ -368,9 +370,9 @@ class TestSimplePaginationRefusedFields:},),# The lookup for both ads is refused, so it splits until the refused ad is alone.- _mock_response(400, self.REFUSED_BODY),+ _mock_response(400, refused_body),_mock_response(200, {"1": {"id": "1", "creative": {"id": "c1"}}}),- _mock_response(400, self.REFUSED_BODY),+ _mock_response(400, refused_body),_mock_response(200, {"data": [{"id": "3", "name": "c", "creative": {"id": "c3"}}], "paging": {}}),]@@ -1675,6 +1677,12 @@ class TestRetryableErrors:'"fbtrace_id":"AaBbCcDdEeFf00112233"}}',"Advantage+ creative",),+ (+ 'Meta API request failed: 400 - {"error":{"message":"Invalid parameter","type":'+ '"OAuthException","code":100,"error_subcode":1443048,"is_transient":false,'+ '"fbtrace_id":"AaBbCcDdEeFf00112233"}}',+ "story is incomplete",+ ),],)def test_retry_exhausted_message_replaces_the_raw_meta_response(@@ -2013,6 +2021,11 @@ class TestEndpointCatalog:# without a `RESOURCE_SCHEMAS` entry only fails at sync time, once a customer selects it.assert endpoint in get_meta_ads_schemas()+ @pytest.mark.parametrize("endpoint", list(ENDPOINTS))+ def test_refusable_fields_are_requested_fields(self, endpoint: str) -> None:+ schema = get_meta_ads_schemas()[endpoint]+ assert set(schema.refusable_fields) <= set(schema.field_names)+def test_resource_the_worker_does_not_know_raises_a_named_error(self, monkeypatch: pytest.MonkeyPatch) -> None:# The web pods and the workers deploy separately, so a newly shipped table is selectable in# the schema picker about an hour before every worker can resolve it. A bare KeyError there