Argo Workflows is widely used for scheduled tasks, machine learning, and ETL, but YAML definitions can be hard to learn. The Hera Python SDK lets you build workflows in Python—supporting complex tasks, easier testing, and seamless Python ecosystem integration. This topic explains how to use the Python SDK to build large-scale Argo Workflows.
Background
Argo Workflows is an open-source Kubernetes-native tool for orchestrating complex workflows with flexible task dependencies.
Use cases include scheduled tasks, machine learning, simulation, scientific computing, ETL, model training, and CI/CD. Argo Workflows uses YAML to define workflows, but YAML's strict indentation and hierarchical structure create a steep learning curve for complex workflows.
Hera is a Python SDK for building and submitting Argo Workflows, letting data scientists work in Python instead of YAML.
Authoring method comparison
|
|
YAML |
Hera |
|
Simplicity |
High |
High, fewer lines of code |
|
Writing complex workflows |
Difficult |
Easy. Avoids YAML syntax errors. |
|
Python ecosystem integration |
Difficult |
Easy. Access to Python libraries. |
|
Testability |
Difficult, prone to syntax errors |
Easy. Testing frameworks improve code quality and maintainability. |
Hera bridges the Python ecosystem and Argo Workflows, enabling large-scale task orchestration without YAML complexity. The following examples use Hera.
Create a cluster and get a token
-
Create an Argo workflow cluster, then enable Argo Server and access the workflow console.
-
Create a cluster token.
kubectl create token default -n default
Submit workflows with Hera
-
Install Hera.
pip install hera -
Write and submit the workflows.
Simple DAG diamond
A DAG diamond is a common pattern where tasks diverge and converge. In this example, Task A runs first, then B and C run in parallel, and Task D runs after both complete.
-
Create a file named
simpleDAG.pywith the following content.# Import the required packages. from hera.workflows import DAG, Workflow, script from hera.shared import global_config import urllib3 urllib3.disable_warnings() # Configure the host address and token. global_config.host = "https://{{argo_server_IP}}:2746" global_config.token = "abcdefgxxxxxx" # Replace with the token you obtained. global_config.verify_ssl = "" # The @script decorator is a key Hera feature that lets you orchestrate Python functions almost natively. # It allows you to call the decorated function within a Hera context manager, such as a Workflow or Steps context. # The function still runs normally outside of any Hera context, which means you can write unit tests for it. # This example prints the input message. @script(image="mirrors-ssl.aliyuncs.com/python:3.10") def echo(message: str): print(message) # A Workflow is the primary resource in Argo and a key class in Hera. It stores templates, sets an entrypoint, and runs them. with Workflow( generate_name="dag-diamond-", entrypoint="diamond", namespace="default", ) as w: with DAG(name="diamond"): A = echo(name="A", arguments={"message": "A"}) # Build a template. B = echo(name="B", arguments={"message": "B"}) C = echo(name="C", arguments={"message": "C"}) D = echo(name="D", arguments={"message": "D"}) A >> [B, C] >> D # Define the dependencies: Tasks B and C depend on A, and task D depends on B and C. # Create the workflow. w.create() -
Submit the workflow.
python simpleDAG.py -
View the task DAG and results in the workflow console.
The workflow
dag-diamond-g9v45shows a diamond DAG: node A completes, B and C run in parallel, then converge at D. All nodes show successful execution.
Map-reduce
This example uses DAG templates to implement MapReduce: split a task into parallel map steps, then aggregate results in a reduce step. Each step is a Python function.
-
Create a file named
map-reduce.pywith the following content. -
Submit the workflow.
python map-reduce.py -
View the task DAG and results in the workflow console. On the WORKFLOW DETAILS page, the DAG shows the
splitnode, four parallelmapnodes, and thereducenode all executed successfully (green check mark).
-
References
-
Hera documentation:
-
YAML deployment examples:
-
Simple diamond (YAML): dag-diamond.yaml
-
Map-reduce (YAML): map-reduce.yaml
-