Skip to content

: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.

fix(data-imports): skip OpenAI projects that 400 on sub-resource listing (#72190)

Tom Owers929ec86dac2e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/openai/openai.py b/products/warehouse_sources/backend/temporal/data_imports/sources/openai/openai.py
index 35b28088eb70..c16ff720198e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/openai/openai.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/openai/openai.py
@@ -350,14 +350,24 @@ def _iter_project_fan_out_rows(
after = resume_after
resume_after = None # only the resumed-into project uses the saved cursor
- for items, last_id in _iter_cursor_pages(session, headers, logger, path, {}, after):
- for item in items:
- row = {**_normalize_entity(config.name, item), "project_id": project_id}
- batcher.batch(row)
- while batcher.should_yield():
- yield batcher.get_table()
- if last_id:
- resumable_source_manager.save_state(OpenAIResumeConfig(cursor=last_id, project_id=project_id))
+ try:
+ for items, last_id in _iter_cursor_pages(session, headers, logger, path, {}, after):
+ for item in items:
+ row = {**_normalize_entity(config.name, item), "project_id": project_id}
+ batcher.batch(row)
+ while batcher.should_yield():
+ yield batcher.get_table()
+ if last_id:
+ resumable_source_manager.save_state(
+ OpenAIResumeConfig(cursor=last_id, project_id=project_id)
+ )
+ except requests.HTTPError as e:
+ # Some projects reject listing their project-scoped sub-resources with a 400 even for a
+ # valid admin key (e.g. the org's default project). Skip just that project so the rest of
+ # the endpoint still syncs, rather than failing the whole run on one bad project.
+ if e.response is None or e.response.status_code != 400:
+ raise
+ logger.warning(f"OpenAI: skipping {config.name} for project {project_id} after a 400 from the API")
# Flush any incomplete batch before checkpointing to the next project, otherwise a crash
# between the state save and the final flush would drop this project's buffered rows.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/openai/tests/test_openai.py b/products/warehouse_sources/backend/temporal/data_imports/sources/openai/tests/test_openai.py
index c893efe2045a..9684510e6a41 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/openai/tests/test_openai.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/openai/tests/test_openai.py
@@ -336,6 +336,47 @@ class TestProjectFanOut:
rows = _collect(_FakeResumableManager(), monkeypatch, responses, "project_users")
assert [(r["project_id"], r["id"]) for r in rows] == [("proj_1", "user_1"), ("proj_2", "user_1")]
+ def test_400_on_one_project_is_skipped_and_others_still_import(self, monkeypatch: Any) -> None:
+ # A single project can reject listing its sub-resources with a 400 even for a valid admin
+ # key (e.g. the org's default project). That must skip only that project, not fail the whole
+ # endpoint's sync — a 400 on proj_1 here still lets proj_2 import.
+ def fake_fetch(session: Any, url: str, headers: dict[str, str], logger: Any) -> dict:
+ if "/projects/proj_1/" in url:
+ raise requests.HTTPError("400 Client Error", response=MagicMock(status_code=400))
+ if "/projects/proj_2/" in url:
+ return {"data": [{"id": "user_1"}], "has_more": False, "last_id": "user_1"}
+ return {"data": [{"id": "proj_1"}, {"id": "proj_2"}], "has_more": False, "last_id": "proj_2"}
+
+ monkeypatch.setattr(openai, "_fetch_page", fake_fetch)
+ rows: list[dict] = []
+ for table in get_rows(
+ api_key="sk-admin-test",
+ endpoint="project_users",
+ logger=MagicMock(),
+ resumable_source_manager=_FakeResumableManager(), # type: ignore[arg-type]
+ ):
+ rows.extend(table.to_pylist())
+ assert [(r["project_id"], r["id"]) for r in rows] == [("proj_2", "user_1")]
+
+ def test_non_400_http_error_still_propagates(self, monkeypatch: Any) -> None:
+ # Only a 400 is a per-project skip; other client errors must surface so real failures aren't
+ # silently swallowed.
+ def fake_fetch(session: Any, url: str, headers: dict[str, str], logger: Any) -> dict:
+ if "/projects/proj_1/" in url:
+ raise requests.HTTPError("404 Client Error", response=MagicMock(status_code=404))
+ return {"data": [{"id": "proj_1"}], "has_more": False, "last_id": "proj_1"}
+
+ monkeypatch.setattr(openai, "_fetch_page", fake_fetch)
+ with pytest.raises(requests.HTTPError):
+ list(
+ get_rows(
+ api_key="sk-admin-test",
+ endpoint="project_users",
+ logger=MagicMock(),
+ resumable_source_manager=_FakeResumableManager(), # type: ignore[arg-type]
+ )
+ )
+
def test_flushes_each_projects_rows_before_checkpointing_next(self, monkeypatch: Any) -> None:
# A project's buffered rows must be yielded before the resume cursor advances to the next
# project; otherwise a crash before the final flush would lose them (they'd never be re-read