# M2 Graded Lab question

> Source: <https://promptcube3.com/en/threads/9952/>
> Published: 2026-10-11 17:53:49+00:00

# 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.
