Lesson 2 of 2 Building a Chat Client with the Foundry SDK

Building a Chat Client with the Responses API

This lesson explains how a chat application sends the user's input to a deployed model and prints the reply through the Responses API, and how it keeps the conversation's context.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the arguments of responses.create().
  • Compare the ways a chat client keeps context between turns.
  • Describe how streaming and error handling work.

Calling the model

The Responses API is the recommended API for generating responses. responses.create() takes the deployment name as model, optional instructions (the system prompt), the input (a string or a list of messages) and parameters such as max_output_tokens. The response's output_text property contains the generated text, and usage reports input and output tokens. The older Chat Completions API, which takes a list of system, user and assistant messages, is still supported.

response = openai.responses.create(
    model="gpt-5.4-mini",                       # the deployment name
    instructions="You are a concise assistant for a hiking equipment shop.",
    input="Which sleeping bag rating do I need for spring camping?",
)
print(response.output_text)
print(response.usage.input_tokens, response.usage.output_tokens)
Match the pairs

Inside a Responses API call

Exam skills 2.1.3

Match each part of a Responses API request or response with what it holds.

Log in to try this activity Interactive activities are free with an account.

Keeping context between turns

Models do not remember earlier requests. A chat client keeps context in one of three ways. It can resend the history, appending each user message and model reply to a list and sending the whole list as input; this is explicit but grows the prompt (and cost) every turn. It can pass previous_response_id, so that the service links the new request to the stored previous response and its context. Or it can create a conversation with conversations.create() and pass its ID with each request, which stores the conversation's items durably.

previous_id = None
while True:
    user_text = input("You: ")
    if user_text.lower() in ("quit", "exit"):
        break
    response = openai.responses.create(
        model="gpt-5.4-mini",
        instructions="You are a concise assistant for a hiking equipment shop.",
        input=user_text,
        previous_response_id=previous_id,      # links this turn to the last one
    )
    previous_id = response.id
    print("Assistant:", response.output_text)

The following diagram compares the three ways to keep context.

Three ways for a chat client to keep context, because the model does not remember earlier requests. Resending the history sends every earlier message and reply each turn, which is explicit but makes the prompt and cost grow. Passing previous_response_id lets the service link the new request to the stored previous response. Creating a conversation and passing its ID with each request stores the conversation's items durably.
Without one of them, every turn starts from nothing.
Sort into groups

Three ways to keep context

Exam skills 2.1.3

Decide which way of keeping conversation context each description belongs to.

Log in to try this activity Interactive activities are free with an account.

Streaming and errors

Passing stream=True returns events as the model generates tokens, so a chat interface can display text progressively; the client prints the text of each response.output_text.delta event. Applications should also handle errors: HTTP 429 means the deployment's rate limit was exceeded and the request should be retried after a delay, and a content-filter error means a guardrail blocked the prompt or response.

Scenario questions

Streams and errors

Exam skills 2.1.3

Handle streaming events and errors in a chat client.

Log in to try this activity Interactive activities are free with an account.
Worked example

A chat that forgets

A developer's chat client answers Which tents do you sell? correctly, but when the user then asks Which of those is lightest? the model replies that it does not know what those refers to. Each turn calls responses.create(model=..., input=user_text).

Each request is independent, so the second turn has no context. Passing the previous response's ID as previous_response_id, using a conversation, or sending the accumulated message history fixes it. Increasing max_output_tokens or choosing a larger model would not, because the missing information was never sent.

Check yourself

What does stream=True do in a Responses API call?

Show answer

It returns the output progressively as it is generated, instead of all at once.

Exam Tips

Remember for the exam

  • responses.create() takes the deployment name, instructions and input; output_text holds the reply.
  • Keep context with message history, previous_response_id or a conversation.
  • stream=True returns output progressively; HTTP 429 calls for retry with backoff.