A Conversation Example Using the OpenAI Responses API

I’m slowly but surely wading through examples of the OpenAI Responses API. Some people can learn just by reading, but I can only learn by coding.

Briefly, to have a conversation, you need to remember the responses of the previous queries. This is accomplished using a response ID fed to the previous_response_id parameter. My demo asks the GPT LLM to tell a joke, and then asks for an explanation, and then asks for another joke.

There are three requests. In order to avoid repeating the parameters that don’t change (the model to use, the temperature, and so on), I set up an info dictionary:

  # set up basic stuff in dictionary to be unpacked
  info = {
    "model": "gpt-4.1",
    "input": [],
    "temperature": 0.5,  # mildly creative (default top_p)
    "max_output_tokens": 100,
    "store": True,  # is the default
  }

Only the “input” parameter will change. The “store” parameter means remember the response. The first query is:

  query1 = "Tell me a joke."
  print("query1 = ")
  print(query1)
  info["input"] = [{ "role": "user", "content": query1 }]
  response1 = client.responses.create(**info)

The input is set and then the info dictionary is unpacked to the create() call using the special ** syntax. The second query is set up like so:

  query2 = "Briefly, explain to me why this is funny"
  print("query2 = ")
  print(query2)
  info["input"] = [{ "role": "user", "content": query2 }]
  info["previous_response_id"] = response1.id
  response2 = client.responses.create(**info)

The previous_response_id parameter is set to the ID of the previous response, and so the second query knows what the joke was.

In the same way, the third query is constructed so that it knows what the original joke and explanation are, so that it doesn’t repeat the first joke:

  query3 = "Tell me a different joke."
  print("query3 = ")
  print(query3)
  info["input"] = [{ "role": "user", "content": query3 }]
  info["previous_response_id"] = response2.id
  response3 = client.responses.create(**info)

Interesting stuff. One step further in my journey to understand AI using large language models.

Complete demo program:

# conversation_openai.py
# conversation using response.id to maintain state

from openai import OpenAI

print("\nBegin conversation demo ")
try:
  key = "sk-proj-_AX7bGTXUwg-qojh2T5Z2CVXrox" + \
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + \
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + \
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 
  client = OpenAI(api_key=key)

  # set up basic stuff in dictionary to be unpacked
  info = {
    "model": "gpt-4.1",
    # "tools": [],
    # "instructions": are per request
    "input": [],
    "temperature": 0.5,  # mildly creative (default top_p)
    "max_output_tokens": 100,
    "store": True,  # is the default
  }
  
  query1 = "Tell me a joke."
  print("\nquery1 = ")
  print(query1)
  info["input"] = [{ "role": "user", "content": query1 }]
  response1 = client.responses.create(**info)

  print("\nThe reply is: ")
  print(response1.output_text)

  query2 = "Briefly, explain to me why this is funny"
  print("\nquery2 = ")
  print(query2)
  info["input"] = [{ "role": "user", "content": query2 }]
  info["previous_response_id"] = response1.id
  response2 = client.responses.create(**info)
  
  print("\nThe reply is: ")
  print(response2.output_text)

  query3 = "Tell me a different joke."
  print("\nquery3 = ")
  print(query3)
  info["input"] = [{ "role": "user", "content": query3 }]
  info["previous_response_id"] = response2.id
  response3 = client.responses.create(**info)

  print("\nThe reply is: ")
  print(response3.output_text)  

except Exception as e:
  print("Fatal error " + str(e))
 
print("\nEnd conversation demo ")


Humor is a funny thing. What is funny to one person is not funny to another.

My all time favorite party game is “Pit”. It was first produced by the Paker Brothers Company in 1904. It was an immediate success and is still popular today.

The classic version has 63 cards: 9 each of 7 kinds of cards: Wheat, Barley, Corn, Rye, Oats, Hay, Flax. Suppose you are playing with 5 players. You use the first five sets (Wheat, Barley, Corn, Rye, Oats). All the cards are shuffled and dealt out so each player has 9 cards. The goal is to collect all 9 cards of a set.

There are no taking turns. Players shout out, “Two for two!” and hold out two cards that are the same and wait for another player to respond, “OK, Two for two!” and hold out their two cards, and the sets are swapped. Players can shout out, “One for one!” or “Three for three!” or more.

Within seconds, the game descends into absolute chaos, with cards and hands and arms flying everywhere. I often end up laughing so hard I cry. Every single person I know, who has been introduced to “Pit”, loves the game.


This entry was posted in OpenAI. Bookmark the permalink.

Leave a Reply