<@ULVA73B9P> I want to use send_chat_message() wit...
# ask-marvin
j
@Marvin I want to use send_chat_message() within a task. It doesn’t allow me to do this due to
RuntimeError: Tasks cannot be run from within tasks. Did you mean to call this task in a flow?
Is there a way around this issue?
m
It appears there are no GitHub issues directly matching your query about using
send_chat_message()
within a task in Prefect 2.x that results in a
RuntimeError
. However, the error you're encountering is quite specific: "Tasks cannot be run from within tasks." Based on the error message, it seems that Prefect is designed to prevent the execution of tasks within tasks, likely to enforce a clear structure of operations where tasks are the smallest unit of execution that should not contain sub-tasks. If you would like to perform an action such as sending a chat message within a task, you may need to consider a few potential workarounds: 1. Refactor your task: Reorganize your code so that the
send_chat_message()
operation is invoked after the task has completed, perhaps in a subsequent task that is dedicated to sending notifications based on the outcome of preceding tasks. 2. Use a flow-level operation: Instead of calling
send_chat_message()
within a task, use it at the flow level, where the entire flow's logic can be coordinated and notifications can be sent based on the results of tasks once they finish their execution. 3. Invoke an external script or service: If
send_chat_message()
is a function you have control over, you could refactor it to be executed as an external script or through an API call, thereby decoupling it from the Prefect task execution and avoiding nested task calls. 4. Task notifications: Look into whether Prefect 2.x has built-in mechanisms for task notifications that you can leverage rather than manually sending messages from within tasks. Please note that without more specific details or code examples, these are general suggestions. If none of these solutions address your situation, you may consider reaching out directly to the Prefect community or opening a new issue in the Prefect GitHub repository with a detailed description of your use case and the challenges you're facing.
k
you can do
send_chat_message.fn()
to call the the function as if it wasn't decorated with
@task
👌 1
🙌 1
j
🦾 Awesome thank you Kevin that’s exactly what I wanted to do.
@Kevin Grismore looks like send_chat_message.fn() doesn’t work, no errors but also no slack messages. Confirmed my syntax is proper by testing outside a task and removing .fn as well
Copy code
from prefect import flow, task, get_run_logger
from prefect_slack.messages import send_chat_message
from prefect_slack import SlackCredentials

slack_credentials = SlackCredentials.load("creds")

flow_name = "flow-from-flow"

@task
def slack_test():
    # this does not work
    send_chat_message.fn(
        slack_credentials=slack_credentials,
        channel="#example-channel",
        text="First task example"
    )

    return "placeholder_value1"

@flow(name=flow_name, log_prints=True)
def flow_from_flow():
    logger = get_run_logger()

    flow1_var = slack_test()

    # this works
    send_chat_message(
        slack_credentials=slack_credentials,
        channel="#example-channel",
        text="working"
    )

if __name__ == "__main__":
    flow_from_flow()
j
@Jey Farmer , it looks it is not working due to being async function/awaitable. This says
fn
basically reverts from prefect engine function to basic python async function, and you'd need await then when using fn(). I think
upvote 1