<@ULVA73B9P> I want to create flow-runs from my de...
# ask-marvin
j
@Marvin I want to create flow-runs from my deployment on the UI. I have setup a Pydantic BaseModel to fail when I supply an incorrect combination of parameters. This does fail, but only after the flow run was created. Is there a mechanism to prevent myself from making the flow run at all?
m
In Prefect 3.x, parameters are validated before a flow is run, not before the flow run is created. If a flow run for a deployment receives invalid parameters, it transitions from a "Pending" state to a "Failed" state without entering a "Running" state. This means that while you can catch parameter validation errors, the flow run is still technically created, albeit in a failed state. Unfortunately, there isn't a built-in mechanism in Prefect to prevent the creation of a flow run entirely based on parameter validation. You could, however, implement a pre-check in your code before triggering the flow run. This would involve validating the parameters using your Pydantic model or another validation method before calling the API to create the flow run. For more details, you can refer to the Prefect documentation on writing flows. If you want to explore the Prefect API further, you can check out the top-level API, which includes useful functions like
flow
,
task
, and others. Here's the Prefect API documentation for more information. It seems you're using PyO3 for Python/Rust integration and prefer an imperative programming style, which might mean you're looking for a more programmatic solution. Unfortunately, without altering the Prefect core behavior, the workaround I mentioned is your best bet.
j
@Marvin is there a mechanism to immediately swap the current page to the newly created flow run?
m
There doesn't appear to be a direct mechanism within Prefect's UI or API to automatically redirect or swap the current page to the newly created flow run. However, you can manually navigate to the flow run page using the Prefect UI URL. When you create a flow run, you can use the Prefect Client to retrieve the flow run's ID. With this ID, you can construct a URL to the specific flow run in the Prefect UI. This URL can then be opened in a web browser to view the flow run details. Here's a conceptual approach: 1. Create the Flow Run: Use the Prefect Client to create a flow run. 2. Retrieve Flow Run ID: Capture the flow run ID from the response. 3. Construct URL: Use the flow run ID to construct the URL to the flow run in the Prefect UI. 4. Open URL: Use a web browser or a redirect mechanism in your application to open the URL. This approach requires some manual steps or additional scripting on your part, but it allows you to directly access the flow run page after creation. For more details on working with flow runs, you may refer to the Prefect documentation. If you're looking to implement this programmatically, you might need to integrate with your front-end or use a script to handle the redirection.
j
@Marvin The failure triggers properly, but it is not reported in the logs of that flow run. Is there some configuration I need to do on my flow or my pydantic BaseModel?
m
To ensure that parameter validation errors are logged in your flow run logs, consider the following steps: 1. Increase Logging Level: Ensure that your logging level is set to
DEBUG
to capture detailed logs, including errors. You can set this using environment variables or the Prefect CLI:
Copy code
bash
   prefect config set PREFECT_LOGGING_LEVEL=DEBUG
This setting will propagate from the worker environment to the flow run environment. 2. Custom Logging for Pydantic: If your Pydantic model raises validation errors, you can catch these exceptions and log them manually using Prefect's logging utilities. Here's a conceptual example:
Copy code
python
   from prefect import flow, get_run_logger
   from pydantic import BaseModel, ValidationError

   class MyModel(BaseModel):
       param1: int
       param2: str

   @flow
   def my_flow(params: dict):
       logger = get_run_logger()
       try:
           validated_params = MyModel(**params)
       except ValidationError as e:
           logger.error(f"Validation error: {e}")
           raise

   # Example usage
   my_flow({"param1": "not an int", "param2": "a string"})
3. Check Worker Logs: If your flow and task logs are empty, there might be an infrastructure issue that prevented logging. Check your worker logs for more details. For more information on troubleshooting and managing Prefect logs, you can refer to the Prefect troubleshooting documentation.