How do I add answers when creating a quiz using the API?

Jump to solution
Community Novice


I'm using the canvasapi Python module, and I'm trying to create quizzes from a file. I am able to create the entire quiz this way except for adding answers. I've looked everywhere I can think of for information on what I'm doing wrong with the answer objects, but I'm unable to find even the basic format of an answer object. I've attached some sample code below from one of my hard-coded quizzes that should illustrate the issue I'm having:

'question_name': 'Gender',
'question_type': 'multiple_choice_question',
'question_text': 'Which gender do you identify as?',
'points_possible': '0.0',
'correct_comments': '',
'incorrect_comments': '',
'neutral_comments': '',
'correct_comments_html': '',
'incorrect_comments_html': '',
'neutral_comments_html': '',
'answers': [{'answer_text':'Male', 'weight':100.0}
,{'answer_text':'Female', 'weight':0.0}
,{'answer_text':'Other', 'weight':0.0}
,{'answer_text':'Prefer not to say', 'weight':0.0}

I had been under the impression that to add an answer, I should only need to specify the answer_text and the weight (or answer_weight, I've tried both and neither's worked). However, when I run that (in terminal), I get this:

Traceback (most recent call last):
  File "<stdin>", line 15, in <module>
  File "/home/jeff/anaconda3/lib/python3.6/site-packages/canvasapi/", line 136, in create_question
  File "/home/jeff/anaconda3/lib/python3.6/site-packages/canvasapi/", line 111, in request
    raise CanvasException("API encountered an error processing your request")
canvasapi.exceptions.CanvasException: API encountered an error processing your request

Obviously that error message isn't particularly helpful.
Out of curiosity, I went onto the website and created an answer via the web interface, just to see what the structure of an answer is. This is what I got (just the answers section, because this question is already getting pretty long):

QuizQuestion(_requester=<canvasapi.requester.Requester object at 0x7f5ac4bd47b8>, id=1337341, quiz_id=122827, quiz_group_id=None, assessment_question_id=1851679, position=None, question_name=Question1, question_type=multiple_choice_question, question_text=<p>It worked!</p>, points_possible=1.0, correct_comments=, incorrect_comments=, neutral_comments=, correct_comments_html=, incorrect_comments_html=, neutral_comments_html=, answers=[{'id': 505, 'text': '1', 'html': '', 'comments': '', 'comments_html': '', 'weight': 100.0}], variables=None, formulas=None, answer_tolerance=None, formula_decimal_places=None, matches=None, matching_answer_incorrect_matches=None, course_id=31084)

I've tried specifying each of those fields within the python code to create my own answers, but again this didn't work. Even if it had, the 'id' field itself already presents a problem, since it's going to be very difficult for me to manually ensure I'm always getting the next unique id.

That said, the structure of the Answer object kind of indicates to me that there's probably a way to actually make this, I just haven't been able to find it in the documentation at all. Has anyone encountered this problem before, and how did you deal with it?

1 Solution
Community Participant

Hey  @bulmer_jeffk ‌!

I'm one of the primary maintainers of the CanvasAPI library. Let's see if we can get you up and running!

The code you provided appears to be correctly formatted. In fact, I copy-pasted it without alterations and was able to successfully create a new question with all the parameters you provided.

The error message you're seeing is as a result of Canvas returning an HTTP 500 error to the API request. The latest version of CanvasAPI actually makes this error slightly more useful by also providing the status code. This error means Canvas is breaking when trying to process your request, often due to bad input that is then mishandled by the server.

Since I don't see anything immediately wrong with the code you posted, I have a few questions:

  1. What version of CanvasAPI are you using? (pip show canvasapi)
  2. What version of Python are you using? (python --version)
  3. Are you making several questions at once? If so, does this occur when you only create one?
  4. Does this error occur whenever you create any question, or only when you provide answers?

My apologies for seeing this so late. I don't frequent the Canvas Community as often as I probably should, so the best place to get CanvasAPI-specific help is the UCF Open Source Slack in the #canvasapi channel. Creating an issue on our GitHub page is also a good way.

- Matt Emond

View solution in original post