LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Working with the OpenAI Responses API

Kurs anzeigen

Anleitung zur Übung

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

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# 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}")
Code bearbeiten und ausführen