A Worklet is a directed acyclic graph of code and model execution that is backed by an API endpoint.
Worklets are where you define your application's business logic. A Worklet is a directed acyclic graph of code and model execution that is backed by an API endpoint. It allows you to think in terms of your application's business logic and flow, rather than worrying about Kubernetes, Docker, Flask, and DB instances. Once you create your worklet, you can then invoke it through its auto-generated API endpoint or a view component, or set it to run on a fixed schedule.
Worklets are composed of blocks of different types, including Code, Model, and Decision blocks, plus actions such as a Slack block.
In the example below, we want to operationalize a zero-shot classification model. The worklet receives English text as input, pre-processes it, runs the model, and stores the results in the database.
A Worklet graph containing a Model block and two Code blocks

How does data flow through a worklet?

As the flow suggests, the output of each block is fed as the input to the next block. The worklet's input becomes the first block's input, and the final block's output is the complete worklet's output.
Inputs and outputs must be JSON-serializable. For instance, dicts, lists, strings, numbers, and booleans are all valid. However, each node can only take a single input, so for named parameters a dict is usually the best option.

How are worklets invoked?

While developing and testing, the easiest way to invoke the worklet is through the Baseten UI. See testing for more.
You can invoke a worklet in three different ways:
  1. 1.
    You can send a request to a worklet from a view component like a button, and assign its input and output to various view components.
  2. 2.
    You can bind the worklet to the data value of a component (such as Table or JSON Viewer) and assigns its input to various view components. This will result in the worklet being run reactively in response to changes to any input value.
  3. 3.
    You can call the worklet through its auto-generated API endpoint. An example of the worklet invocation API usage is present on the start node of the worklet.
  4. 4.
    And finally you can set the worklet to execute on a set schedule. To schedule a worklet, use the "Settings" button in the start block, enable the "Schedule this worklet" option then choose a frequency and, if necessary, UTC time for the worklet to run.
The first invocation of a worklet can take a few seconds to wake up.

Managing worklets

To create a new worklet, click the "+" button on the left-side worklets menu. You can immediately rename the newly created worklet, and if you change your mind you can rename it later by clicking the three dots next to the worklet to open up the menu for that worklet.
For every worklet, you can:
  • Rename: The new name will be how you refer to the worklet in the Application, but will not change how you call it via the API.
  • Clone: Create an exact copy of the worklet in the application.
  • Delete: Delete the worklet. Any associated Files and Models will not be affected, but Views that rely on the worklet may break, as will any worklets and APIs that call the deleted worklet. Carefully ensure the worklet is totally unneeded before deleting it.

Testing worklets

When I write code, it works perfectly the first time, every time ... said no one ever. To test and debug your worklets, click the "Testing" tab in the bottom navigation menu.
Select the worklet you want to test from the dropdown. If you want to run just a single block, select it in the next dropdown. Selecting "Start" will run the full worklet.
The testing tab
This two-paned tab accepts an input in the left pane and prints worklet output and logs in the right pane. When you're ready, click the "Run" button to run the worklet or block.


Your input must be JSON-serializable.
Example valid inputs:
  • "this is a string"
  • 12345
  • [1, 2, 3, 4]
  • {"this is": "JSON"}
Example invalid inputs:
  • these are random words but not a string
  • 1, 2, 3, 4, 5
  • 1 + 2
  • {"this is": "would be fine except", "the JSON is invalid"; "because semicolon"}
Calling this "input," not "inputs," is intentional. Worklets assume you only give one input, and the function signatures for code blocks use positional arguments. If you need multiple variables passed into a function, that's what the JSON is for; create a dictionary with as many keys as necessary.

Output logging

While debugging a Code or Decision block, it might be handy to log some information. You can use Python's print() statements.

Worklet run logs

Every time a worklet is run, it generates a log entry. These logs are available in the "Logs" tab at the bottom of the page. Each log contains a timestamp, application and run ids, and the worklet run's output, state, and logs. Write to worklet logs from code blocks with the print() function.
The logs tab
Logs are fully searchable by string or regular expression, and can be filtered to any time range. Default ranges from the past five minutes to the past week are built-in, but you can use the calendar to select logs from any date.