trl icon indicating copy to clipboard operation
trl copied to clipboard

Support explicit JSON tool schemas in GRPOTrainer (in addition to callable tools)

Open ansh-info opened this issue 10 months ago • 3 comments

Feature request

GRPOTrainer currently requires tools to be callables and builds tool schemas from functionsignatures/docstrings. This makes nested tool arguments shallow (object with no properties), which hurts tool‑argument correctness during RL.

Please add support for passing explicit JSON schemas (same format used by Transformers chat tools) into GRPOTrainer, so prompts can include the full nested argument structure.

No paper/code link - this is a tooling capability request.

Motivation

We are training GRPO for tool calling. With nested tool arguments (e.g., sys_bio_model.biomodel_id, arg_data.time_data.duration/interval), the auto‑schema derived from callables is too shallow. The model invents keys during RL (model_id, time_points, etc.) because it never sees the full schema in the prompt.

We had to workaround this by embedding explicit JSON examples in the system prompt, which is fragile and not scalable for many tools. Allowing explicit JSON schemas would solve this cleanly and improve argument correctness. (No related GH issue found yet.)

Your contribution

Yes, I can submit a PR. I’m happy to implement support for tools_schema (list of dicts) or allow tools to accept JSON schemas directly alongside callables, gated by version checks if needed.

If you want, I can also sketch the exact API change.

ansh-info avatar Jan 18 '26 20:01 ansh-info

Do you have a minimal example of such tools? So that I have an example of it.

qgallouedec avatar Jan 18 '26 20:01 qgallouedec

Do you have a minimal example of such tools? So that I have an example of it.

Thank you for the reply here are the tools that Im talking about simulate_model and ask_question

ansh-info avatar Jan 18 '26 21:01 ansh-info

well this is not minimal nor self-contained. From the description I assume that the issue is roughly something like this:

def multiply(a: int, b: int) -> int:
    """
    Multiplies two integers.

    Args:
        a: The first integer.
        b: The second integer.

    Returns:
        The product of the two integers.
    """
    return a * b

def add(x: int, y: int) -> int:
    """
    Adds two integers.

    Args:
        x: The first integer.
        y: The second integer.

    Returns:
        The sum of the two integers.
    """
    return x + y

def calculator(operation: str, args: dict[str, int]) -> int:
    """
    Performs a calculation based on the specified operation.

    Args:
        operation: A string specifying the operation ('add' or 'multiply').
        args: A dictionary containing the arguments for the operation.

    Returns:
        The result of the calculation.
    """
    if operation == 'add':
        return add(**args)
    elif operation == 'multiply':
        return multiply(**args)
    else:
        raise ValueError("Unsupported operation. Use 'add' or 'multiply'.")
    
if __name__ == "__main__":
    from transformers.utils import get_json_schema

    schema = get_json_schema(calculator)
    print(schema)
{'type': 'function', 'function': {'name': 'calculator', 'description': 'Performs a calculation based on the specified operation.', 'parameters': {'type': 'object', 'properties': {'operation': {'type': 'string', 'description': "A string specifying the operation ('add' or 'multiply')."}, 'args': {'type': 'object', 'additionalProperties': {'type': 'integer'}, 'description': 'A dictionary containing the arguments for the operation.'}}, 'required': ['operation', 'args']}, 'return': {'type': 'integer', 'description': 'The result of the calculation.'}}}

From the json schema, the model cannot "guess" the input name for add are x and y, while for multiply, it's a and b. Am I right?

qgallouedec avatar Jan 19 '26 23:01 qgallouedec