diff --git a/airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py b/airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py index 50fe4f0bc..000beeff9 100644 --- a/airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py +++ b/airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py @@ -374,7 +374,11 @@ def _migrate_child_state_to_parent_state(self, stream_state: StreamState) -> Str # Ignore per-partition states or invalid formats. if isinstance(substream_state, (list, dict)) or len(substream_state_values) != 1: # If a global state is present under the key "state", use its first value. - if "state" in stream_state and isinstance(stream_state["state"], dict): + if ( + "state" in stream_state + and isinstance(stream_state["state"], dict) + and stream_state["state"] != {} + ): substream_state = list(stream_state["state"].values())[0] else: return {} diff --git a/unit_tests/sources/declarative/partition_routers/test_substream_partition_router.py b/unit_tests/sources/declarative/partition_routers/test_substream_partition_router.py index c4a608f4a..675e76a95 100644 --- a/unit_tests/sources/declarative/partition_routers/test_substream_partition_router.py +++ b/unit_tests/sources/declarative/partition_routers/test_substream_partition_router.py @@ -473,6 +473,17 @@ def test_substream_partition_router_invalid_parent_record_type(): }, {"parent_stream_cursor": "2023-05-27T00:00:00Z"}, ), + # Case 7: Migrate child state to parent state but child state is empty + ( + { + "state": {}, + "states": [], + "parent_state": {"posts": {}}, + "lookback_window": 1, + "use_global_cursor": False, + }, + {}, + ), ], ids=[ "empty_initial_state", @@ -481,6 +492,7 @@ def test_substream_partition_router_invalid_parent_record_type(): "initial_state_no_parent_per_partition_state", "initial_state_with_parent_state", "initial_state_no_parent_global_state_declarative", + "initial_state_no_parent_and_no_child", ], ) def test_set_initial_state(initial_state, expected_parent_state):