# openai-python — Official OpenAI Python SDK > Call the OpenAI REST API from Python 3.9+ with typed request/response models and sync/async clients. Use it as a core SDK for agents and app backends. ## Install Save as a script file and run: # openai-python — Official OpenAI Python SDK > Call the OpenAI REST API from Python 3.9+ with typed request/response models and sync/async clients. Use it as a core SDK for agents and app backends. ## Quick Use 1. Install: ```bash pip install openai ``` 2. Run: ```bash python -c "import openai; print(getattr(openai, '__version__', 'version?'))" ``` 3. Verify: - Make one authenticated request (e.g., list models) and confirm the client handles errors and retries as expected. --- ## Intro Call the OpenAI REST API from Python 3.9+ with typed request/response models and sync/async clients. Use it as a core SDK for agents and app backends. - **Best for:** Python developers building LLM apps who want typed clients and a stable upgrade path - **Works with:** Python 3.9+, OpenAI API, httpx-powered sync/async clients - **Setup time:** 6 minutes ### Quantitative Notes - Setup time ~6 minutes (pip install + set API key + one request) - GitHub stars + forks (verified): see Source & Thanks - Python requirement: 3.9+ (repo README) --- ## Practical Notes Keep SDK usage behind a thin adapter in your codebase (one module) so upgrades don’t ripple through your app. Centralize timeouts/retries and log request IDs. For agents, restrict tool schemas and validate all model outputs before execution. **Safety note:** Never hardcode API keys; use env vars/secret managers and scrub logs. ### FAQ **Q: Do I need an API key to import it?** A: No. You can import and build clients without a key; you need a key to call the API. **Q: Does it support async?** A: Yes. The library provides synchronous and asynchronous clients. **Q: How do I manage rate limits?** A: Use retries/backoff, batch where possible, and log request IDs for debugging. --- ## Source & Thanks > GitHub: https://github.com/openai/openai-python > Owner avatar: https://avatars.githubusercontent.com/u/14957082?v=4 > License (SPDX): Apache-2.0 > GitHub stars (verified via `api.github.com/repos/openai/openai-python`): 30,747 > GitHub forks (verified via `api.github.com/repos/openai/openai-python`): 4,777 --- # openai-python——OpenAI 官方 Python SDK > 在 Python 3.9+ 中以同步/异步方式调用 OpenAI REST API,并获得类型化的请求/响应模型;适合作为 agent 与后端服务的核心 SDK,并便于集中管理重试/超时与日志。 ## 快速使用 1. 安装: ```bash pip install openai ``` 2. 运行: ```bash python -c "import openai; print(getattr(openai, '__version__', 'version?'))" ``` 3. 验证: - Make one authenticated request (e.g., list models) and confirm the client handles errors and retries as expected. --- ## 简介 在 Python 3.9+ 中以同步/异步方式调用 OpenAI REST API,并获得类型化的请求/响应模型;适合作为 agent 与后端服务的核心 SDK,并便于集中管理重试/超时与日志。 - **适合谁(Best for):** 做 LLM 应用的 Python 开发者,希望使用类型化 client 并保持稳定升级路径 - **兼容工具(Works with):** Python 3.9+、OpenAI API、基于 httpx 的同步/异步 client - **安装时间(Setup time):** 6 分钟 ### 量化信息 - 跑通约 6 分钟(pip 安装 + 配 API key + 一次请求) - GitHub stars + forks(已核验):见「来源与感谢」 - Python 版本要求:3.9+(仓库说明) --- ## 实战要点 建议把 SDK 使用封装在一层很薄的适配器里(一个模块),避免升级时全局改动。统一配置 timeout/重试并记录 request_id。做 agent 时要收敛工具 schema,并对模型输出做校验后再执行。 **安全提示:** 不要把 API key 写死在代码里;用环境变量/密钥管理,并清理日志中的敏感信息。 ### FAQ **Q: 导入库需要 API key 吗?** A: 不需要。创建 client/导入不依赖 key;调用 API 才需要。 **Q: 支持异步吗?** A: 支持,提供同步与异步 client。 **Q: 怎么处理限流?** A: 用重试/退避策略,能批量就批量,并记录 request_id 便于排障。 --- ## 来源与感谢 > GitHub:https://github.com/openai/openai-python > Owner avatar:https://avatars.githubusercontent.com/u/14957082?v=4 > 许可证(SPDX):Apache-2.0 > GitHub stars(已通过 `api.github.com/repos/openai/openai-python` 核验):30,747 > GitHub forks(已通过 `api.github.com/repos/openai/openai-python` 核验):4,777 --- Source: https://tokrepo.com/en/workflows/openai-python-official-openai-python-sdk Author: Agent Toolkit