expectedparrot / edsl

Design, conduct and analyze results of AI-powered surveys and experiments. Simulate social science and market research with large numbers of AI agents and LLMs.
https://docs.expectedparrot.com
MIT License
176 stars 18 forks source link

Handling Integer Key Conversion for option_labels in QuestionLinearScale JSON Serialization/Deserialization #165

Closed zer0dss closed 2 weeks ago

zer0dss commented 6 months ago

Something to take into consideration when sending the JSON data of the surveys/results to the remote server and loading them back to edsl. Reproduction code:

from edsl.questions import QuestionLinearScale            
import json 
q = QuestionLinearScale(
        question_text="How much do you like ice cream?",
        question_options=[1, 2, 3, 4, 5],
        question_name="ice_cream",
        option_labels={1: "I hate it", 5: "I love it"}
    )
q_dict = q.to_dict()
#simulation of sending to a remote endpoint
json_data = json.dumps(q_dict)
# here code to send the data
# ....
#simulation of loading the request data
q_dict = json.loads(json_data)
new_q = QuestionLinearScale.from_dict(q_dict) # this triggers a validation error
#fix code (converting the option_labels keys to int)
if q_dict["question_type"] == "linear_scale":
    option_labels = q_dict["option_labels"]
    option_labels = {int(key): value for key, value in option_labels.items()}
    q_dict["option_labels"] = option_labels
new_q = QuestionLinearScale.from_dict(q_dict)
apostolosfilippas commented 6 months ago

@johnjosephhorton this should be fixed on EDSL level I think