Instructions to use google/gemma-4-31B-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-4-31B-it with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="google/gemma-4-31B-it") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("google/gemma-4-31B-it") model = AutoModelForImageTextToText.from_pretrained("google/gemma-4-31B-it") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.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(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- AMD Developer Cloud
- Local Apps Settings
- vLLM
How to use google/gemma-4-31B-it with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-4-31B-it" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-4-31B-it", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/google/gemma-4-31B-it
- SGLang
How to use google/gemma-4-31B-it 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 "google/gemma-4-31B-it" \ --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": "google/gemma-4-31B-it", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'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 "google/gemma-4-31B-it" \ --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": "google/gemma-4-31B-it", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use google/gemma-4-31B-it with Docker Model Runner:
docker model run hf.co/google/gemma-4-31B-it
fix: chat template โ null handling, reasoning preservation, turn-tag balance, input validation
Summary
Improves Gemma4 chat template:
Bug fixes
Nonevalues now render asnullinstead of Python'sNone- String-typed
tool_calls[].function.argumentsnow raises a clear error instead of silently producing malformed DSL - Prior-turn reasoning/thinking is preserved across multi-turn tool-call chains (
preserve_thinkingflag, default=true) - Consecutive assistant messages now produce balanced
<|turn>model/<turn|>tags via forward-scan continuation detection
Improvements
enable_thinkingnormalized once with| default(false), eliminating repetitiveis defined andchecksimage_urlandinput_audiocontent types now map to<|image|>and<|audio|>(OpenAI compatibility)- Empty
messages=[]handled gracefully instead of crashing - Unmatched
tool_call_idin tool responses falls back to'unknown'instead of crashing - Consistent
.get()access preventsStrictUndefinederrors for optional message keys - O(1) backward scan for model-turn continuation (was O(n) per message)
Thanks for the commit, I hope this template changes fix premature ending problems. First in the morning to apply these new changes!
I am convincing teams that we can fix the issues, we should not give up and transfer to Qwen models lol. Cannot let gemma4 go.
I've been sharing this publicly which it seems you fix a few things here as well: https://gist.github.com/jscott3201/ad69c4ffbd79f18b11a0f6a94c94fadf
I can move this to a repo with MIT/Apache2 if you want to pull anything off this template for the official template.
I've been sharing this publicly which it seems you fix a few things here as well: https://gist.github.com/jscott3201/ad69c4ffbd79f18b11a0f6a94c94fadf
I can move this to a repo with MIT/Apache2 if you want to pull anything off this template for the official template.
I have been running your template but can you now comment on which is better - Google's new one or yours and will you be coming up with a new one incorporating google's changes?
I would have to diff them but they designed the LLM so I'll lean towards theirs being correct! Their 4 noted issues align with my callouts in the header.