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)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("google/gemma-4-31B-it") model = AutoModelForMultimodalLM.from_pretrained("google/gemma-4-31B-it", device_map="auto") 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=256) 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 assistant content order before tool calls
Render an assistant's pre-tool content before its tool_calls and the corresponding tool results in google/gemma-4-31B-it.
The current template reorders an OpenAI-style message containing both fields as tool_calls → tool_responses → content. It also uses that earlier explanation to close the model turn after the final tool result, preventing continuation in the same turn.
Move the existing content-rendering block before tool calls, remove has_content, and use the existing next-message scan to decide when to close the turn. This preserves this repository's thinking-channel and multimodal handling.
Example model turn, with content="I'll check." and a weather tool returning sunny:
Before: <|tool_call>...<tool_call|><|tool_response>...<tool_response|>I'll check.<turn|>
After: I'll check.<|tool_call>...<tool_call|><|tool_response>...<tool_response|>
Validation: on B300 with Transformers 5.5.4, rendered this exact repository template using AutoTokenizer.apply_chat_template(..., chat_template=template, tokenize=False, add_generation_prompt=True). The tokenizer was the installed Gemma 4 26B-A4B tokenizer; this is a template-rendering test. With the same message history, the original template fails content ordering and continuation; the patched file passes both, plus the next-user turn boundary. The same checks passed for all 23 standalone official Gemma 4 templates audited in this rollout.
Minimal history used:
messages = [
{"role": "user", "content": "Weather?"},
{"role": "assistant", "content": "I'll check.", "tool_calls": [{
"id": "call_1", "type": "function", "function": {
"name": "get_weather", "arguments": {"city": "London"}
}
}]},
{"role": "tool", "tool_call_id": "call_1", "content": "sunny"},
]
Related work:
- 26B-A4B fix and family rollout.
- vLLM shared Gemma 4 template fix #66269.
- Existing 12B ordering fix #12, independently verified and reused for that repository. This PR applies the minimal equivalent fix to a different model repository.
AI assistance (OpenAI Codex) was used for implementation and validation.
The open #142 changes thought-channel generation; its template still renders the explanation after the tool result. This patch addresses that input-history ordering and its associated turn closure directly.