Skip to main content
Use the official OpenAI Python SDK. Set the environment variables first. Run these examples on a server or in a terminal.

Install

Requires Python 3.10+.
These are the versions used to verify the examples. Upgrade according to your dependency policy and retest.

Chat Completions

Save as chat.py.

Responses

Save as responses.py. Select a Responses-compatible model first.

Run and handle results

Each command sends a real request; run only the one you need. Examples use a 60-second timeout and disable SDK retries to avoid implicit repeated requests. Design timeout, retry, and concurrency policies for production. Chat Completions text can be empty, for example during tool calls. The Responses example requires completed status. output_text is an SDK convenience property that joins text; handle tools and other non-text output separately. Streaming · Error handling Reference: Official OpenAI Python SDK documentation。 The text-only Responses example rejects empty text, refusals, tool calls, and incomplete output. Continue with the full Responses examples in conversations, tool calling, and structured output. 简体中文