@task

Overview

The @task decorator creates a child span for a single unit of work — an LLM call, data processing step, validation, or any discrete operation within a workflow.

1from respan import task

Parameters

ParameterTypeDefaultDescription
namestr | NoneFunction nameDisplay name for the task span.
versionint | NoneNoneVersion number for the task.
method_namestr | NoneNoneRequired when decorating a class. Specifies which method to use as the entry point.
processorsstr | List[str] | NoneNoneRoute this span to specific named processors. See add_processor.
export_filterFilterParamDict | NoneNoneFilter dict to control which spans are exported. Uses AND logic — all conditions must match.

Function usage

1from respan import Respan, workflow, task
2from openai import OpenAI
3
4respan = Respan(api_key="your-api-key")
5client = OpenAI()
6
7@task(name="generate_text")
8def generate_text(prompt: str):
9 response = client.chat.completions.create(
10 model="gpt-4o-mini",
11 messages=[{"role": "user", "content": prompt}],
12 )
13 return response.choices[0].message.content
14
15@task(name="summarize")
16def summarize(text: str):
17 response = client.chat.completions.create(
18 model="gpt-4o-mini",
19 messages=[{"role": "user", "content": f"Summarize: {text}"}],
20 )
21 return response.choices[0].message.content
22
23@workflow(name="content_pipeline")
24def content_pipeline(topic: str):
25 text = generate_text(f"Write about {topic}")
26 return summarize(text)
27
28print(content_pipeline("Python"))

Class usage

1from respan import Respan, workflow, task
2
3respan = Respan(api_key="your-api-key")
4
5@workflow(name="agent_run", method_name="run")
6class Agent:
7 @task(name="respond")
8 def respond(self, prompt: str):
9 return f"Echo: {prompt}"
10
11 def run(self):
12 return self.respond("Hello")
13
14print(Agent().run())

Features

  • Automatic I/O capture — Function arguments and return values are serialized as span input/output (up to 1MB).
  • Exception recording — Exceptions are automatically recorded on the span with error status.
  • Async support — Works with async def functions and generators.

Best practices

  • Keep tasks small and focused on a single operation
  • Use task names that reflect intent, not implementation details
  • Nest tasks inside workflows for proper trace hierarchy