M2 Graded Lab question A graded lab exercise fails with an AttributeError because the revise_draft function prints the OpenAI chat completion response instead of returning it, so the grader receives None when it accesses return_value.choices. The error log shows a valid response object with model 'gpt-4o-2024-08-06', id 'chatcmpl-EXmjZdadkz2ZenK9biYfu4MNdoCn0', prompt_tokens=85 and completion_tokens=38, confirming the API call succeeded while the function's exit point leaked the object. The fix is to assign the client.chat.completions.create call to a variable and return it, and to ensure the prompt includes both the draft and feedback variables since the model replied that it needed the original draft and reflection feedback. M2 Graded Lab question Why does the error trigger if the print shows a response? The contradiction between a successful print and a NoneType error typically stems from the difference between your manual execution and the automated test environment. If you see a response containing model=‘gpt-4o-2024-08-06’ and a valid id=‘chatcmpl-EXmjZdadkz2ZenK9biYfu4MNdoCn0’ , the API is working, but your function revise draft is likely not returning that object to the caller. When the grader runs revise draft , it expects the function to return the completion object. If the function finishes without an explicit return statement, Python implicitly returns None . The test case then tries to access return value.choices , which triggers the AttributeError because return value is None . How to fix the revise draft function To resolve this, ensure the response object from the OpenAI client is actually passed back out of the function. A common mistake is printing the response for debugging purposes but forgetting to return it. Check your code for these specific patterns: 1. Ensure the API call is assigned to a variable. 2. Ensure that variable is returned at the end of the function. python def revise draft prompt : Example of the correct flow response = client.chat.completions.create model="gpt-4o-2024-08-06", messages= {"role": "user", "content": prompt} If you only print response here, the test will fail. return response If the error persists, verify that there isn't a conditional branch like an if/else block where the function might exit without hitting a return statement. If the code enters a logic path that doesn't return the object, the grader receives None and crashes. Handling the prompt content issue Looking at the response provided in the error log, the assistant replied: "To provide a meaningful improvement to your essay, please include both the original draft and the reflection feedback." This indicates a secondary problem: the prompt being sent to the model is insufficient. While the code might be technically functioning returning a valid object , the model is refusing to perform the revision because it lacks the necessary context. In a production environment, this would be a "soft failure" where the API works but the business logic fails. To fix this, ensure your prompt construction includes both the draft and the feedback variables before calling the API. Debugging the test environment If you are certain the return statement exists, the issue might be related to how the revise draft function handles exceptions. If you wrapped your API call in a try/except block and the except block doesn't return a value, any network glitch or API timeout will result in a None return. When debugging these graded labs, always compare the exact object structure. The log shows completion tokens=38 and prompt tokens=85 , confirming the request reached the server. Since the object exists in your logs but not in the test case, the "leak" is happening at the function's exit point. Double-check that you aren't returning the content of the message a string instead of the full response object, as the grader specifically looks for the .choices attribute. Next Why is managing ChatGPT custom instructions such a chore? → https://promptcube3.com/en/threads/9929/ All Replies (1) Want a live back-and-forth? Join the global AI chat room https://promptcube3.com/en/chat/ — login to talk. Check if you actually saved the notebook. Auto-graders often run stale versions, ignoring your latest revise draft fixes.