Step Decorators API Reference¶
The step decorators provide a way to define workflow steps using Python decorators like @think, @act, @observe, and @synthesize.
Function Definitions¶
act = create_step_decorator(StepType.ACT)
module-attribute
¶
Decorator for creating action/execution steps.
Example
# Standard usage
@act("process", description="Processes analyzed data")
def process_data(self, analysis: str) -> str:
return f"Processing results: {analysis}"
# With validation
from pydantic import BaseModel
class ActionResult(BaseModel):
action_taken: str
success: bool
details: str
@act(
"process",
json_output=True,
return_type=ActionResult
)
def process_data(self, analysis: str) -> ActionResult:
return ActionResult(
action_taken="data_cleanup",
success=True,
details="Processed and normalized data"
)
observe = create_step_decorator(StepType.OBSERVE)
module-attribute
¶
Decorator for creating observation/data gathering steps.
Example
# Standard usage
@observe("gather", description="Gathers input data")
def gather_data(self, query: str) -> str:
return f"Gathering data for: {query}"
# With validation
from pydantic import BaseModel
from typing import List
class Observation(BaseModel):
data_points: List[float]
timestamp: str
source: str
@observe(
"gather",
json_output=True,
return_type=Observation
)
def gather_data(self, query: str) -> Observation:
return Observation(
data_points=[1.2, 3.4, 5.6],
timestamp="2024-01-01T12:00:00",
source="sensor_array"
)
synthesize = create_step_decorator(StepType.SYNTHESIZE)
module-attribute
¶
Decorator for creating synthesis/summary steps.
Example
# Standard usage
@synthesize("summarize", description="Summarizes results")
def summarize_data(self, data: str) -> str:
return f"Summary of: {data}"
# With validation
from pydantic import BaseModel
from typing import List, Dict
class Summary(BaseModel):
key_findings: List[str]
metrics: Dict[str, float]
conclusion: str
@synthesize(
"summarize",
json_output=True,
return_type=Summary
)
def summarize_data(self, data: str) -> Summary:
return Summary(
key_findings=["Finding 1", "Finding 2"],
metrics={"accuracy": 0.95, "confidence": 0.87},
conclusion="Data shows positive trends"
)
think = create_step_decorator(StepType.THINK)
module-attribute
¶
Decorator for creating thinking/analysis steps.
Example
@think("analyze", description="Analyzes input data")
def analyze_data(self, data: str) -> str:
return f"Analysis task: {data}"
# Standard usage
@think("analyze", description="Analyzes input data")
def analyze_data(self, data: str) -> str:
return f"Analysis task: {data}"
# With validation
from pydantic import BaseModel
class Analysis(BaseModel):
score: float
findings: str
@think(
"analyze",
json_output=True,
return_type=Analysis
)
def analyze_data(self, data: str) -> Analysis:
return Analysis(score=0.8, findings="Found X")
BoundRunFunction
¶
Run function bound to a specific agent instance.
Maintains the binding between a run function and its agent instance while preserving method attributes and proper execution context.
Attributes:
Name | Type | Description |
---|---|---|
func |
The original function to be bound |
|
instance |
The agent instance to bind to |
|
_is_run |
Flag indicating this is a run method |
|
_run_description |
Optional[str]
|
Optional description of the run behavior |
Example
Create bound function:
Source code in clientai/agent/steps/decorators.py
__call__(*args, **kwargs)
¶
Execute the function with the bound instance.
Automatically prepends the bound instance as the first argument (self) when calling the wrapped function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
Any
|
Keyword arguments to pass to the function. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The result of executing the bound function. |
Source code in clientai/agent/steps/decorators.py
__init__(func, instance)
¶
Initialize a bound run function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[..., Any]
|
The function to bind. |
required |
instance
|
Any
|
The instance to bind the function to. |
required |
Source code in clientai/agent/steps/decorators.py
RunFunction
¶
A wrapper class for custom run methods in agents.
This class implements Python's descriptor protocol to enable proper method binding when the wrapped function is accessed as a class attribute. It ensures that the function behaves correctly as an instance method while maintaining its custom attributes and metadata.
Attributes:
Name | Type | Description |
---|---|---|
func |
The original run function being wrapped. |
|
_is_run |
Flag indicating this is a run method. |
|
_run_description |
Optional[str]
|
Optional description of the run behavior. |
Source code in clientai/agent/steps/decorators.py
__call__(*args, **kwargs)
¶
Execute the wrapped run function directly.
Note: This method is typically only called when the descriptor is used without being bound to an instance, which should raise a TypeError through get.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
Any
|
Keyword arguments to pass to the function. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The result of the run function execution. |
Source code in clientai/agent/steps/decorators.py
__get__(obj, objtype=None)
¶
Support descriptor protocol for instance method binding.
Implements Python's descriptor protocol to create bound methods when the function is accessed through an instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The instance that the method is being accessed from. |
required |
objtype
|
Optional[type]
|
The type of the instance (not used). |
None
|
Returns:
Name | Type | Description |
---|---|---|
BoundRunFunction |
BoundRunFunction
|
A bound version of the run function. |
Raises:
Type | Description |
---|---|
TypeError
|
If accessed without an instance (obj is None). |
Source code in clientai/agent/steps/decorators.py
__init__(func)
¶
Initialize the run function wrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[..., Any]
|
The run function to wrap. |
required |
Source code in clientai/agent/steps/decorators.py
StepFunction
¶
Bases: Generic[T]
A wrapper class for step functions that maintains metadata and execution context.
Wraps agent step functions while preserving their metadata and allowing attachment of additional step information through the _step_info attribute.
Attributes:
Name | Type | Description |
---|---|---|
func |
The original step function being wrapped |
|
_step_info |
Optional[Step]
|
Optional Step instance containing step metadata |
Example
Create a wrapped step function:
Source code in clientai/agent/steps/decorators.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
__call__(instance, *args, **kwargs)
¶
Execute the step function with instance binding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance
|
Optional[Any]
|
The agent instance the step is being called from. If None, executes the raw function without engine involvement. |
required |
*args
|
Any
|
Positional arguments to pass to the step |
()
|
**kwargs
|
Any
|
Keyword arguments to pass to the step |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
Either: - Result from execution_engine if called from instance with step info - Raw function result if called without instance or step info |
Example
When called from an agent instance:
When called directly:
Source code in clientai/agent/steps/decorators.py
__get__(instance, owner)
¶
Make steps behave like instance methods via the descriptor protocol.
When a step is accessed on an agent instance, returns a bound method that automatically passes the instance through call for engine execution. When accessed on the class, returns the StepFunction itself.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance
|
Optional[object]
|
The agent instance accessing the step. None if accessed on class. |
required |
owner
|
Optional[type]
|
The agent class the step is defined on. |
required |
Returns:
Type | Description |
---|---|
Union[StepFunction[T], Callable[..., T]]
|
Union[StepFunction, Callable[..., str]]: - If accessed on class (instance=None), returns the StepFunction itself - If accessed on instance, returns a callable that passes the instance through call for engine-managed execution |
Example
Source code in clientai/agent/steps/decorators.py
__init__(func)
¶
Initialize the step function wrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[..., T]
|
The step function to wrap. |
required |
create_step_decorator(step_type)
¶
Generate a decorator for defining workflow steps of a specific type.
Creates specialized decorators (like @think, @act) that mark methods as workflow steps with specific configurations and types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step_type
|
StepType
|
The type of step (THINK, ACT, OBSERVE, SYNTHESIZE) this decorator creates |
required |
Returns:
Type | Description |
---|---|
Callable[..., Union[StepFunction[Union[str, Iterator[str], Any]], Callable[[Callable[..., Union[str, Iterator[str], Any]]], StepFunction[Union[str, Iterator[str], Any]]]]]
|
A decorator function that can be used to mark methods as workflow steps |
Example
Create custom step decorator:
Notes
- Generated decorators support both parameterized and bare usage
- Decorators handle both tool selection and LLM configuration
- Step type influences default configuration values
Source code in clientai/agent/steps/decorators.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
|
run(func=None, *, description=None)
¶
Decorator for defining a custom run
method in an agent class.
Marks a method as the custom run implementation for an agent, optionally with a description of its behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Optional[Callable[..., T]]
|
The function to decorate (when used without parameters) |
None
|
description
|
Optional[str]
|
Optional description of the custom run behavior |
None
|
Returns:
Type | Description |
---|---|
Union[Callable[[Callable[..., T]], RunFunction], RunFunction]
|
Either a decorator function or the decorated function |
Example
Define custom run methods: