Skip to content

Agentic RL Fine-tuning

dashscope.applications.finetune hosts the Agentic RL fine-tuning workflow: submit training jobs built from your own rollout / reward functions. Job, dataset and function management on the service side lives under client.rl (see Resources); this page covers the local submission entry point and the authoring API for reward / rollout components.

Submission entry point

AgenticRL

AgenticRL(api_key: str = None)

Bases: AgenticRLTuning

Source code in dashscope/applications/finetune/agentic_rl.py
136
137
138
139
140
141
142
143
144
145
146
147
def __init__(self, api_key: str = None):
    super().__init__()

    try:
        set_api_key(api_key)
    except Exception as e:
        raise ValueErrorWithCode.from_catalog(
            "sdk.InvalidApiKey",
            error_code=3001,
        ) from e

    self._api_key = api_key

SUB_PATH class-attribute

SUB_PATH: str = 'fine-tunes'

init

init(config_path: Optional[str] = None, **kwargs) -> Self

Initialize an AgenticRL instance from a YAML configuration file.

Source code in dashscope/applications/finetune/agentic_rl.py
153
154
155
156
157
158
159
def init(self, config_path: Optional[str] = None, **kwargs) -> Self:
    """
    Initialize an AgenticRL instance from a YAML configuration file.
    """
    self.tuning = TuningModel.load_from_yaml(config_path or "", **kwargs)

    return self

register_functions async

register_functions(
    functions: Optional[
        Union[
            List[
                Union[
                    RolloutFunctionComponent,
                    RewardFunctionComponent,
                ]
            ],
            RolloutFunctionComponent,
            RewardFunctionComponent,
        ]
    ] = None,
    lazy_load: Optional[bool] = True,
) -> tuple[
    List[str],
    List[str],
    List[str],
    List[str],
    List[str],
    List[str],
]

Register function components and return entity/instance IDs.

Source code in dashscope/applications/finetune/agentic_rl.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
async def register_functions(
    self,
    functions: Optional[
        Union[
            List[Union[RolloutFunctionComponent, RewardFunctionComponent]],
            RolloutFunctionComponent,
            RewardFunctionComponent,
        ]
    ] = None,
    lazy_load: Optional[bool] = True,
) -> tuple[
    List[str],
    List[str],
    List[str],
    List[str],
    List[str],
    List[str],
]:
    """Register function components and return entity/instance IDs."""
    if functions:
        self.tuning.functions = functions

    try:
        (
            rollout_entity_ids,
            reward_entity_ids,
            group_reward_entity_ids,
            rollout_instance_ids,
            reward_instance_ids,
            group_reward_instance_ids,
        ) = await self.tuning.register_functions(
            lazy_load=lazy_load,
            api_key=self._api_key,
        )
        logger.info("Function components registered")
    except Exception as e:
        if hasattr(e, "error_code"):
            raise
        raise RegistrationError.from_catalog(
            "sdk.agentic_rl.FunctionRegistrationFailed",
            error_code=3002,
            variables={"inner_code": _inner_code(e)},
        ) from e

    return (
        rollout_entity_ids,
        reward_entity_ids,
        group_reward_entity_ids,
        rollout_instance_ids,
        reward_instance_ids,
        group_reward_instance_ids,
    )

upload_datasets async

upload_datasets(
    datasets: Optional[List[Dataset]] = None,
    training_files: Optional[Union[List[str], str]] = None,
    validation_files: Optional[
        Union[List[str], str]
    ] = None,
) -> tuple[List[str], List[str]]
Source code in dashscope/applications/finetune/agentic_rl.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
async def upload_datasets(
    self,
    datasets: Optional[List[Dataset]] = None,
    training_files: Optional[Union[List[str], str]] = None,
    validation_files: Optional[Union[List[str], str]] = None,
) -> tuple[List[str], List[str]]:
    if datasets:
        self.tuning.datasets = datasets

    try:
        (
            uploaded_training_ids,
            uploaded_validation_ids,
        ) = await self.tuning.upload_datasets(
            training_files=training_files or [],
            validation_files=validation_files or [],
            api_key=self._resolve_api_key(),
        )
        logger.info("Datasets uploaded")
    except Exception as e:
        raise DatasetsError.from_catalog(
            "sdk.agentic_rl.DatasetsUploadFailed",
            error_code=3003,
            variables={"inner_code": _inner_code(e)},
        ) from e

    return uploaded_training_ids, uploaded_validation_ids

submit_job

submit_job(
    model: Optional[str] = None,
    datasets: Optional[List[Dataset]] = None,
    functions: Optional[
        Union[
            List[
                Union[
                    RolloutFunctionComponent,
                    RewardFunctionComponent,
                    AgenticRLFunctionComponent,
                ]
            ],
            RolloutFunctionComponent,
            RewardFunctionComponent,
            AgenticRLFunctionComponent,
        ]
    ] = None,
    hyper_parameters: Optional[Dict[str, str]] = None,
    resources: Optional[Dict[str, str]] = None,
    job_name: Optional[str] = None,
    teacher_model: Optional[str] = None,
    **kwargs
) -> SubmitResult

Submit RL tuning job to the platform.

Source code in dashscope/applications/finetune/agentic_rl.py
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
def submit_job(
    self,
    model: Optional[str] = None,
    datasets: Optional[List[Dataset]] = None,
    functions: Optional[
        Union[
            List[
                Union[
                    RolloutFunctionComponent,
                    RewardFunctionComponent,
                    AgenticRLFunctionComponent,
                ]
            ],
            RolloutFunctionComponent,
            RewardFunctionComponent,
            AgenticRLFunctionComponent,
        ]
    ] = None,
    hyper_parameters: Optional[Dict[str, str]] = None,
    resources: Optional[Dict[str, str]] = None,
    job_name: Optional[str] = None,
    teacher_model: Optional[str] = None,
    **kwargs,
) -> SubmitResult:
    """
    Submit RL tuning job to the platform.
    """
    return SubmitResult(
        **self._submit_envelope(
            model=model,
            datasets=datasets,
            functions=functions,
            hyper_parameters=hyper_parameters,
            resources=resources,
            job_name=job_name,
            teacher_model=teacher_model,
            **kwargs,
        ),
    )

run async

run(
    model: Optional[str] = None,
    training_datasets: Optional[
        List[TrainingDataset]
    ] = None,
    validation_datasets: Optional[
        List[ValidationDataset]
    ] = None,
    functions: Optional[
        Union[
            List[
                Union[
                    RolloutFunctionComponent,
                    RewardFunctionComponent,
                    AgenticRLFunctionComponent,
                ]
            ],
            RolloutFunctionComponent,
            RewardFunctionComponent,
            AgenticRLFunctionComponent,
        ]
    ] = None,
    hyper_parameters: Optional[Dict[str, str]] = None,
    resources: Optional[Dict[str, str]] = None,
    job_name: Optional[str] = None,
    teacher_model: Optional[str] = None,
    **kwargs
) -> SubmitResult

Execute RL tuning workflow.

Source code in dashscope/applications/finetune/agentic_rl.py
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
async def run(
    self,
    model: Optional[str] = None,
    # Datasets parameters
    training_datasets: Optional[List[TrainingDataset]] = None,
    validation_datasets: Optional[List[ValidationDataset]] = None,
    # Path-driven parameters (auto-register & upload)
    functions: Optional[
        Union[
            List[
                Union[
                    RolloutFunctionComponent,
                    RewardFunctionComponent,
                    AgenticRLFunctionComponent,
                ]
            ],
            RolloutFunctionComponent,
            RewardFunctionComponent,
            AgenticRLFunctionComponent,
        ]
    ] = None,
    # Common parameters
    hyper_parameters: Optional[Dict[str, str]] = None,
    resources: Optional[Dict[str, str]] = None,
    job_name: Optional[str] = None,
    teacher_model: Optional[str] = None,
    **kwargs,
) -> SubmitResult:
    """
    Execute RL tuning workflow.
    """
    try:
        logger.info(
            "Path-Driven mode: Registering functions & uploading "
            "datasets...",
        )
        await self.register_functions(
            functions=functions,
            lazy_load=True,
        )

        datasets = list(training_datasets or []) + list(
            validation_datasets or [],
        )
        await self.upload_datasets(
            datasets=datasets,
        )

        return self.submit_job(
            model=model,
            datasets=datasets,
            hyper_parameters=hyper_parameters,
            resources=resources,
            job_name=job_name,
            teacher_model=teacher_model,
            **kwargs,
        )
    except Exception as e:
        if hasattr(e, "error_code"):
            raise
        raise RuntimeErrorWithCode.from_catalog(
            "sdk.agentic_rl.WorkflowFailed",
            error_code=3006,
            variables={"inner_code": _inner_code(e)},
        ) from e

test_functions async classmethod

test_functions(
    instance_id: str,
    functype: FunctionType,
    input_data: Dict[str, Any],
    api_key: str = None,
    pull_logs: bool = False,
    log_page_size: int = 100,
    start_time: Optional[int] = None,
    end_time: Optional[int] = None,
)

Test a deployed function instance with custom input data.

Parameters:

Name Type Description Default
instance_id str

Target function instance ID.

required
functype FunctionType

Function type (ROLLOUT/REWARD/GROUP_REWARD).

required
input_data Dict[str, Any]

Test input payload.

required
api_key str

DashScope API key (uses DASHSCOPE_API_KEY env var if omitted).

None
pull_logs bool

If True, pull all logs of the function instance (with pagination) after verification and print them between separator markers.

False
log_page_size int

Page size used when pulling logs.

100
start_time Optional[int]

Optional start time filter (in seconds) for log pulling. Defaults to 24 hours before end_time.

None
end_time Optional[int]

Optional end time filter (in seconds) for log pulling. Defaults to the current time (now).

None
Source code in dashscope/applications/finetune/agentic_rl.py
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
@classmethod
async def test_functions(
    cls,
    instance_id: str,
    functype: FunctionType,
    input_data: Dict[str, Any],
    api_key: str = None,
    pull_logs: bool = False,
    log_page_size: int = 100,
    start_time: Optional[int] = None,
    end_time: Optional[int] = None,
):
    """Test a deployed function instance with custom input data.

    Args:
        instance_id: Target function instance ID.
        functype: Function type (ROLLOUT/REWARD/GROUP_REWARD).
        input_data: Test input payload.
        api_key: DashScope API key (uses DASHSCOPE_API_KEY env var
            if omitted).
        pull_logs: If True, pull all logs of the function instance
            (with pagination) after verification and print them
            between separator markers.
        log_page_size: Page size used when pulling logs.
        start_time: Optional start time filter (in seconds) for log
            pulling. Defaults to 24 hours before ``end_time``.
        end_time: Optional end time filter (in seconds) for log
            pulling. Defaults to the current time (now).
    """
    try:
        set_api_key(api_key)

        if functype == FunctionType.ROLLOUT:
            value = RolloutInput.model_validate(input_data)
        elif functype == FunctionType.REWARD:
            value = RewardInput.model_validate(input_data)
        elif functype == FunctionType.GROUP_REWARD:
            value = GroupRewardInput.model_validate(input_data)
        else:
            raise ValueErrorWithCode.from_catalog(
                "sdk.agentic_rl.UnsupportedFunctionType",
                error_code=3007,
                variables={"functype": str(functype)},
            )

        logger.info(
            f"Starting {str(functype)} verification",
            extra={
                "instance_id": instance_id,
                "input_params": value.model_dump(exclude={"api_key"}),
            },
        )

        result = await AgenticRLFunctionComponent.verify_function(
            value,
            instance_id,
            api_key=api_key,
        )

        if pull_logs:
            await cls._pull_function_instance_logs(
                function_instance_id=instance_id,
                page_size=log_page_size,
                start_time=start_time,
                end_time=end_time,
                api_key=api_key,
            )

        return result

    except Exception as e:
        raise ValidationError.from_catalog(
            "sdk.agentic_rl.FunctionTestFailed",
            error_code=3008,
            variables={"inner_code": _inner_code(e)},
        ) from e

query_function_instance_logs async classmethod

query_function_instance_logs(
    function_instance_id: str,
    page_number: int = 1,
    page_size: int = 100,
    start_time: Optional[int] = None,
    end_time: Optional[int] = None,
    keywords: Optional[List[str]] = None,
    api_key: str = None,
) -> Dict[str, Any]

Query one page of logs for a function (faas) instance.

Parameters:

Name Type Description Default
function_instance_id str

Target function instance ID.

required
page_number int

Page number, starting from 1.

1
page_size int

Number of log entries per page.

100
start_time Optional[int]

Optional start time filter (in seconds).

None
end_time Optional[int]

Optional end time filter (in seconds).

None
keywords Optional[List[str]]

Optional keyword filters for log messages.

None
api_key str

DashScope API key (uses DASHSCOPE_API_KEY env var if omitted).

None

Returns:

Type Description
Dict[str, Any]

Raw response dict of the log query API.

Source code in dashscope/applications/finetune/agentic_rl.py
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
@classmethod
async def query_function_instance_logs(
    cls,
    function_instance_id: str,
    page_number: int = 1,
    page_size: int = 100,
    start_time: Optional[int] = None,
    end_time: Optional[int] = None,
    keywords: Optional[List[str]] = None,
    api_key: str = None,
) -> Dict[str, Any]:
    """Query one page of logs for a function (faas) instance.

    Args:
        function_instance_id: Target function instance ID.
        page_number: Page number, starting from 1.
        page_size: Number of log entries per page.
        start_time: Optional start time filter (in seconds).
        end_time: Optional end time filter (in seconds).
        keywords: Optional keyword filters for log messages.
        api_key: DashScope API key (uses DASHSCOPE_API_KEY env var
            if omitted).

    Returns:
        Raw response dict of the log query API.
    """
    try:
        set_api_key(api_key)

        fc_component = AgenticRLFunctionComponent
        return await fc_component.query_function_instance_logs(
            function_instance_id=function_instance_id,
            page_number=page_number,
            page_size=page_size,
            start_time=start_time,
            end_time=end_time,
            keywords=keywords,
            api_key=api_key,
        )

    except Exception as e:
        if hasattr(e, "error_code"):
            raise
        raise InstanceQueryError(
            "Function instance log query failed",
            error_code=3009,
        ) from e

query_all_function_instance_logs async classmethod

query_all_function_instance_logs(
    function_instance_id: str,
    page_size: int = 100,
    start_time: Optional[int] = None,
    end_time: Optional[int] = None,
    keywords: Optional[List[str]] = None,
    api_key: str = None,
) -> List[str]

Fetch all logs of a function (faas) instance.

Pages are fetched one by one via the paginated log query until all entries are collected.

Parameters:

Name Type Description Default
function_instance_id str

Target function instance ID.

required
page_size int

Number of log entries per page.

100
start_time Optional[int]

Optional start time filter (in seconds).

None
end_time Optional[int]

Optional end time filter (in seconds).

None
keywords Optional[List[str]]

Optional keyword filters for log messages.

None
api_key str

DashScope API key (uses DASHSCOPE_API_KEY env var if omitted).

None

Returns:

Type Description
List[str]

Aggregated list of log messages across all pages.

Source code in dashscope/applications/finetune/agentic_rl.py
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
@classmethod
async def query_all_function_instance_logs(
    cls,
    function_instance_id: str,
    page_size: int = 100,
    start_time: Optional[int] = None,
    end_time: Optional[int] = None,
    keywords: Optional[List[str]] = None,
    api_key: str = None,
) -> List[str]:
    """Fetch all logs of a function (faas) instance.

    Pages are fetched one by one via the paginated log query until
    all entries are collected.

    Args:
        function_instance_id: Target function instance ID.
        page_size: Number of log entries per page.
        start_time: Optional start time filter (in seconds).
        end_time: Optional end time filter (in seconds).
        keywords: Optional keyword filters for log messages.
        api_key: DashScope API key (uses DASHSCOPE_API_KEY env var
            if omitted).

    Returns:
        Aggregated list of log messages across all pages.
    """
    try:
        set_api_key(api_key)

        fc_component = AgenticRLFunctionComponent
        return await fc_component.query_all_function_instance_logs(
            function_instance_id=function_instance_id,
            page_size=page_size,
            start_time=start_time,
            end_time=end_time,
            keywords=keywords,
            api_key=api_key,
        )

    except Exception as e:
        if hasattr(e, "error_code"):
            raise
        raise InstanceQueryError(
            "Function instance log query failed",
            error_code=3010,
        ) from e

delete_function_instance async classmethod

delete_function_instance(
    function_instance_id: str, api_key: str = None
) -> Dict[str, Any]

Delete a function (faas) runtime instance.

Parameters:

Name Type Description Default
function_instance_id str

Target function instance ID.

required
api_key str

DashScope API key (uses DASHSCOPE_API_KEY env var if omitted).

None

Returns:

Type Description
Dict[str, Any]

Raw response dict of the delete API, containing

Dict[str, Any]

data.status (e.g. "deleted") and

Dict[str, Any]

data.sandbox_code.

Raises:

Type Description
InstanceQueryError

If the deletion fails.

Source code in dashscope/applications/finetune/agentic_rl.py
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
@classmethod
async def delete_function_instance(
    cls,
    function_instance_id: str,
    api_key: str = None,
) -> Dict[str, Any]:
    """Delete a function (faas) runtime instance.

    Args:
        function_instance_id: Target function instance ID.
        api_key: DashScope API key (uses DASHSCOPE_API_KEY env var
            if omitted).

    Returns:
        Raw response dict of the delete API, containing
        ``data.status`` (e.g. "deleted") and
        ``data.sandbox_code``.

    Raises:
        InstanceQueryError: If the deletion fails.
    """
    try:
        set_api_key(api_key)

        fc_component = AgenticRLFunctionComponent
        return await fc_component.delete_function_instance(
            function_instance_id=function_instance_id,
            api_key=api_key,
        )

    except Exception as e:
        if hasattr(e, "error_code"):
            raise
        raise InstanceQueryError(
            "Function instance deletion failed",
            error_code=3011,
        ) from e

SubmitOutput

SubmitOutput(**kwargs: Any)

The output payload nested in a submit response.

A plain attribute container on purpose: the server's job payload is open-ended, and reinforcement_learning._to_job flattens it with vars(output), which needs a __dict__.

Source code in dashscope/applications/finetune/agentic_rl.py
86
87
def __init__(self, **kwargs: Any):
    self.__dict__.update(kwargs)

SubmitResult

SubmitResult(
    status_code: int = None,
    request_id: str = "",
    code: str = None,
    message: str = "",
    output: Any = None,
    usage: Any = None,
    headers: Dict[str, Any] = None,
    **kwargs: Any
)

The envelope AgenticRL.submit_job returns.

Carries the same seven fields as 1.x's DashScopeAPIResponse, so FineTune(**envelope) and SubmitResult(**envelope) accept the same dict. A failed submit is a non-200 status_code here rather than an exception, exactly as it was under FineTune.

Source code in dashscope/applications/finetune/agentic_rl.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def __init__(
    self,
    status_code: int = None,
    request_id: str = "",
    code: str = None,
    message: str = "",
    output: Any = None,
    usage: Any = None,
    headers: Dict[str, Any] = None,
    **kwargs: Any,
):
    self.__dict__.update(kwargs)
    self.status_code = status_code
    self.request_id = request_id
    self.code = code
    self.message = message
    self.output = (
        SubmitOutput(**output) if isinstance(output, dict) else output
    )
    self.usage = usage
    self.headers = headers or {}

status_code instance-attribute

status_code = status_code

request_id instance-attribute

request_id = request_id

code instance-attribute

code = code

message instance-attribute

message = message

output instance-attribute

output = (
    SubmitOutput(**output)
    if isinstance(output, dict)
    else output
)

usage instance-attribute

usage = usage

headers instance-attribute

headers = headers or {}

Reward decorators

These three decorators target the reward processor, not rollout function registration: reward_func is a class decorator applied to an AbstractRewardProcessor subclass (it generates process() for you); sub_reward_func and aggregate_func decorate processor methods to declare sub-rewards and aggregation logic. Minimal example:

from dashscope.applications.finetune.reinforcement import (
    AbstractRewardProcessor,
    RewardInput,
    RewardOutput,
    Reward,
    TaskStatus,
    reward_func,
    sub_reward_func,
)

@reward_func("MyReward")
class MyReward(AbstractRewardProcessor):
    @sub_reward_func("length", sub_weight=1.0)
    def length(self, input_data: RewardInput) -> RewardOutput:
        messages = input_data.agent_output.messages
        text = str(messages[0].get("content", "")) if messages else ""
        return RewardOutput(
            reward=Reward(reward_score=min(len(text) / 100, 1.0)),
            status=TaskStatus.SUCCESS,
        )

Without an aggregate_func, sub-rewards are combined as a weighted sum using sub_weight. Rollouts are authored differently: subclass AbstractRolloutProcessor and implement process() (optionally override setup()); these decorators are not involved — see "Processor base classes & demos" below.

reward_func

reward_func(processor_id: str) -> Callable[[Type], Type]

Class decorator to mark reward processors and collect sub-function information

Source code in dashscope/applications/finetune/reinforcement/component/func_decorator.py
 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def reward_func(processor_id: str) -> Callable[[Type], Type]:
    """Class decorator to mark reward processors and collect sub-function
    information"""

    def decorator(cls: Type) -> Type:
        # Create metadata object and attach to class
        meta = RewardProcessorMeta(processor_id)
        setattr(cls, "reward_meta", meta)

        async def process(self, input_data: RewardInput) -> RewardOutput:
            """Processes input by executing all sub-reward functions and
            aggregating results"""
            sub_rewards: Dict[str, RewardOutput] = {}

            tasks = []
            for name, sub_func in self.reward_meta.sub_functions.items():
                # Use the decorated function directly
                func = sub_func.func

                # Check if it's a coroutine function
                if asyncio.iscoroutinefunction(func):
                    task = func(input_data)
                else:
                    # Run synchronous function in executor
                    loop = asyncio.get_running_loop()
                    task = loop.run_in_executor(
                        self.executor,
                        func,
                        input_data,
                    )
                tasks.append((name, task))

            # Wait for all tasks to complete
            for name, task in tasks:
                try:
                    result = await task
                    sub_rewards[name] = result

                except Exception as e:
                    logger.error(
                        f"Error in sub-reward function {name}: {str(e)}",
                    )
                    # Store error as a zero score
                    sub_rewards[name] = RewardOutput(
                        reward=Reward(reward_score=0.0, reward_metrics={}),
                        status=TaskStatus.FAILED,
                        error=str(e),
                    )

            # Call aggregation function if available
            if self.reward_meta.aggregate_function:
                # Get the underlying function (unbound method)
                func = self.reward_meta.aggregate_function.func

                if asyncio.iscoroutinefunction(func):
                    return await func(sub_rewards)
                else:
                    # Run synchronous aggregate function in executor
                    loop = asyncio.get_running_loop()
                    return await loop.run_in_executor(
                        self.executor,
                        func,
                        sub_rewards,
                    )

            # If no aggregate function, use default weighted sum
            total = self.get_total(sub_rewards)

            # Return RewardOutput with calculated total
            return RewardOutput(
                reward=Reward(
                    reward_score=total,
                ),
                status=TaskStatus.SUCCESS,
                error=None,
            )

        # Set the new process method
        setattr(cls, "process", process)

        # Fix abstract method issue
        if hasattr(cls, "__abstractmethods__"):
            # Create a new set without 'process'
            abstract_methods = set(cls.__abstractmethods__)
            abstract_methods.discard("process")
            cls.__abstractmethods__ = frozenset(abstract_methods)

        return cls

    return decorator

sub_reward_func

sub_reward_func(
    name: Optional[str] = None, sub_weight: float = 1.0
) -> Callable[[Callable], Callable]

Decorator to mark sub-reward functions and specify weights

Source code in dashscope/applications/finetune/reinforcement/component/func_decorator.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def sub_reward_func(
    name: Optional[str] = None,
    sub_weight: float = 1.0,
) -> Callable[[Callable], Callable]:
    """Decorator to mark sub-reward functions and specify weights"""

    def decorator(func: Callable) -> Callable:
        func_name = name or func.__name__

        # Preserve async nature of the original function
        if asyncio.iscoroutinefunction(func):

            @wraps(func)
            async def async_wrapper(self, input_data: RewardInput):
                return await func(self, input_data)

            wrapper = async_wrapper
        else:

            @wraps(func)
            def sync_wrapper(self, input_data: RewardInput):
                return func(self, input_data)

            wrapper = sync_wrapper

        # Attach metadata
        setattr(wrapper, "_is_sub_reward_func", True)
        setattr(wrapper, "_sub_reward_name", func_name)
        setattr(wrapper, "_sub_weight", sub_weight)
        return wrapper

    return decorator

aggregate_func

aggregate_func(func: Callable) -> Callable

Decorator to mark aggregation functions

Source code in dashscope/applications/finetune/reinforcement/component/func_decorator.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def aggregate_func(func: Callable) -> Callable:
    """Decorator to mark aggregation functions"""
    if asyncio.iscoroutinefunction(func):

        @wraps(func)
        async def async_wrapper(self, *args, **kwargs):
            return await func(self, *args, **kwargs)

        wrapper = async_wrapper
    else:

        @wraps(func)
        def sync_wrapper(self, *args, **kwargs):
            return func(self, *args, **kwargs)

        wrapper = sync_wrapper

    wrapper.is_aggregate_func = True
    return wrapper

Data models

RolloutInput

Bases: BaseDataModel

Input parameter model for Rollout business processor.

Defines the structure of a single training sample or execution task. Corresponds to the RolloutInput structure in protocol.py.

func_type class-attribute instance-attribute

func_type: FunctionType = FuncType.ROLLOUT

messages class-attribute instance-attribute

messages: List[Dict] = Field(
    ...,
    description="The input messages provided to the agent.",
)

tools class-attribute instance-attribute

tools: Optional[List[Dict[str, Any]]] = Field(
    default=None, description="The tool used by the agent."
)

ground_truth class-attribute instance-attribute

ground_truth: Optional[Any] = Field(
    default=None,
    description="The expected gold-standard answer used for reward calculation or automated evaluation.",
)

rollout_extra class-attribute instance-attribute

rollout_extra: Optional[Dict[str, Any]] = Field(
    default=None,
    description="Extra information from the dataset.",
)

sampling_params class-attribute instance-attribute

sampling_params: Optional[Dict[str, Any]] = Field(
    default=None,
    description="Sampling parameters for the agent.",
)

request_metadata class-attribute instance-attribute

request_metadata: Optional[RequestMetadata] = Field(
    default=None,
    description="The metadata for the request.",
)

RolloutOutput

Bases: BaseModel

Rollout processor output result model.

Result of agent invocation, returned by AgentClient (remote/local). Corresponds to the RolloutOutput structure in protocol.py.

agent_output class-attribute instance-attribute

agent_output: Optional[AgentOutput] = Field(
    default=None, description="Agent output content."
)

status class-attribute instance-attribute

status: TaskStatus = Field(
    default=TaskStatus.SUCCESS,
    description="Task execution status.",
)

error class-attribute instance-attribute

error: Optional[str] = Field(
    default=None, description="Error details (when failed)."
)

error_code class-attribute instance-attribute

error_code: Optional[str] = Field(
    default=None, description="Error code (when failed)."
)

normalize_error_code classmethod

normalize_error_code(v: Any) -> Any

Normalize int error codes to str (protocol stays string).

Source code in dashscope/applications/finetune/reinforcement/component/data/rollout_output.py
49
50
51
52
53
@field_validator("error_code", mode="before")
@classmethod
def normalize_error_code(cls, v: Any) -> Any:
    """Normalize int error codes to str (protocol stays string)."""
    return _normalize_error_code(v)

RewardInput

Bases: BaseDataModel

Input parameter model for Reward business processor.

Corresponds to the RewardInput structure in protocol.py.

func_type class-attribute instance-attribute

func_type: FunctionType = FuncType.REWARD

ground_truth class-attribute instance-attribute

ground_truth: Optional[Any] = Field(
    default=None,
    description="The expected gold-standard answer used for reward calculation or automated evaluation.",
)

request_metadata class-attribute instance-attribute

request_metadata: Optional[RequestMetadata] = Field(
    default=None,
    description="The metadata for the request.",
)

agent_output class-attribute instance-attribute

agent_output: AgentOutput = Field(
    ...,
    description="The agent output for the reward calculation.",
)

Reward

Bases: BaseModel

Reward calculation result.

reward_score class-attribute instance-attribute

reward_score: float = Field(
    ..., description="The reward score."
)

reward_metrics class-attribute instance-attribute

reward_metrics: Optional[Dict[str, float]] = Field(
    None,
    description="Additional reward-specific metrics as string key-value pairs.",
)

RewardOutput

Bases: BaseModel

Reward processor output result model.

reward class-attribute instance-attribute

reward: Reward = Field(
    ...,
    description="The computed reward for the given rollout.",
)

status class-attribute instance-attribute

status: TaskStatus = Field(
    default=TaskStatus.SUCCESS,
    description="The status of the reward computation.",
)

error class-attribute instance-attribute

error: Optional[str] = Field(
    default=None,
    description="Error details if the reward computation failed.",
)

error_code class-attribute instance-attribute

error_code: Optional[str] = Field(
    default=None,
    description="Error code if the reward computation failed.",
)

normalize_error_code classmethod

normalize_error_code(v: Any) -> Any

Normalize int error codes to str (protocol stays string).

Source code in dashscope/applications/finetune/reinforcement/component/data/reward_output.py
64
65
66
67
68
@field_validator("error_code", mode="before")
@classmethod
def normalize_error_code(cls, v: Any) -> Any:
    """Normalize int error codes to str (protocol stays string)."""
    return _normalize_error_code(v)

AgentOutput

Bases: BaseModel

Represents final result of agent execution or specific rollout.

messages class-attribute instance-attribute

messages: Optional[List[Dict]] = Field(
    None,
    description="The complete conversation history or sequence of internal thoughts and actions.",
)

rollout_extra class-attribute instance-attribute

rollout_extra: Optional[Dict[str, Any]] = Field(
    default=None,
    description="Extra information from the dataset.",
)

rollout_metrics class-attribute instance-attribute

rollout_metrics: Optional[Dict[str, float]] = Field(
    default_factory=dict,
    description="Additional evaluation metrics or metadata associated with this output.",
)

reward_score class-attribute instance-attribute

reward_score: Optional[float] = Field(
    None,
    description="Scalar feedback score assigned to this output (typically for RL/evaluation).",
)

Task

Bases: BaseModel

Defines structure for individual training samples or execution tasks.

rollout_id class-attribute instance-attribute

rollout_id: str = Field(
    default_factory=lambda: _generate_ro_id(12),
    description="Unique identifier for tracking specific task executions.",
)

prompt class-attribute instance-attribute

prompt: Union[str, List[Dict], Dict] = Field(
    ...,
    description="Input instructions/questions/message list provided to the agent.",
)

ground_truth class-attribute instance-attribute

ground_truth: Optional[Any] = Field(
    default=None,
    description="Reference answer for reward calculation or automated evaluation.",
)

training_state class-attribute instance-attribute

training_state: Optional[Dict[str, Any]] = Field(
    default_factory=dict,
    description="Optional training state information (e.g., task_id, epoch).",
)

TaskStatus

Bases: str, Enum

Task execution status.

SUCCESS class-attribute instance-attribute

SUCCESS = 'success'

FAILED class-attribute instance-attribute

FAILED = 'failed'

Processor base classes & demos

AbstractRolloutProcessor

Bases: AbstractProcessor

Abstract base class for Rollout business processors.

Users must implement process() to define custom agent inference/rollout logic. Optionally override setup() to initialize workspace before the server starts.

Example

class MyRolloutProcessor(AbstractRolloutProcessor): ... def setup(self) -> None: ... # Load models, initialize connections, etc. ... self.model = load_model("path/to/model") ... ... def process(self, input: RolloutInput) -> RolloutOutput: ... # Call model service for inference ... response = self._call_model(input) ... return RolloutOutput( ... agent_output=AgentOutput( ... message=response.messages, ... ), ... status=TaskStatus.SUCCESS, ... )

setup

setup() -> None

Initialize workspace before the server starts processing requests.

Override this method to perform one-time initialization tasks such as: - Loading ML models into memory - Establishing database connections - Initializing cache - Loading configuration files - Pre-computing static data

This method is called once during server startup, before any requests are processed. The default implementation does nothing.

Raises:

Type Description
Exception

If initialization fails, the server will fail to start.

Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_rollout_processor.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def setup(self) -> None:
    """
    Initialize workspace before the server starts processing requests.

    Override this method to perform one-time initialization tasks such as:
    - Loading ML models into memory
    - Establishing database connections
    - Initializing cache
    - Loading configuration files
    - Pre-computing static data

    This method is called once during server startup, before any
    requests are processed. The default implementation does nothing.

    Raises:
        Exception: If initialization fails, the server will fail to start.
    """

process abstractmethod async

process(input_data: RolloutInput) -> RolloutOutput

Execute rollout logic.

Parameters:

Name Type Description Default
input_data RolloutInput

Parsed RolloutInput input object.

required

Returns:

Type Description
RolloutOutput

RolloutOutput object containing standardized results (

RolloutOutput

rollout_id, content, status).

Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_rollout_processor.py
66
67
68
69
70
71
72
73
74
75
76
77
78
@abstractmethod
async def process(self, input_data: RolloutInput) -> RolloutOutput:
    """
    Execute rollout logic.

    Args:
        input_data: Parsed RolloutInput input object.

    Returns:
        RolloutOutput object containing standardized results (
        rollout_id, content, status).
    """
    raise NotImplementedError

AbstractRewardProcessor

AbstractRewardProcessor(
    executor: Optional[Executor] = None,
)

Bases: AbstractProcessor

Abstract base class for Reward business processors.

Users must implement process() to define custom reward calculation logic. Optionally override setup() to initialize workspace before the server starts.

Example

class MyRewardProcessor(AbstractRewardProcessor): ... def setup(self) -> None: ... # Load embedding models, initialize databases, etc. ... self.embedding_model = load_embedding_model() ... ... def process(self, input: RewardInput) -> RewardOutput: ... # Custom reward calculation ... score = self._compute_score(input.agent_output, input.ground_truth) ... return RewardOutput( ... reward=Reward( ... reward_score=score, ... ), ... status=TaskStatus.SUCCESS, ... )

Initialize reward processor

Parameters:

Name Type Description Default
executor Optional[Executor]

Optional executor for running synchronous functions

None
Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_reward_processor.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def __init__(self, executor: Optional[Executor] = None):
    """
    Initialize reward processor

    Args:
        executor: Optional executor for running synchronous functions
    """
    self.executor = executor or ThreadPoolExecutor()
    self._executor_owned = executor is None
    # Instance-level flag so collection state is never shared through the
    # class attribute.
    self.functions_collected = False
    # Collect all sub-functions and aggregate functions during
    # initialization
    # IMPORTANT: Subclasses MUST call super().__init__() to ensure
    # function collection
    self._collect_functions()

reward_meta instance-attribute

reward_meta: RewardProcessorMeta

functions_collected class-attribute instance-attribute

functions_collected: bool = False

executor instance-attribute

executor = executor or ThreadPoolExecutor()

shutdown

shutdown()

Cleanup resources when the processor is no longer needed.

Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_reward_processor.py
128
129
130
131
132
def shutdown(self):
    """Cleanup resources when the processor is no longer needed."""
    if self._executor_owned and self.executor is not None:
        self.executor.shutdown(wait=True)
        self.executor = None

get_weights

get_weights()
Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_reward_processor.py
143
144
145
146
147
def get_weights(self):
    return {
        name: self.reward_meta.sub_functions[name].weight
        for name in self.reward_meta.sub_functions
    }

get_scores

get_scores(sub_rewards: Dict[str, RewardOutput])
Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_reward_processor.py
149
150
151
152
def get_scores(self, sub_rewards: Dict[str, RewardOutput]):
    return {
        name: sub_rewards[name].reward.reward_score for name in sub_rewards
    }

get_total

get_total(sub_rewards: Dict[str, RewardOutput])
Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_reward_processor.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def get_total(self, sub_rewards: Dict[str, RewardOutput]):
    total = 0.0
    for name, reward_output in sub_rewards.items():
        if name not in self.reward_meta.sub_functions:
            raise ValueError(
                f"Sub-reward function '{name}' is not registered. "
                f"Available:"
                f" {list(self.reward_meta.sub_functions.keys())}",
            )
        total += (
            reward_output.reward.reward_score
            * self.reward_meta.sub_functions[name].weight
        )
    return total

get_reward_metrics

get_reward_metrics(sub_rewards: Dict[str, RewardOutput])
Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_reward_processor.py
169
170
171
172
173
174
175
176
177
178
179
def get_reward_metrics(self, sub_rewards: Dict[str, RewardOutput]):
    reward_metrics = {}
    for name, reward_output in sub_rewards.items():
        # Add namespace prefix to avoid key collisions
        for key, value in (
            reward_output.reward.reward_metrics or {}
        ).items():
            prefixed_key = f"{name}.{key}"
            reward_metrics[prefixed_key] = value

    return reward_metrics

setup

setup() -> None

Initialize workspace before the server starts processing requests.

Override this method to perform one-time initialization tasks such as: - Loading ML models into memory - Establishing database connections - Initializing cache - Loading configuration files - Pre-computing static data

This method is called once during server startup, before any requests are processed. The default implementation does nothing.

Raises:

Type Description
Exception

If initialization fails, the server will fail to start.

Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_reward_processor.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
def setup(self) -> None:
    """
    Initialize workspace before the server starts processing requests.

    Override this method to perform one-time initialization tasks such as:
    - Loading ML models into memory
    - Establishing database connections
    - Initializing cache
    - Loading configuration files
    - Pre-computing static data

    This method is called once during server startup, before any
    requests are processed.
    The default implementation does nothing.

    Raises:
        Exception: If initialization fails, the server will fail to start.
    """

process abstractmethod

process(input_data: RewardInput) -> RewardOutput

Execute reward calculation logic.

Parameters:

Name Type Description Default
input_data RewardInput

Parsed RewardInput input object.

required

Returns:

Type Description
RewardOutput

RewardOutput object containing standardized results (score,

RewardOutput

rollout_id, status).

Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_reward_processor.py
201
202
203
204
205
206
207
208
209
210
211
212
213
@abstractmethod
def process(self, input_data: RewardInput) -> RewardOutput:
    """
    Execute reward calculation logic.

    Args:
        input_data: Parsed RewardInput input object.

    Returns:
        RewardOutput object containing standardized results (score,
        rollout_id, status).
    """
    raise NotImplementedError

DemoRolloutProcessor

Bases: AbstractRolloutProcessor

Demo implementation of Rollout Processor. Demonstrates simple rollout execution flow (simulating Agent invocation).

In production, this should invoke real Agent/model services, e.g., through OpenAI SDK or Dashscope SDK.

setup

setup() -> None

Initialize workspace before processing requests.

Demo implementation: Logs startup message. In production, this could load ML models, establish connections, etc.

Source code in dashscope/applications/finetune/reinforcement/component/demo/rollout_processor_demo.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def setup(self) -> None:
    """
    Initialize workspace before processing requests.

    Demo implementation: Logs startup message.
    In production, this could load ML models, establish connections, etc.
    """
    logger.info(
        "[DemoRolloutProcessor] setup() called - initializing workspace",
    )
    # Demo: No actual initialization needed
    # In production, you might:
    # - Load ML models into memory
    # - Initialize API clients
    # - Set up connection pools
    logger.info("[DemoRolloutProcessor] setup() completed")

process

process(input_data: RolloutInput) -> RolloutOutput

Demo implementation: Simulates Agent invocation by echoing messages.

Parameters:

Name Type Description Default
input_data RolloutInput

RolloutInput input parameter

required

Returns:

Type Description
RolloutOutput

RolloutOutput object containing standardized execution results

Source code in dashscope/applications/finetune/reinforcement/component/demo/rollout_processor_demo.py
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
def process(self, input_data: RolloutInput) -> RolloutOutput:
    """
    Demo implementation: Simulates Agent invocation by echoing messages.

    Args:
        input_data: RolloutInput input parameter

    Returns:
        RolloutOutput object containing standardized execution results
    """
    logger.info(
        f"[DemoRolloutProcessor] starting rollout | "
        f"model={input_data.model_resource.model_name}",
    )

    start = time.time()

    # Demo: Return messages as echo content
    messages = list(input_data.messages)

    # Add a simple assistant response
    last_user_msg = ""
    for msg in reversed(messages):
        if msg.get("role") == "user":
            last_user_msg = msg.get("content", "")
            break

    assistant_content = f"[DemoRolloutProcessor Echo] {last_user_msg}"
    messages.append({"role": "assistant", "content": assistant_content})

    latency = round(time.time() - start, 4)

    result = RolloutOutput(
        agent_output=AgentOutput(
            messages=messages,
            rollout_extra=input_data.rollout_extra,
            rollout_metrics={},
        ),
        status=TaskStatus.SUCCESS,
        error=None,
    )
    logger.info(f"[DemoRolloutProcessor] result: latency={latency}s")
    return result

DemoRewardProcessor

DemoRewardProcessor(executor: Optional[Executor] = None)

Bases: AbstractRewardProcessor

Demo implementation of Reward Processor. Demonstrates rule-based scoring for Agent outputs.

Scoring Strategy: - Reward 1.0 if ground_truth exists and is contained in agent_output messages - Reward 0.5 if agent_output has messages - Default reward 0.0 otherwise

Source code in dashscope/applications/finetune/reinforcement/component/processor/abstract_reward_processor.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def __init__(self, executor: Optional[Executor] = None):
    """
    Initialize reward processor

    Args:
        executor: Optional executor for running synchronous functions
    """
    self.executor = executor or ThreadPoolExecutor()
    self._executor_owned = executor is None
    # Instance-level flag so collection state is never shared through the
    # class attribute.
    self.functions_collected = False
    # Collect all sub-functions and aggregate functions during
    # initialization
    # IMPORTANT: Subclasses MUST call super().__init__() to ensure
    # function collection
    self._collect_functions()

setup

setup() -> None

Initialize workspace before processing requests.

Demo implementation: Logs startup message. In production, this could load embedding models, initialize databases, etc.

Source code in dashscope/applications/finetune/reinforcement/component/demo/reward_processor_demo.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def setup(self) -> None:
    """
    Initialize workspace before processing requests.

    Demo implementation: Logs startup message.
    In production, this could load embedding models, initialize
    databases, etc.
    """
    logger.info(
        "[DemoRewardProcessor] setup() called - initializing workspace",
    )
    # Demo: No actual initialization needed
    # In production, you might:
    # - Load embedding models for semantic similarity
    # - Initialize database connections for storing rewards
    # - Load configuration files
    logger.info("[DemoRewardProcessor] setup() completed")

process

process(input_data: RewardInput) -> RewardOutput

Demo implementation: Calculate simple rewards based on agent_output messages and ground_truth matching.

Parameters:

Name Type Description Default
input_data RewardInput

RewardInput input parameter

required

Returns:

Type Description
RewardOutput

RewardOutput object containing standardized reward calculation

Source code in dashscope/applications/finetune/reinforcement/component/demo/reward_processor_demo.py
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
def process(self, input_data: RewardInput) -> RewardOutput:
    """
    Demo implementation: Calculate simple rewards based on agent_output
    messages
    and ground_truth matching.

    Args:
        input_data: RewardInput input parameter

    Returns:
        RewardOutput object containing standardized reward calculation
    """
    logger.info(
        "[DemoRewardProcessor] computing reward for rollout_id",
    )

    score = 0.0

    if (
        input_data.ground_truth is not None
        and input_data.agent_output.messages
    ):
        gt_str = str(input_data.ground_truth)
        for msg in input_data.agent_output.messages:
            if (
                isinstance(msg.get("content"), str)
                and gt_str in msg["content"]
            ):
                score = 1.0
                break
        if score == 0.0 and len(input_data.agent_output.messages) > 0:
            score = 0.5

    result = RewardOutput(
        reward=Reward(
            reward_score=score,
            reward_metrics=input_data.agent_output.rollout_metrics,
        ),
        status=TaskStatus.SUCCESS,
        error=None,
    )
    logger.info(f"[DemoRewardProcessor] result: {result}")
    return result