Skip to content

rg.Response

Class for interacting with Argilla Responses of records. Responses are answers to questions by a user. Therefore, a 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"]: # (1)
        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)
    else:
        record.responses.add(
            rg.Response("label", "positive", user_id=user.id)
        ) # (2)
1. Access the responses for the question named label for each record like a dictionary containing a list of Response objects. 2. Add a response to the record if it does not already have one.

Format per Question type

Depending on the Question type, responses might need to be formatted in a slightly different way.

rg.Response(
    question_name="label",
    value="positive",
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="multi-label",
    value=["positive", "negative"],
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="rank",
    value=["1", "3", "2"],
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="rating",
    value=4,
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="span",
    value=[{"start": 0, "end": 9, "label": "MISC"}],
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="text",
    value="value",
    user_id=user.id,
    status="draft"
)

Response

Class for interacting with Argilla Responses of records. Responses are answers to questions by a user. Therefore, a record 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/responses.py
class Response:
    """Class for interacting with Argilla Responses of records. Responses are answers to questions by a user.
    Therefore, a record 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,
        status: Optional[Union[ResponseStatus, str]] = None,
        _record: Optional["Record"] = None,
    ) -> None:
        """Initializes a `Response` for a `Record` with a user_id and value

        Attributes:
            question_name (str): The name of the question that the suggestion is for.
            value (str): The value of the response
            user_id (UUID): The id of the user that submits the response
            status (Union[ResponseStatus, str]): The status of the response as "draft", "submitted", "discarded".
        """

        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")

        if isinstance(status, str):
            status = ResponseStatus(status)

        self._record = _record
        self.question_name = question_name
        self.value = value
        self.user_id = user_id
        self.status = status

    @property
    def record(self) -> "Record":
        """Returns the record associated with the response"""
        return self._record

    @record.setter
    def record(self, record: "Record") -> None:
        """Sets the record associated with the response"""
        self._record = record

    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,
            "status": self.status,
        }

record: Record property writable

Returns the record associated with the response

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

Initializes a Response for a Record with a user_id and value

Attributes:

Name Type Description
question_name str

The name of the question that the suggestion is for.

value str

The value of the response

user_id UUID

The id of the user that submits the response

status Union[ResponseStatus, str]

The status of the response as "draft", "submitted", "discarded".

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

    Attributes:
        question_name (str): The name of the question that the suggestion is for.
        value (str): The value of the response
        user_id (UUID): The id of the user that submits the response
        status (Union[ResponseStatus, str]): The status of the response as "draft", "submitted", "discarded".
    """

    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")

    if isinstance(status, str):
        status = ResponseStatus(status)

    self._record = _record
    self.question_name = question_name
    self.value = value
    self.user_id = user_id
    self.status = status

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/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,
        "status": self.status,
    }