SDKs overview
OpenAI provides Software Development Kits (SDKs) and client libraries to facilitate interaction with its API endpoints. These SDKs abstract the underlying HTTP requests and response parsing, allowing developers to integrate OpenAI's models and services using language- idiomatic code. The official SDKs are maintained by OpenAI and typically offer the most up-to-date features and support for new models and API capabilities, such as function calling and JSON mode.
For languages without official support, community-developed libraries often fill the gap, providing similar functionalities. These libraries may vary in their feature completeness, maintenance frequency, and adherence to the latest API specifications. Developers should consult the respective library's documentation for details on supported features and compatibility with specific OpenAI models and API versions.
Official SDKs by language
OpenAI currently maintains official SDKs for Python, Node.js, Go, and Java. These libraries are designed to provide a consistent and streamlined experience for developers, offering direct access to services like Chat Completions, Assistants API, Embeddings, and Image Generation. Each SDK includes methods for authentication, request construction, and response handling, simplifying the integration of AI capabilities into applications.
The table below summarizes the official OpenAI SDKs, their package names, and typical installation commands:
| Language | Package Name | Installation Command | Maturity |
|---|---|---|---|
| Python | openai |
pip install openai |
Stable |
| Node.js | openai |
npm install openai or yarn add openai |
Stable |
| Go | github.com/openai/openai-go/openai |
go get github.com/openai/openai-go/openai |
Stable |
| Java | com.openai:openai-java |
Maven: Add dependency in pom.xml, Gradle: Add dependency in build.gradle |
Stable |
Installation
Installation procedures vary by programming language and package manager. Ensure you have the respective language runtime and package manager installed on your system before proceeding. You will also need an OpenAI API key, which typically starts with sk-. This key should be kept secure and handled as a confidential credential.
Python
pip install openai
To use the Python SDK, ensure your API key is set as an environment variable (OPENAI_API_KEY) or passed directly during client initialization.
Node.js
npm install openai
# or
yarn add openai
For Node.js, the API key can be provided via the OPENAI_API_KEY environment variable or directly to the client constructor, similar to the Python SDK. The module system in JavaScript means you might use import or require depending on your project setup.
Go
go get github.com/openai/openai-go/openai
The Go SDK supports API key configuration typically through environment variables or by providing it directly to the client. Refer to the OpenAI Go SDK documentation for detailed usage patterns.
Java
For Maven projects, add the following dependency to your pom.xml:
<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>0.17.0</version> <!-- Check for the latest version -->
</dependency>
For Gradle projects, add to your build.gradle:
implementation 'com.openai:openai-java:0.17.0' // Check for the latest version
After adding the dependency, build your project to download the library. API key configuration in Java typically involves setting an environment variable or passing the key during client instantiation.
Quickstart example
This Python example demonstrates how to use the official OpenAI SDK to get a chat completion from the gpt-4o-mini model. It illustrates basic client initialization, sending a message, and processing the response.
from openai import OpenAI
# Ensure your OPENAI_API_KEY environment variable is set
client = OpenAI()
def get_chat_completion(prompt):
try:
response = client.chat.completions.create(
model="gpt-4o-mini", # Using the latest recommended mini model
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=150
)
return response.choices[0].message.content
except Exception as e:
return f"An error occurred: {e}"
if __name__ == "__main__":
user_prompt = "Explain the concept of recursion in programming in simple terms."
completion = get_chat_completion(user_prompt)
print(completion)
This example showcases an asynchronous approach for Node.js using the official OpenAI SDK to generate a chat completion. The API key is assumed to be available as an environment variable.
import OpenAI from 'openai';
const openai = new OpenAI(); // Reads OPENAI_API_KEY from process.env
async function getChatCompletion(prompt) {
try {
const chatCompletion = await openai.chat.completions.create({
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: prompt },
],
model: 'gpt-4o-mini',
temperature: 0.7,
max_tokens: 150,
});
return chatCompletion.choices[0].message.content;
} catch (error) {
console.error('Error fetching chat completion:', error);
return `An error occurred: ${error.message}`;
}
}
(async () => {
const userPrompt = "What are the main benefits of using TypeScript over JavaScript for large projects?";
const completion = await getChatCompletion(userPrompt);
console.log(completion);
})();
Community libraries
Beyond the official SDKs, the developer community has created libraries for various other languages and specific use cases. These libraries can offer different paradigms, convenience functions, or support for languages not officially covered by OpenAI. While often robust, community libraries may not always keep pace with the latest API changes as quickly as official offerings or may have different support models.
.NET (C#)
For .NET developers, the OpenAI-API-dotnet library is a widely used community-maintained client. It provides a C# interface to OpenAI's services, allowing integration into .NET applications. Installation is typically done via NuGet Package Manager:
Install-Package OpenAI
This library generally aims to mirror the functionality of the official Python and Node.js SDKs, providing type-safe access to the API. Developers should check the OpenAI-API-dotnet GitHub repository for the latest features, usage examples, and compatibility information.
Other community efforts include integrations with specific frameworks or higher-level abstractions built on top of the OpenAI API, such as those within the Semantic Kernel project by Microsoft, which aims to provide a lightweight SDK for combining LLMs with conventional programming languages.