Connect Python SDK to Unsloth
Guide to calling Unsloth's local API from Python using the official OpenAI or Anthropic SDKs including streaming, vision, function calling, and Unsloth's built-in server-side tools.
🔑 Prerequisites
curl http://localhost:8888/v1/models -H "Authorization: Bearer sk-unsloth-…"
export UNSLOTH_STUDIO_AUTH_TOKEN=sk-unsloth-xxxxxxxxxxxx🤖 OpenAI SDK
pip install openaiimport os
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8888/v1", # your unsloth port + /v1
api_key=os.environ["UNSLOTH_STUDIO_AUTH_TOKEN"], # your sk-unsloth-… key
)Basic chat completion

Streaming

Images (vision)

Function calling (OpenAI tools)

Unsloth server-side tools (shorthand)



🧠 Anthropic SDK
Basic message

Streaming
Images (vision)

Tool calling (Anthropic tools)

Unsloth server-side tools (shorthand)


JSON decoding (response_format)
response_format)
Strip the markdown fence Gemma 4 wraps around the JSON, then parse.
🧪 Choosing an SDK
❔ Troubleshooting
Last updated
Was this helpful?

