Robocorp Work Items
The `robocorp-workitems` library is part of the Robocorp ecosystem, providing utilities to manage data flow between tasks in a Robotic Process Automation (RPA) workflow. It allows robots to read input work items and write output work items, facilitating the transfer of structured data, files, and assets. The current version is 1.5.0, and it's actively maintained as part of the broader Robocorp platform.
Common errors
-
AttributeError: 'NoneType' object has no attribute 'get_payload'
cause Attempting to access `current.get_payload()` when `current` is `None`. This typically happens when the `robocorp-workitems` library is used outside a Robocorp task execution context without simulating input data.fixExecute your robot using `rc task run` or within Robocorp Control Room. For local development, create a `devdata/work-items-in/work-item-1/work-item.json` file to simulate an input work item. -
ModuleNotFoundError: No module named 'RPA.Robocorp.WorkItems'
cause This error indicates that the project is trying to use the deprecated `RPA.Robocorp.WorkItems` library (part of `rpaframework`) while `robocorp-workitems` is installed, or the `rpaframework` is not installed.fixUpdate your import statements from `from RPA.Robocorp.WorkItems import ...` to `from robocorp.workitems import ...` and refactor your code to use the new API. If you specifically need `rpaframework`, ensure it is installed (`pip install rpaframework`). -
TypeError: Object of type <YourCustomClass> is not JSON serializable
cause You attempted to add an instance of a custom Python class, a `datetime` object, or another non-JSON-serializable data type directly into a work item payload.fixBefore setting the payload, convert your custom objects or `datetime` objects into basic Python data structures (e.g., dictionaries, lists, strings) that are JSON serializable (e.g., `dt_object.isoformat()`).
Warnings
- breaking The `robocorp-workitems` library is the modern replacement for the older `RPA.Robocorp.WorkItems` from the `rpaframework`. Direct migration is needed as the API has changed significantly.
- gotcha The `robocorp.workitems.current` object (and `inputs.current`, `outputs.create()`) relies on a Robocorp execution environment. When running a Python script directly without `rc task run` or a properly configured `devdata` folder, `current` might be `None` or operations might fail.
- gotcha Payloads for work items must be JSON serializable. Attempting to add non-serializable objects (e.g., custom class instances, datetime objects without conversion) directly to the payload will result in a serialization error.
Install
-
pip install robocorp-workitems
Imports
- current
from robocorp.workitems import current
- inputs
from robocorp.workitems import inputs
- outputs
from robocorp.workitems import outputs
- WorkItems
from RPA.Robocorp.WorkItems import WorkItems
from robocorp.workitems import current
Quickstart
from robocorp.tasks import task
from robocorp.workitems import inputs, outputs
@task
def process_data():
# Read current input work item's payload
input_item = inputs.current
if input_item.get_payload('my_key'):
data = input_item.get_payload('my_key')
print(f"Received data: {data}")
else:
print("No 'my_key' found in input payload. Using default.")
data = {"message": "Hello from robot!"}
# Create an output work item
output_item = outputs.create()
# Add a payload to the output work item
output_item.set_payload({"status": "processed", "original_data": data})
# You can also add files
# output_item.add_file('path/to/my_file.txt')
# Save the output work item
output_item.save()
print("Output work item created and saved.")