gitmyhub

openai-python

Python ★ 31k updated 3d ago

The official Python library for the OpenAI API

openai-python is OpenAI's official Python library that lets you call GPT and other OpenAI models from Python in a few lines of code, handling authentication, streaming, vision inputs, and both sync and async clients automatically.

Pythonhttpxsetup: easycomplexity 2/5

openai-python is the official Python library for the OpenAI API. The OpenAI API is the HTTP service that lets a program send a prompt and receive a generated response from OpenAI's language models. Calling that service directly from code means building HTTP requests, formatting JSON bodies, and parsing replies by hand. This library wraps all of that into ready-made Python objects so a developer can talk to the models in a few lines of code. The way it works is that you install the package and create a client object, optionally passing an API key (the README recommends keeping the key out of source control by loading it from an environment variable through a .env file). The library exposes the primary Responses API, used by calling create with a model name, optional instructions, and the input text — the model's reply is returned in a structured object you can read fields from. The older Chat Completions API is also supported indefinitely, with the same shape but a list of role-tagged messages. Beyond plain text the README documents vision input (sending an image URL alongside a prompt), streaming responses delivered as Server-Sent Events so the answer appears word by word, both synchronous and asynchronous clients (powered by the httpx HTTP library), and workload identity authentication that swaps long-lived API keys for short-lived tokens issued by Kubernetes service accounts, Azure managed identity, or Google Cloud's metadata service. The library is generated from OpenAI's OpenAPI specification with Stainless, and includes type definitions for every request and response field. You would use this library whenever you are building a Python application — a script, a backend service, a notebook, a CLI tool — that needs to call OpenAI's models for text generation, chat, image-aware prompts, or any other supported endpoint. It targets Python 3.9 and newer and is distributed on PyPI.

Where it fits