cd /news/ai-tools/m2-graded-lab-question · home › topics › ai-tools › article
[ARTICLE · art-149252] src=promptcube3.com ↗ pub= topic=ai-tools verified=true sentiment=· neutral

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.

read3 min views1 publishedOct 11, 2026
M2 Graded Lab question
Image: Promptcube3 (auto-discovered)

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.
def revise_draft(prompt):
response = client.chat.completions.create(
model="gpt-4o-2024-08-06",
messages=[{"role": "user", "content": prompt}]
)
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? →

All Replies (1) #

Want a live back-and-forth? Join the global AI chat room — login to talk.

Check if you actually saved the notebook. Auto-graders often run stale versions, ignoring your latest revise_draft fixes.

── more in #ai-tools 4 stories · sorted by recency
── more on @openai 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/m2-graded-lab-questi…] indexed:0 read:3min 2026-10-11 · —