Instructions to use openai/gpt-oss-20b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use openai/gpt-oss-20b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="openai/gpt-oss-20b", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("openai/gpt-oss-20b") model = AutoModelForCausalLM.from_pretrained("openai/gpt-oss-20b", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- AMD Developer Cloud
- Local Apps Settings
- vLLM
How to use openai/gpt-oss-20b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "openai/gpt-oss-20b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "openai/gpt-oss-20b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/openai/gpt-oss-20b
- SGLang
How to use openai/gpt-oss-20b with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "openai/gpt-oss-20b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "openai/gpt-oss-20b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "openai/gpt-oss-20b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "openai/gpt-oss-20b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use openai/gpt-oss-20b with Docker Model Runner:
docker model run hf.co/openai/gpt-oss-20b
Tokenizer vocab size doesn't match model vocab size
The gpt-oss-20b model configuration specifies a vocab size of 201088. The tokenizer in this repository has 199998 tokens in its data file and 21 additional special tokens in tokenizer_config.json, for a total of 200019 tokens.
Code to replicate these numbers:
>>> import transformers
>>> model = transformers.AutoModelForCausalLM.from_pretrained("openai/gpt-oss-20b")
>>> tokenizer = transformers.AutoTokenizer.from_pretrained("openai/gpt-oss-20b")
>>> print(f"{model.vocab_size=}")
model.vocab_size=201088
>>> print(f"{tokenizer.vocab_size=}")
tokenizer.vocab_size=199998
>>> print(f"{len(tokenizer)=}")
len(tokenizer)=200019
Users who attempt to, for example, configure constrained decoding using the tokenizer's vocab size of 200019 will encounter errors when the model emits token IDs that are greater than 200019.
Can you please upload an updated tokenizer config that adds the missing 1069 reserved tokens?
Hi @frreiss , code to convert the tokenizer (tiktoken) in gpt-oss to a HF compatible format.
Tested with Python 3.10+
from pathlib import Path
from transformers.integrations.tiktoken import convert_tiktoken_to_fast
from tiktoken import encoding_name_for_model
from transformers import PreTrainedTokenizerFast
model_name = "gpt-oss-20b"
encoding = encoding_name_for_model(model_name)
outdir = Path(f"tokenizer/{encoding}")
outdir.mkdir(parents=True, exist_ok=True)
convert_tiktoken_to_fast(encoding, outdir)
tokenizer = PreTrainedTokenizerFast.from_pretrained(outdir)
print(f"{len(tokenizer)=}")
vocab_size = tokenizer.vocab_size
added_vocab_size = len(tokenizer.get_added_vocab().keys())
print(f"{vocab_size=}")
print(f"{added_vocab_size=}")
print(f"{(vocab_size+added_vocab_size)=}")
Output
len(tokenizer)=201089
vocab_size=199998
added_vocab_size=1091
(vocab_size+added_vocab_size)=201089