Get startedGet started for free

Using Items for Custom Handling

You're building a chatbot that needs to provide detailed logging for debugging purposes. The chatbot uses a reasoning-enabled model, and you want to create custom output messages that clearly distinguish between reasoning summaries and assistant responses. You have a response object from a previous API call that contains multiple output items.

This exercise is part of the course

Working with the OpenAI Responses API

View Course

Exercise instructions

  • Loop through each item in response.output.
  • Check if the item type is 'reasoning' and print a formatted message showing the reasoning summary if it exists.
  • Check if the item type is 'message' and print a formatted message with the assistant's text output.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Loop through each item in the response output
for item in response.____:
    # Check if the item is a reasoning item
    if item.____ == '____':
        if item.____:
            print(f"Reasoning: {item.____[0]}")
        else:
            print("No reasoning summary found.")   
    
    # Check if the item is a message item
    if item.____ == 'message':
        print(f"Assistant: {item.____[0].text}")
Edit and Run Code