Using get_last_partition_key with Partition Loading Context

Last updated: October 28, 2025

Problem Description

The get_last_partition_key function requires a partition loading context when used with a current_time parameter. This change affects users who rely on this function in schedule execution functions to find partitions to trigger at scheduled execution times.

Symptoms

  • Errors when using get_last_partition_key with current_time parameter

  • Failed schedule execution functions that depend on finding the latest partition

Root Cause

The current_time argument was removed from get_last_partition_key as it was not part of the public API. The function now requires a partition loading context to properly determine the last partition key.

Solution

Wrap the get_last_partition_key call with a partition_loading_context

import datetime
import dagster as dg
from dagster._core.definitions.partitions.context import partition_loading_context

partitions_def = dg.DailyPartitionsDefinition(start_date="2025-01-01")
with partition_loading_context(effective_dt=datetime.datetime(2025, 1, 2)):
    print(partitions_def.get_last_partition_key())  # 2025-01-01

Step-by-Step Resolution

  1. Import the required context manager from dagster (Note: In upcoming releases, this will be available from the top-level dagster module)

  2. Wrap your get_last_partition_key call with the context manager

  3. Pass the desired timestamp using the effective_dt parameter

Alternative Solutions

While get_partition_key_for_timestamp is available, it may return keys for non-existent partitions and requires additional validation code.

Prevention

Always use the partition loading context when working with partition keys that depend on specific timestamps.

Related Documentation