AttributeError
builtins.AttributeError: 'TextBlock' object has no attribute 'output_text'
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in process_response
result = response.output_text
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'TextBlock' object has no attribute 'output_text' Why it happens
The OpenAI Responses API (v1.60+) returns response content as TextBlock objects from the new responses.create() method. These objects expose the text content via the text property, not output_text. This attribute name differs from legacy Assistants API patterns and catches developers migrating their code. Attempting to access a non-existent attribute raises AttributeError immediately.
Detection
Add type hints to your response handling code (e.g., response: Response) and use IDE autocomplete to verify available attributes. Log the response object type and dir(response) before accessing properties to catch attribute mismatches early.
Causes & fixes
Accessing response.output_text instead of response.text on TextBlock objects
Replace response.output_text with response.text or response.content[0].text for the first text block in the response
Migrating from legacy Assistants API and using outdated attribute names
Review OpenAI SDK v1.60+ Responses API docs and update all response property accesses to the new TextBlock schema: use .text instead of .output_text
Iterating over response.content but accessing wrong property on each block
Use for block in response.content: if hasattr(block, 'text'): print(block.text) to safely extract text from response blocks
Assuming response object is a string or has string-like attributes
Inspect the actual response type with type(response) and response.model_dump() to understand the structure before accessing nested properties
Code: broken vs fixed
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# Using Responses API (v1.60+)
response = client.responses.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What is 2+2?"}]
)
# BROKEN: Accessing non-existent attribute
result = response.output_text # AttributeError raised here
print(result) import os
from openai import OpenAI
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# Using Responses API (v1.60+)
response = client.responses.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What is 2+2?"}]
)
# FIXED: Access text property from the response object
# Response is a stateful object; extract text from first content block
if response.content and len(response.content) > 0:
result = response.content[0].text # Correct attribute path
print(f"Assistant: {result}")
else:
print("No text content in response") Workaround
If you need immediate compatibility without refactoring, create a helper function that safely extracts text: def get_response_text(response): return next((block.text for block in response.content if hasattr(block, 'text')), None). This extracts text from the first TextBlock without raising AttributeError if the property doesn't exist.
Prevention
Always test response objects by logging their structure (response.model_dump_json()) before accessing properties. Add type hints using openai.types.Response to enable IDE autocomplete and catch attribute errors at development time. Pin your openai SDK version and review release notes when updating to catch API changes.