{"slug": "m2-graded-lab-question", "title": "M2 Graded Lab question", "summary": "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.", "body_md": "# M2 Graded Lab question\n\n## Why does the error trigger if the print shows a response?\n\nThe 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.\n\nWhen 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`.\n\n## How to fix the revise_draft function\n\nTo 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.\n\nCheck your code for these specific patterns:\n\n1. Ensure the API call is assigned to a variable.\n2. Ensure that variable is returned at the end of the function.\n\n``` python\ndef revise_draft(prompt):\n# Example of the correct flow\nresponse = client.chat.completions.create(\nmodel=\"gpt-4o-2024-08-06\",\nmessages=[{\"role\": \"user\", \"content\": prompt}]\n)\n# If you only print(response) here, the test will fail.\nreturn response\n```\n\nIf 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.\n\n## Handling the prompt content issue\n\nLooking 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.\"\n\nThis 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.\n\n## Debugging the test environment\n\nIf 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.\n\nWhen 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.\n\n[Next Why is managing ChatGPT custom instructions such a chore? →](https://promptcube3.com/en/threads/9929/)\n\n## All Replies （1）\n\nWant a live back-and-forth? [Join the global AI chat room](https://promptcube3.com/en/chat/) — login to talk.\n\nCheck if you actually saved the notebook. Auto-graders often run stale versions, ignoring your latest `revise_draft` fixes.", "url": "https://wpnews.pro/news/m2-graded-lab-question", "canonical_source": "https://promptcube3.com/en/threads/9952/", "published_at": "2026-10-11 17:53:49+00:00", "updated_at": "2026-10-11 18:28:48.438277+00:00", "lang": "en", "topics": ["ai-tools", "large-language-models", "developer-tools"], "entities": ["OpenAI", "gpt-4o-2024-08-06", "revise_draft", "chat.completions.create", "ChatGPT"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/m2-graded-lab-question", "markdown": "https://wpnews.pro/news/m2-graded-lab-question.md", "text": "https://wpnews.pro/news/m2-graded-lab-question.txt", "jsonld": "https://wpnews.pro/news/m2-graded-lab-question.jsonld"}}