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): give clear error when source config is not a mapping (#64628)

Tom Owers67ebba022086
diff --git a/posthog/temporal/data_imports/sources/common/config.py b/posthog/temporal/data_imports/sources/common/config.py
index f4bbdf09f04d..5e0167896676 100644
--- a/posthog/temporal/data_imports/sources/common/config.py
+++ b/posthog/temporal/data_imports/sources/common/config.py
@@ -531,6 +531,13 @@ def config(
def wrap(cls: type[_T]) -> type[_T]:
def from_dict(cls, d: dict[str, typing.Any]):
+ if not isinstance(d, dict):
+ # Stored config (e.g. an `EncryptedJSONField`) can occasionally come back as a
+ # non-mapping (a double-encoded string). Fail with an actionable message instead
+ # of the opaque `TypeError: string indices must be integers` raised when `to_config`
+ # tries to index into it.
+ raise TypeError(f"Cannot build '{cls.__name__}' from {type(d).__name__}; expected a mapping")
+
if prefix:
prefixes = (prefix,)
else:
diff --git a/posthog/temporal/data_imports/sources/common/test/test_config.py b/posthog/temporal/data_imports/sources/common/test/test_config.py
index 494a1e0f3544..4b2bd021bedd 100644
--- a/posthog/temporal/data_imports/sources/common/test/test_config.py
+++ b/posthog/temporal/data_imports/sources/common/test/test_config.py
@@ -462,6 +462,30 @@ def test_to_config_scalar_under_nested_config_key(
assert cfg.account_id == expected_account_id
[email protected]("bad_input", ["not a mapping", '{"key_file": {}}', b"bytes", 5, ["a", "b"], None])
+def test_from_dict_with_non_mapping_raises_clear_error(bad_input):
+ """A non-mapping input must raise an actionable error, not the opaque builtin `TypeError`.
+
+ Stored config can come back as a non-mapping (e.g. a double-encoded string from an
+ `EncryptedJSONField`). Indexing into it deep inside `to_config` raised
+ `TypeError: string indices must be integers`, which is impossible to triage from the
+ source alone. `from_dict` must reject it up front with the config class name in the message.
+ """
+
+ @config.config
+ class KeyFileConfig:
+ project_id: str
+ private_key: str
+
+ @config.config
+ class SourceConfig(config.Config):
+ key_file: KeyFileConfig
+ dataset_id: str
+
+ with pytest.raises(TypeError, match="Cannot build 'SourceConfig'"):
+ SourceConfig.from_dict(bad_input)
+
+
def test_validate_dict():
@config.config
class TestConfig(config.Config):