Introducing a new way to review your PRs.

Read the announcement

dagster-io / dagster

An orchestration platform for the development, production, and observation of data assets.

View on GitHub

19 documents · Updated Nov 23, 2026

docs / resources-and-io-managers

Resources and I/O managers

Resources and I/O managers connect user code to services outside a run. Resources provide scoped clients and shared services through dependency injection. I/O managers define how values cross step boundaries or persist as assets. Together, they let compute code focus on data work while the execution runtime controls setup, access, cleanup, and run events. See Execution runtime, Run planning and execution, Instance and persistence, and Events and observability for the surrounding systems.

Resource model

Resource definitions

A resource is a named service that the runtime creates for a run and makes available to user code. A resource definition describes how to create that service, how to validate its configuration, which other resources it needs, and, when used, which version identifies the definition. A resource can represent a database client, an API client, a credential-aware helper, or a simple shared value. ResourceDefinition

The resource key is the contract between a definition and its users. Compute functions, hooks, type loaders, input managers, and I/O managers request keys. The runtime then exposes the matching objects through their context instead of requiring each caller to construct them.

Resources can receive run information, configuration, logging, the instance, and their already-built dependencies through InitResourceContext. This context is the main boundary between runtime state and resource setup.

References: python_modules/…/resource_definition.py• L45-89 python_modules/…/init.py• L18-43

Resource dependency graph

Resource dependencies form a directed graph. If resource warehouse needs resource credentials, the runtime builds credentials first and gives it to warehouse during setup. The graph also lets the runtime find the full set of resources needed by the selected steps.

The runtime rejects a graph with a missing key or a cycle. It does not need to create every declared resource for every step. It selects the resources required by compute code, input and output managers, type loaders, and hooks, then adds their transitive dependencies. resources_init.py resources_init.py

References: python_modules/…/resources_init.py• L67-116 python_modules/…/resources_init.py• L352-436

Resource lifecycle

The runtime creates resources before the work that needs them and releases them after that work ends. A resource function can return one object for simple setup or yield one object to keep a cleanup scope open. Code after the yield runs during teardown.

Setup follows dependency order. Teardown follows the reverse order so that a dependent service closes before the service it uses. The selected executor can create more than one resource instance during a run, so external clients must not assume that one process-wide instance exists. Setup and teardown failures are reported as resource lifecycle failures and can stop the run. resources_init.py resources_init.py

The same lifecycle applies when resources are used outside a job through build_resources. That API gives callers a managed scope and closes resources when the scope exits. build_resources.py

References: python_modules/…/resource_definition.py• L45-57 python_modules/…/resources_init.py• L119-215 python_modules/…/resources_init.py• L248-349 python_modules/…/build_resources.py• L43-119

Execution access

Dependency injection

Dependency injection means that the runtime supplies a requested service to user code instead of user code creating it. A step context contains only the resources needed by that step. A manager context contains the resources requested by the manager. This keeps service setup in one place and makes the same user code usable with different resource definitions.

The runtime computes step requirements from the op, input sources, output managers, input managers, and types. It builds a scoped resource view for the step and uses resource keys to resolve manager instances. StepExecutionContext system.py

References: python_modules/…/system.py• L416-449 python_modules/…/system.py• L549-576 python_modules/…/resources_init.py• L389-436

Initialization contexts

An initialization context describes the run and the resource being created. It carries resource configuration and already-initialized dependencies. This lets a resource build a client with the correct settings while keeping run-aware logging and instance access available when needed.

Resource definitions may also be supplied as already-created values. The runtime wraps those values in definitions so they follow the same key-based access model. An I/O manager value receives an I/O-manager definition rather than a generic resource definition, which preserves its input and output contract. build_resources.py

References: python_modules/…/init.py• L34-85 python_modules/…/build_resources.py• L122-147

I/O managers

Manager contract

An I/O manager is a resource that owns the boundary between an in-memory Python value and an external or shared store. Its handle_output method receives a produced value and stores it. Its load_input method retrieves the value needed by a downstream step. IOManager

The output and input sides use separate contexts because they answer different questions. The output side describes what a step produced. The input side describes what a step needs and, when applicable, which upstream output or asset supplies it. An I/O manager definition can also declare manager configuration and per-input or per-output configuration. An I/O manager can request other resources such as a client or a filesystem service through its resource definition.

An I/O manager can be selected at the job level or for a specific output. An input manager is a related resource that loads an input without also defining output storage. When a downstream input has no explicit input manager, the runtime uses the manager associated with its upstream output or asset. IOManagerDefinition InputManagerDefinition

References: python_modules/…/io_manager.py• L35-100 python_modules/…/io_manager.py• L121-151 python_modules/…/input_manager.py• L30-65 python_modules/…/inputs.py• L345-395

Input handling

Before compute code runs, the execution runtime resolves each step input. An input may come from an upstream step output, an asset, run configuration, a direct value, or a declared default. Manager-backed inputs receive an InputContext, and type-backed configured inputs use the type loader instead.

For an upstream value, the input context includes the upstream output identity. For an asset, it includes the asset key and partition information when present. It also provides input-level configuration, resource configuration, definition metadata, logging, and access to the manager's required resources. The manager returns the Python value that the step receives. StepInputSource InputContext InputContext.resources

The runtime records a loaded-input event after the manager returns. It then type-checks the value before it passes the value to compute code. This separates storage errors from user compute errors and makes the input path visible in run events. execute_step.py inputs.py

References: python_modules/…/inputs.py• L70-90 python_modules/…/inputs.py• L108-207 python_modules/…/inputs.py• L345-395 python_modules/…/inputs.py• L434-457 python_modules/…/execute_step.py• L459-482

Output handling

After compute code produces an output, the runtime checks the value against its declared type and resolves the output's I/O manager. It builds an OutputContext with the step key, output name, run identity, output configuration, metadata, asset information, partition information, and manager resources. get_output_context

The runtime then calls handle_output with the value. A successful call produces a handled-output event. The manager can also add output metadata or report an asset materialization. Outputs that do not represent stored data, such as Nothing outputs and certain asset events, bypass storage when the execution model says no value must be persisted. execute_step.py

The core flow connects compute, type checks, storage, and observability in this order:

flowchart LR
    A[Compute code] --> B[Produced value]
    B --> C[Output type check]
    C --> D[Output context]
    D --> E[I/O manager]
    E --> F[External or shared store]
    E --> G[Handled output and asset events]
    F --> H[Downstream input load]
    H --> I[Input context]
    I --> J[Input type check]
    J --> A2[Downstream compute]

References: python_modules/…/execute_step.py• L386-435 python_modules/…/execute_step.py• L552-579 python_modules/…/execute_step.py• L754-884 python_modules/…/output.py• L66-80

External system boundaries

Storage and identity

An I/O manager chooses the storage system and the identity used to find a value later. The context supplies stable information such as a run or version identity, step key, output name, mapping key, asset key, and partition scope. A manager uses the identity that matches its storage model.

The built-in filesystem manager shows the two common storage choices. Step outputs use run and step identity so separate executions do not overwrite each other. Assets use their asset path so a later materialization replaces the current asset value. The in-memory manager uses the output identifier and keeps values only for the current manager lifetime. fs_io_manager.py mem_io_manager.py

Database-backed managers use a table slice as the storage identity. The slice combines the database, schema, table, selected columns, and partition filters. Type handlers then store or load supported Python types through a database client. Warehouse-specific escaping keeps partition filters correct for values that contain apostrophes or backslashes during both reads and replacement deletes. db_io_manager.py db_io_manager.py

This means storage durability is a manager property, not a general guarantee of the execution runtime. A run can load data from a prior run only when the selected manager and its identity rules support that behavior.

References: python_modules/…/output.py• L628-664 python_modules/…/input.py• L434-458 python_modules/…/fs_io_manager.py• L34-54 python_modules/…/mem_io_manager.py• L6-20 python_modules/…/db_io_manager.py• L24-66 python_modules/…/db_io_manager.py• L120-205

Metadata and events

Manager contexts carry definition metadata and run-time metadata so an external system can be described in the run record. Managers can add metadata during input or output handling. Output handling can also emit materialization or observation events for assets.

The runtime consumes these additions and combines them with its own step events. This keeps storage facts, such as a path or row count, next to the handled output or asset event without requiring compute code to know how the manager stores data. OutputContext InputContext

References: python_modules/…/output.py• L697-814 python_modules/…/input.py• L484-552 python_modules/…/execute_step.py• L813-884

Change checklist

When these files or sections are changed, remember to consider these:

  • A resource key or required dependency change affects step resource selection, manager lookup, and the transitive initialization graph.
  • An input or output manager assignment change affects both sides of the value boundary. The downstream input must still resolve the manager that can read the upstream value.
  • An output or input identifier change affects existing stored values, re-execution loads, versioned outputs, and partition-scoped asset data.
  • A context field or metadata contract change affects custom managers and the events that describe loaded inputs, handled outputs, and asset materializations.
  • A resource generator lifecycle change affects setup order, reverse teardown, and executors that create more than one resource instance.
  • A manager-level or per-input/per-output configuration change affects both config validation and the values that custom managers receive through their contexts.
  • A partition filter change affects both warehouse reads and cleanup deletes. The escaping rule must match the SQL dialect used by each database client.

Important failure modes

TriggerConsequenceGuard
A required resource key is missing or creates a dependency cycleResource initialization fails before dependent work can runValidate the resource graph before execution and keep dependency keys aligned with supplied definitions.
A resource generator yields no object or more than one objectResource setup or teardown raises a user-code resource errorYield exactly one service object and keep cleanup after that yield.
An input explicitly names a resource that is not an InputManagerThe step cannot load the inputReturn an input-capable manager for input-manager keys.
An output manager cannot store or reload the valueOutput handling or a later input load fails, so the affected step failsMatch value types and identity rules to the selected store.
An output fails its declared type checkThe runtime does not complete output storage for that valueKeep the produced value aligned with the output definition.
A manager accesses asset or partition fields that are not present in its contextContext access raises an invariant error during input or output handlingCheck the corresponding has_* property before using optional asset or partition data.
A manager receives configuration outside its declared contractResource setup or input/output handling fails during config validation or useKeep manager, input, and output configuration schemas aligned with the context fields the manager reads.
A partition key is escaped for the wrong warehouse string-literal rulesA read can select the wrong rows or a cleanup delete can remove the wrong slice. A warehouse can also reject a valid key.Use the database dialect's escape rules for every partition filter.

Key files

FileWhy to read it
python_modules/…/resource_definition.pyDefines the resource contract, configuration, dependency keys, version marker, and cleanup model.
python_modules/…/resources_init.pyShows how the runtime selects, orders, initializes, and tears down resources.
python_modules/…/build_resources.pyShows how the same resource lifecycle works in a managed context outside a job.
python_modules/…/system.pyConnects step execution to scoped resources and manager contexts.
python_modules/…/io_manager.pyDefines the input/output manager contract, manager configuration, and per-input or per-output configuration.
python_modules/…/inputs.pyDescribes the supported input sources and the manager load boundary.
python_modules/…/input.pyDefines the information available while loading an input.
python_modules/…/output.pyDefines the information available while storing an output.
python_modules/…/execute_step.pyShows where type checks, manager calls, and output events fit in step execution.
python_modules/…/db_io_manager.pyIllustrates database table identity, type-handler boundaries, partition filters, and warehouse-specific literal escaping.
python_modules/…/fs_io_manager.pyIllustrates durable filesystem storage and asset versus step-output identity.
python_modules/…/mem_io_manager.pyIllustrates run-local, in-memory input and output handling.