High severity beginner · Fix: 2-5 min

AttributeError

builtins.AttributeError

What this error means
The OpenAI Python SDK's response.choices[0].message.parsed attribute is None because the LLM output does not match the expected structured format or the response was not parsed as JSON.

Stack trace

traceback
AttributeError: 'NoneType' object has no attribute 'get'
  File "app.py", line 42, in <module>
    data = response.choices[0].message.parsed  # This returns None and causes error
QUICK FIX
Set response_format={"type": "json_object"} in your OpenAI chat completion call to get a parsed JSON object in message.parsed.

Why it happens

The OpenAI SDK's message.parsed attribute is None when the LLM response is not valid JSON or does not conform to the expected structured output format. This often happens if the model returns text with markdown fences, extra commentary, or formatting that prevents automatic JSON parsing.

Detection

Check if response.choices[0].message.parsed is None before accessing its fields, and log the raw response content to detect format mismatches early.

Causes & fixes

1

The LLM response includes markdown fences or extra text around the JSON output.

✓ Fix

Modify your prompt to instruct the model to return only raw JSON without markdown fences, or use a parser that strips fences automatically.

2

The model used is not instruction-tuned and ignores output format instructions.

✓ Fix

Switch to an instruction-tuned model like gpt-4o-mini or claude-3-5-haiku-20241022 that reliably follows output format instructions.

3

The response_format parameter is not set to 'json' or a structured format in the OpenAI chat completion call.

✓ Fix

Set response_format={"type": "json_object"} in the chat.completions.create() call to ensure the SDK parses the response automatically.

Code: broken vs fixed

Broken - triggers the error
python
from openai import OpenAI
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Give me JSON data."}]
)

# This line causes AttributeError because parsed is None
data = response.choices[0].message.parsed  # Broken: parsed is None
print(data)
Fixed - works correctly
python
from openai import OpenAI
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Give me JSON data."}],
    response_format={"type": "json_object"}  # Added to get parsed JSON
)

data = response.choices[0].message.parsed  # Now parsed is a dict
print(data)  # Prints the parsed JSON object
Added response_format={"type": "json_object"} to the chat completion call so the SDK parses the LLM output as JSON, preventing parsed from being None.

Workaround

Wrap access to message.parsed in a try/except or if-check; if None, parse response.choices[0].message.content manually with json.loads() after stripping markdown fences.

Prevention

Use the OpenAI SDK's response_format parameter to enforce structured JSON output at the API level, avoiding parsing errors and None values in message.parsed.

Python 3.9+ · openai >=1.0.0 · tested on 1.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.