Skip to content

rg.Response

Class for interacting with Argilla Responses of records. Responses are answers to questions by a user. Therefore, a recod question can have multiple responses, one for each user that has answered the question. A Response is typically created by a user in the UI or consumed from a data source as a label, unlike a Suggestion which is typically created by a model prediction.

Usage Examples

Responses can be added to an instantiated Record directly or as a dictionary a dictionary. The following examples demonstrate how to add responses to a record object and how to access responses from a record object:

Instantiate the Record and related Response objects:

dataset.records.log(
    [
        rg.Record(
            fields={"text": "Hello World, how are you?"},
            responses=[rg.Response("label", "negative", user_id=user.id)],
            external_id=str(uuid.uuid4()),
        )
    ]
)

Or, add a response from a dictionary where key is the question name and value is the response:

dataset.records.log(
    [
        {
            "text": "Hello World, how are you?",
            "label.response": "negative",
        },
    ]
)

Responses can be accessed from a Record via their question name as an attribute of the record. So if a question is named label, the response can be accessed as record.label. The following example demonstrates how to access responses from a record object:

# iterate over the records and responses

for record in dataset.records:
    for response in record.responses.label:
        print(response.value)
        print(response.user_id)

# validate that the record has a response

for record in dataset.records:
    if record.responses.label:
        for response in record.responses.label:
            print(response.value)
            print(response.user_id)

Class Reference

rg.Response

Class for interacting with Argilla Responses of records. Responses are answers to questions by a user. Therefore, a recod question can have multiple responses, one for each user that has answered the question. A Response is typically created by a user in the UI or consumed from a data source as a label, unlike a Suggestion which is typically created by a model prediction.

Source code in src/argilla_sdk/responses.py
class Response:
    """Class for interacting with Argilla Responses of records. Responses are answers to questions by a user.
    Therefore, a recod question can have multiple responses, one for each user that has answered the question.
    A `Response` is typically created by a user in the UI or consumed from a data source as a label,
    unlike a `Suggestion` which is typically created by a model prediction.

    """

    def __init__(
        self,
        question_name: str,
        value: Any,
        user_id: UUID,
        _record: Optional["Record"] = None,
    ) -> None:
        """Initializes a `Response` for a `Record` with a user_id and value"""

        if question_name is None:
            raise ValueError("question_name is required")
        if value is None:
            raise ValueError("value is required")
        if user_id is None:
            raise ValueError("user_id is required")

        self.record = _record
        self.question_name = question_name
        self.value = value
        self.user_id = user_id

    def serialize(self) -> dict[str, Any]:
        """Serializes the Response to a dictionary. This is principally used for sending the response to the API, \
            but can be used for data wrangling or manual export.

        Returns:
            dict[str, Any]: The serialized response as a dictionary with keys `question_name`, `value`, and `user_id`.

        Examples:

        ```python
        response = rg.Response("label", "negative", user_id=user.id)
        response.serialize()
        ```
        """
        return {
            "question_name": self.question_name,
            "value": self.value,
            "user_id": self.user_id,
        }

__init__(question_name, value, user_id, _record=None)

Initializes a Response for a Record with a user_id and value

Source code in src/argilla_sdk/responses.py
def __init__(
    self,
    question_name: str,
    value: Any,
    user_id: UUID,
    _record: Optional["Record"] = None,
) -> None:
    """Initializes a `Response` for a `Record` with a user_id and value"""

    if question_name is None:
        raise ValueError("question_name is required")
    if value is None:
        raise ValueError("value is required")
    if user_id is None:
        raise ValueError("user_id is required")

    self.record = _record
    self.question_name = question_name
    self.value = value
    self.user_id = user_id

serialize()

Serializes the Response to a dictionary. This is principally used for sending the response to the API, but can be used for data wrangling or manual export.

Returns:

Type Description
dict[str, Any]

dict[str, Any]: The serialized response as a dictionary with keys question_name, value, and user_id.

Examples:

response = rg.Response("label", "negative", user_id=user.id)
response.serialize()
Source code in src/argilla_sdk/responses.py
def serialize(self) -> dict[str, Any]:
    """Serializes the Response to a dictionary. This is principally used for sending the response to the API, \
        but can be used for data wrangling or manual export.

    Returns:
        dict[str, Any]: The serialized response as a dictionary with keys `question_name`, `value`, and `user_id`.

    Examples:

    ```python
    response = rg.Response("label", "negative", user_id=user.id)
    response.serialize()
    ```
    """
    return {
        "question_name": self.question_name,
        "value": self.value,
        "user_id": self.user_id,
    }