跳到正文

第 3 课 · Getting Started

Your First Request with the Responses API

Send your first Responses API request in Python or curl, read output_text and output items, check status and usage, and compare it with Chat Completions.

You have a key and an SDK, so you can make a real call. A basic request needs only two things: a model and some input. The response is a richer object than a single string, and learning its shape now saves you many bugs later.

The code that talks to OpenAI in this lesson is read-only here. Run it on your own computer. A runnable stand-in then lets you practice reading a response.

The smallest request

Python
# Read-only: needs your own API key
from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="gpt-6.2-sol",
    input="Explain what an HTTP status code is in two sentences.",
)

print(response.output_text)

The same request with curl shows what the SDK sends for you:

Terminal
curl https://api.openai.com/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-6.2-sol",
    "input": "Explain what an HTTP status code is in two sentences."
  }'

gpt-6.2-sol is a model ID that was current as of December 2026. Models are replaced and retired over time, so keep the ID in one configuration setting. Models and reasoning effort explains how to choose.

Your answer will not be the same on every run. Models generate text with some variation, so write code and tests that do not expect one exact sentence.

What comes back

The response object has many fields. These are the ones you will use first.

Field Meaning
id Identifier of this response
status For example completed or incomplete
output A list of items the model produced
output_text SDK shortcut that joins the text output
usage Token counts for the request and reply
incomplete_details Why a response stopped early, if it did

The important idea is that output is a list of items. A reply can contain a message, a reasoning item, one or more tool calls, or results from built-in tools. A message item has the role assistant and a list of content parts, such as output_text or a refusal.

Do not assume the text is at output[0].content[0].text. The first item may be reasoning or a tool call. Use output_text when you only want text, or loop over the items and check each type.

Practice reading output

This stand-in contains a hand-made response with the same general shape. The text is illustrative and no model produced it.

Python Stand-in: walk through output items

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Try changing the message part to {"type": "refusal", "refusal": "I cannot help with that."} and run it again. Your program now has no text but has a refusal to show or log.

Check the status and set an output limit

You can cap how much the model generates with max_output_tokens. When a reply hits the cap, the status is incomplete.

Python
# Read-only
response = client.responses.create(
    model="gpt-6.2-sol",
    input="Summarize the rules of chess.",
    max_output_tokens=4000,
)

if response.status == "incomplete":
    reason = response.incomplete_details.reason
    print("Stopped early:", reason)
else:
    print(response.output_text)

print(response.usage.input_tokens, response.usage.output_tokens)

The cap covers more than the visible words. On reasoning models, hidden reasoning tokens count too. A very low cap can therefore give you an incomplete response with no visible text at all. OpenAI's reasoning guide suggests leaving generous room while you are still learning how much your tasks use.

Responses and Chat Completions

Older tutorials and many existing programs use Chat Completions. It takes a list called messages and returns a list called choices.

Python
# Read-only: the older style you may meet in existing code
completion = client.chat.completions.create(
    model="gpt-6.2-sol",
    messages=[{"role": "user", "content": "Say hello."}],
)
print(completion.choices[0].message.content)
Idea Chat Completions Responses
Input field messages input
Output choices[...].message output items
Server-side conversation state You resend messages Optional built-in features
Built-in tools More limited Broad support

Chat Completions is still supported as of December 2026, but the documentation for the newest models says that tool calling needs the Responses API. For new work, start with Responses. A third interface, the Assistants API, was shut down in August 2026. If you find sample code that creates assistants, threads and runs, it no longer works.

When something fails

Calls can fail before any model runs: a wrong key, no network, a missing model name or a rate limit. The SDK raises an exception in these cases. A simple first guard looks like this:

Python
# Read-only
import openai

try:
    response = client.responses.create(model="gpt-6.2-sol", input="Hi")
except openai.AuthenticationError:
    print("Check your API key.")
except openai.APIError as error:
    print("API problem:", error)

You will build a fuller strategy in errors, retries and rate limits.

Reading a response in your logs

When you start out, print more than the text. The response id, the model, the status and the usage numbers tell you what really happened. If an answer is short or empty, the status often explains why. If a call costs more than you expected, compare input and output tokens. If a reply contains no text at all, list the type of each output item: you may find a tool call that your program did not handle, or a refusal.

Python
# Read-only: a debugging summary
print(response.id, response.model, response.status)
print([item.type for item in response.output])
print(response.usage.input_tokens, response.usage.output_tokens)

Wrap the call in one function

Do not scatter API calls across your program. Put them behind one small function of your own, such as generate_reply(question). That function chooses the model, sets limits, checks the status and returns either text or a clear failure. The rest of your code then does not depend on the shape of the SDK. You get one place for logging and error handling, and in tests you can replace the function with a fake. When an API detail changes, you edit one file.

Keep prompts and settings near this function, so a reader can see everything that affects the answer in one place.

Common mistakes

  • Reading the text before checking the status, so a cut-off or refused reply is treated as a good answer.
  • Writing the model ID in many places, which turns a model change into a search through the whole project.
  • Copying an old sample that uses a retired interface, then debugging your own code for an error that comes from the sample.
  • Testing with one friendly question only. Try an empty string, a very long input and a question in another language before you rely on the call.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which two arguments make up a basic Responses request?

    A model ID and the input are enough for a simple text request.
  2. 2. Why should you not read output[0] and assume it is text?

    Loop over the items and check their types, or use output_text for the combined text.
  3. 3. What can happen if max_output_tokens is very small on a reasoning model?

    Reasoning tokens count against the same limit as visible output.
  4. 4. What is the status of the Assistants API as of December 2026?

    It was shut down in August 2026. The Responses API and Conversations API replace it.

Summary

  • A basic call is client.responses.create(model=..., input=...).
  • output is a list of typed items; output_text is a convenient shortcut for text.
  • Check status, and read usage to see what a call consumed.
  • max_output_tokens includes reasoning tokens.
  • Use Responses for new code; recognize Chat Completions in old code.

Next: Instructions and Message Roles

你的学习进度保存在此浏览器中,无需账号。