DEV Community

shashank ms
shashank ms

Posted on

Introduction to OpenAI SDK: A Beginner's Guide

The OpenAI SDK is the de facto standard for interacting with large language models in Python and Node.js. It provides a typed, promise-based client that handles authentication, request serialization, and streaming with minimal boilerplate. If you are building your first AI-powered application, learning this SDK gives you a portable skill set that works across dozens of providers, including Oxlo.ai.

What Is the OpenAI SDK?

The OpenAI SDK is the official client library for OpenAI's REST API. Available for Python, Node.js, and any language that can send HTTP requests, it wraps endpoints for chat completions, embeddings, image generation, audio transcription, and text-to-speech into a single, typed interface. Because it is so widely adopted, many third-party providers implement the same schema, which means the skills you learn here transfer directly to other platforms. Oxlo.ai is one such provider, offering full compatibility so that every method and object you see below works without code changes.

Installation

You only need the package and an API key to begin.

pip install openai

For Node.js, run:

npm install openai

Store your key in an environment variable rather than committing it to version control.

Your First Request

The canonical entry point is the chat completions endpoint. The following Python script sends a system message and a user message, then prints the assistant's reply.

import os
from openai import OpenAI

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain how API clients work in one paragraph."}
    ]
)

print(response.choices[0].message.content)

The OpenAI object is a lightweight wrapper around an HTTP session. It automatically serializes the messages list into JSON, sets the correct headers, and parses the response into native objects.

Chat Completions and Messages

Every request to the chat endpoint requires a messages array. Each element is a dictionary with a role and content field. The three primary roles are system, which sets behavior; user, which carries the prompt; and assistant, which stores previous replies. For multi-turn conversations, append the assistant's last response to the array before sending the next user message. This pattern is identical whether you are calling OpenAI or Oxlo.ai.

Streaming Responses

Waiting for the entire response to finish can feel sluggish in interactive applications. The SDK supports streaming via the stream=True parameter, which returns a generator that yields partial chunks as the model generates them.

stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Count from 1 to 10 slowly."}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Streaming uses server-sent events under the hood, but the SDK abstracts this into a clean iterator interface.

Function Calling

Modern LLMs can emit structured JSON to invoke external tools. You define available functions in the tools parameter, and the model decides when to call one. The snippet below registers a mock weather function.

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string", "description": "City and country"}
                },
                "required": ["location"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "What is the weather in Tokyo?"}],
    tools=tools
)

print(response.choices[0].message.tool_calls)

If the model decides a tool is relevant, it returns a tool_calls object instead of plain text. Your application executes the function, appends the result as a tool message, and sends the conversation back to the model for final summarization.

Switching to Oxlo.ai

Because Oxlo.ai is fully OpenAI SDK compatible, migrating requires only two changes: point the base_url to https://api.oxlo.ai/v1 and swap the API key. Every pattern shown above, streaming, function calling, multi-turn chat, and JSON mode, works without modification.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key=os.getenv("OXLO_API_KEY")
)

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain how API clients work in one paragraph."}
    ]
)

print(response.choices[0].message.content)

Oxlo.ai offers 45+ open-source and proprietary models, including Llama 3.3 70B, DeepSeek R1 671B MoE, Qwen 3 32B, and Kimi K2.6, all accessible through the same SDK methods. If your workloads involve long contexts or agentic loops, Oxlo.ai's request-based pricing can be significantly cheaper than token-based billing for long-context workloads. You pay one flat cost per API request regardless of prompt length, which removes the surprise of ballooning input tokens during multi-step agent execution. See the pricing page for plan details.

Next Steps

Start with a small project that exercises one endpoint. A command-line chatbot is ideal because it forces you to manage message history, handle streaming output, and optionally integrate function calling. Once you are comfortable with the request lifecycle, experiment with different models by changing the base_url and model string. Oxlo.ai provides a free tier with 60 requests per day and a 7-day full-access trial, so you can test heavier workloads without upfront commitment. Keep the SDK reference handy, and remember that any code you write today remains portable across any OpenAI-compatible provider.

Top comments (0)