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(close): fall back to standard fields when custom fields make the list too long (#91686)

Tom Owers3a7313cd72ac
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/close/close.py b/products/warehouse_sources/backend/temporal/data_imports/sources/close/close.py
index 3a969bd99b56..3ee5322c6c6e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/close/close.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/close/close.py
@@ -9,6 +9,7 @@ from requests import Response, Session
from structlog.types import FilteringBoundLogger
from products.warehouse_sources.backend.temporal.data_imports.sources.close.search import (
+ CloseSearchError,
fetch_custom_field_selectors,
iter_search_rows,
)
@@ -221,17 +222,38 @@ def close_search_source(
def items() -> Iterator[list[dict[str, Any]]]:
session = _make_search_session(api_key)
- fields = [*config.search_fields, *fetch_custom_field_selectors(session, CLOSE_BASE_URL, object_type, logger)]
- yield from iter_search_rows(
- session=session,
- base_url=CLOSE_BASE_URL,
- object_type=object_type,
- fields=fields,
- cursor_field=cursor_field,
- start_anchor=start_anchor,
- logger=logger,
- on_checkpoint=save_checkpoint,
- )
+ custom_fields = fetch_custom_field_selectors(session, CLOSE_BASE_URL, object_type, logger)
+ fields = [*config.search_fields, *custom_fields]
+
+ try:
+ yield from iter_search_rows(
+ session=session,
+ base_url=CLOSE_BASE_URL,
+ object_type=object_type,
+ fields=fields,
+ cursor_field=cursor_field,
+ start_anchor=start_anchor,
+ logger=logger,
+ on_checkpoint=save_checkpoint,
+ )
+ except CloseSearchError as exc:
+ if "list is too long" not in str(exc).lower() or not custom_fields:
+ raise
+ # Some orgs have enough custom fields that the combined _fields list exceeds Close's
+ # undocumented limit. Fall back to standard fields so the sync can complete.
+ logger.warning(
+ f"Close: _fields list too long ({len(fields)} fields), retrying with standard fields. object_type={object_type}"
+ )
+ yield from iter_search_rows(
+ session=session,
+ base_url=CLOSE_BASE_URL,
+ object_type=object_type,
+ fields=config.search_fields,
+ cursor_field=cursor_field,
+ start_anchor=start_anchor,
+ logger=logger,
+ on_checkpoint=save_checkpoint,
+ )
return SourceResponse(
name=endpoint,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/close/tests/test_close_search.py b/products/warehouse_sources/backend/temporal/data_imports/sources/close/tests/test_close_search.py
index 2a48f5068a69..f44331f63189 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/close/tests/test_close_search.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/close/tests/test_close_search.py
@@ -328,6 +328,39 @@ class TestSearchSourceWiring:
assert session.bodies[0]["query"]["queries"][0]["object_type"] == endpoint.rstrip("s").lower()
+ def test_fields_list_too_long_retries_without_custom_fields(self, monkeypatch: Any) -> None:
+ # Orgs with many custom fields can push the combined _fields list past Close's limit.
+ # The source must fall back to standard fields so the sync can complete rather than
+ # permanently failing on every Temporal retry.
+ manager = MagicMock(spec=ResumableSourceManager)
+ manager.can_resume.return_value = False
+ too_long = _response(
+ {"errors": [], "field-errors": {"_fields": {"errors": {"lead": "List is too long."}}}},
+ status_code=400,
+ )
+ ok = _response({"data": []})
+ custom_field_page = _response({"data": [{"id": "cf_x"}], "has_more": False})
+ session = FakeSession(pages=[too_long, ok], get_pages=[custom_field_page])
+ monkeypatch.setattr(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.close.close._make_search_session",
+ lambda _api_key: session,
+ )
+
+ response = close_source(
+ api_key="test-key",
+ endpoint="Leads",
+ team_id=1,
+ job_id="job",
+ resumable_source_manager=manager,
+ db_incremental_field_last_value=None,
+ logger=MagicMock(),
+ )
+ list(cast(Any, response.items()))
+
+ assert len(session.bodies) == 2
+ assert "custom.cf_x" in session.bodies[0]["_fields"]["lead"]
+ assert "custom.cf_x" not in session.bodies[1]["_fields"]["lead"]
+
@pytest.mark.parametrize(
("incremental", "field", "last_value", "expected_cursor_field", "expected_anchor"),
[