Implementation:Neuml Txtai ServiceTask
| Knowledge Sources | |
|---|---|
| Domains | Workflow, HTTP, API Integration |
| Last Updated | 2026-02-10 01:00 GMT |
Overview
Concrete tool for executing HTTP requests against remote service APIs within workflows provided by txtai.
Description
The ServiceTask class extends the base Task class to make HTTP GET or POST requests against remote service URLs. It supports both batch mode (all elements sent in a single request) and per-element mode (one request per element). The task handles both JSON and XML response parsing automatically based on the response Content-Type header, using xmltodict for XML responses. A configurable extraction path allows drilling into nested response structures to pull out specific data sections. Default query parameters can be merged with dynamic element data, where None-valued parameters are replaced with the input data.
Usage
Use ServiceTask in txtai workflows when you need to call external REST APIs or web services as part of a processing pipeline. This is useful for enriching data with external lookups, calling third-party NLP services, or integrating with existing microservice architectures. Configure it with the target URL, HTTP method, default parameters, batch mode, and optional response extraction path.
Code Reference
Source Location
- Repository: Neuml_Txtai
- File: src/python/txtai/workflow/task/service.py
Signature
class ServiceTask(Task):
def register(self, url=None, method=None, params=None, batch=True, extract=None)
def execute(self, elements, executor=None)
def request(self, data)
Import
from txtai.workflow.task.service import ServiceTask
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| elements | list | Yes | List of data elements to send to the remote service |
| executor | object | No | Optional executor instance for concurrent task actions |
| url | str | Yes (register) | URL of the remote service endpoint |
| method | str | No (register) | HTTP method: "get" or "post"; defaults to POST if not specified |
| params | dict | No (register) | Default query parameters; keys with None values are replaced with dynamic input data |
| batch | bool | No (register) | If True (default), all elements are sent in a single request; if False, one request per element |
| extract | str or list | No (register) | Key path(s) to extract from the response; supports nested extraction via list of keys |
Outputs
| Name | Type | Description |
|---|---|---|
| results | list | Parsed response data (JSON or XML converted to dict), optionally extracted to the specified key path |
Usage Examples
from txtai.workflow.task.service import ServiceTask
# POST request with batch mode (all elements in one request)
service = ServiceTask(
url="https://api.example.com/analyze",
method="post",
batch=True,
extract=["results", "data"]
)
results = service(["text one", "text two", "text three"])
# GET request with default parameters, per-element mode
service = ServiceTask(
url="https://api.example.com/lookup",
method="get",
params={"query": None, "format": "json"},
batch=False,
extract="response"
)
# Each element replaces the None-valued "query" parameter
results = service(["term1", "term2"])
# XML API integration
xml_service = ServiceTask(
url="https://api.example.com/xml-endpoint",
method="get",
params={"q": None},
batch=False,
extract=["root", "items", "item"]
)
results = xml_service(["search query"])