IIRC, the context is an object that you can send a...
# ask-community
c
IIRC, the context is an object that you can send as the first function parameter that includes dagster info. I could be wrong here, but try:
def dbt_op(context):
as the function signature
❤️ 1
l
Thank you @clay...I tried already this solution and I receive:
Copy code
dagster._core.errors.DagsterInvalidInvocationError: Resource initialization function has context argument, but no context was provided when invoking.
I then tried to use the
build_op_context
function, but still it doesn't work. Dagster says that the --project-dir flag is not a dbt project. And at this point I was even more confused because I don't know how to create a context that contains dbt flags
j
this error message indicates that a resource doesn’t have the correct context. can you share how you’re creating your resources?
❤️ 1
l
Hi jamie, this is how I defined the resources. It works fine for the assets DAG, but not for my dbt run op.
Copy code
# Define the nedeed paths as variables
DBT_PROJECT_DIR = file_relative_path(__file__, "../UM_FOX_AU-dbt/dbt")
DBT_PROFILES_DIR = file_relative_path(__file__, "../UM_FOX_AU-dbt/dbt/config")
DBT_CONFIG = {"project_dir": DBT_PROJECT_DIR, "profiles_dir": DBT_PROFILES_DIR}

# Loading dbt assets
dbt_assets = load_assets_from_dbt_project(project_dir=DBT_PROJECT_DIR)

# Resources definition
resource_defs = {
    "dbt": dbt_cli_resource.configured(DBT_CONFIG)
}
j
ok this looks good. i think the issue is probably that the job doesn’t have dbt cli resource attached to it. can you try
Copy code
@op
def dbt_op():
    cli_resource = dbt_cli_resource(context)
    cli_resource.run(models=["my_model"])
@job(
    resource_defs={
    "dbt": dbt_cli_resource.configured(DBT_CONFIG)
}
)
def dbt_job():
    dbt_op()
l
Thank you for taking the time to help me 🙏🏻 Tried your code snippet and "context" is not defined in this case.(
NameError: name 'context' is not defined
). - I then tried:
Copy code
@op
def dbt_op(context):
    cli_resource = dbt_cli_resource(context)
    cli_resource.run(models=["TMP_UM_CM_B_ADDRESS_P_AU_NEW_RECORDS"])
@job(
    resource_defs={
    "dbt": dbt_cli_resource.configured(DBT_CONFIG)
}
)
def dbt_job():
    dbt_op()
And got back:
dagster._check.ParameterCheckError: Param "context" is not a UnboundInitResourceContext. Got <dagster._core.execution.context.compute.OpExecutionContext object at 0x7f7c22ff1d00> which is type <class 'dagster._core.execution.context.compute.OpExecutionContext'>.
- Finally, I removed the "context" from both and got:
dagster._core.errors.DagsterInvalidInvocationError: Resource initialization function has context argument, but no context was provided when invoking.
j
ok this is on me, i misread your op a bit. Within the body of the op you don’t need to instantiate the dbt resource. instead you can tell dagster that the op needs the resource and then you can access it from the context within the op
Copy code
@op(
    required_resource_keys={"dbt}
)
def dbt_op(context):
    cli_resource = context.resources.dbt
    cli_resource.run(models=["TMP_UM_CM_B_ADDRESS_P_AU_NEW_RECORDS"])
@job(
    resource_defs={
    "dbt": dbt_cli_resource.configured(DBT_CONFIG)
}
)
def dbt_job():
    dbt_op()
❤️ 1
l
@jamie hopefully you like flowers, because I am preparing a boquet and sending it straight to Elementl HQ... Now the op is finally working! Thank you so much! dagster evolution