Custom Nodes
In this guide, we will walk through the process of creating custom nodes for Vellum Workflows using the Workflows SDK. Custom nodes allow you to extend the functionality of your Workflows by implementing specific logic tailored to your needs.
Overview
In this tutorial, we will create a simple workflow that:
- Fetches fake social media posts from an API
- Maintains strong types by validating the API response with Pydantic, via the Workflows SDK
UniversalBaseModel
- Depending on runtime inputs, also fetches & appends post author data
- Passes the data to a Prompt Node to summarize the results using a language model.
Components
1. workflow.py
This file defines the main workflow. The workflow consists of a graph that connects a CustomNode
(our data fetcher) to a SummarizeResultsNode
. The Outputs
class specifies the expected output of the workflow.
2. custom_node.py
In custom_node.py
we define a CustomNode
that extends BaseNode
. The run
method fetches fake social media posts from an API and, depending on the include_user_details
input, fetches user details from a separate API and returns the results. This node demonstrates how you can use the run
method to define custom runtime logic based on the node’s inputs.
In types.py
we define response types using UniversalBaseModel
(which inherits from Pydantic) to achieve static type checking & type inference throughout our Workflow. We also use pydash
to coerce our response types from camel case to snake case, to achieve consistency with the code conventions we prefer (and simultaneously, make our linter happy).
3. summarize_results_node.py
This file defines a SummarizeResultsNode
that extends InlinePromptNode
. It uses GPT-4o-mini to summarize the posts data fetched by the CustomNode
. The node is configured with prompt blocks that define how the input data should be processed.
Running the Workflow
To execute the workflow, create a script.py
like the one below, and run it via python script.py
. The workflow will fetch the social media posts, process them through the SummarizeResultsNode
, and log the summary.
Output:
Conclusion
By following this guide, you have learned how to extend BaseNode
to create custom nodes in Vellum Workflows. This approach allows you to tailor workflows to your specific requirements, integrating external data sources and processing them with advanced language models.