How to handle multi-step branches

Hi team! I'm converting an N8N workflow to Mastra and struggling with how to properly implement branching logic where each branch needs to execute multiple steps before merging back to the main workflow. Use Case: I'm processing inbound emails and need to branch based on security checks:
Email Processing Workflow:

START

[Step 1] Receive Email

[Step 2] Security Check

├─── IF Security Issues Detected:
│ ↓
│ [Branch A1] Log Security Incident
│ ↓
│ [Branch A2] Quarantine Email
│ ↓
│ [Branch A3] Send Security Alert
│ ↓
│ [Merge Point]

└─── IF No Security Issues:

[Branch B1] Validate Sender

[Branch B2] Extract Email Content

[Branch B3] Classify Email Type

[Branch B4] Process Content

[Branch B5] Update Database

[Merge Point]

[Step 3] Send Confirmation/Response

END
Email Processing Workflow:

START

[Step 1] Receive Email

[Step 2] Security Check

├─── IF Security Issues Detected:
│ ↓
│ [Branch A1] Log Security Incident
│ ↓
│ [Branch A2] Quarantine Email
│ ↓
│ [Branch A3] Send Security Alert
│ ↓
│ [Merge Point]

└─── IF No Security Issues:

[Branch B1] Validate Sender

[Branch B2] Extract Email Content

[Branch B3] Classify Email Type

[Branch B4] Process Content

[Branch B5] Update Database

[Merge Point]

[Step 3] Send Confirmation/Response

END
What I've Tried: Using Workflow.branch() - but the documentation and examples only show single-step branches (just a then: async () => {} function) Creating sub-workflows - but this creates a poor UX in Mastra Studio as I can't clearly see the results of sub-workflows when building/debugging Questions: Is there a way to define multiple sequential steps within a branch before merging back to the main flow? Should I be nesting all the logic inside the branch's then function, or is there a cleaner pattern? Are there any examples of complex branching workflows with multiple steps per branch? Any guidance or examples would be really helpful! Coming from N8N where this is visually straightforward, so looking for the Mastra equivalent pattern.
4 Replies
Sebastian
Sebastian3w ago
Just my 2 cents: I think sub workflows are the most correct way from a process modelling perspective. The poor UX in Mastra Studio you described might get improvements down the line, but hopefully won't be a roadblock. I dealt with some BPMN heavy topics in the past and this scenario would translate to a Subprocess or a Call Activity. A subprocess would behave as an expandable sub node in many BPMN tools while a Call Activity would rather be a link to an independent execution. The latter should be easier to implement and have a nicer UX in complex scenarios. It should also fit nicely with the implementation used in Mastra Studio. I'm not quite at that point of complexity, but I also plan to implement workflows with nested structures so a more flexible Mastra Studio implementation would certainly help me with that in the future.
Mastra Triager
📝 Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10383 🔍 If you're experiencing an error, please provide a minimal reproducible example whenever possible to help us resolve it quickly. 🙏 Thank you for helping us improve Mastra!
levi.putna
levi.putnaOP3w ago
I think the approach is to use sub-workflows run within a .branches() structure. There’s a small “View Nested Graph” button on the node that can be clicked to open the sub-workflow in a dialog. It’s a bit small and not an ideal user experience, but it does work. Additionally, you can open the sub-workflow independently while working on it, allowing you to run your own tests separately from the parent workflow. This is a nice experience during development. Ideally, it would be good to see the entire workflow or open the full graph view when clicking into a sub-workflow, even if it opens in another tab. But overall, this seems like a solid starting point and gives me confidence that this is the right approach for handling complex sub-workflow processes.
Grayson
Grayson3w ago
Yes, use nested workflows as steps! Create separate workflows for each branch, then use them as steps. We definitely plan on improving the UI for displaying these complex use cases. Here is a gist as an example: https://gist.github.com/graysonhicks/aa33caa507ca62add7f616e29abdd086
Gist
Nested Workflows or Sequential Steps Within Branches
Nested Workflows or Sequential Steps Within Branches - complex-branching.ts

Did you find this page helpful?