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
- 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
Update chat_template.jinja
Browse filesThis updates the Gemma chat_template.jinja tool declaration rendering so nullable referenced JSON Schema fields (in common constructs likely to matter for tool declarations) are not collapsed into empty type declarations.
- chat_template.jinja +50 -11
chat_template.jinja
CHANGED
|
@@ -11,12 +11,39 @@
|
|
| 11 |
description:<|"|>{{ value['description'] }}<|"|>
|
| 12 |
{%- set add_comma = true -%}
|
| 13 |
{%- endif -%}
|
| 14 |
-
{%-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
{%- endif -%}
|
| 19 |
-
{%-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
{%- if value['items'] is mapping and value['items'] -%}
|
| 21 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 22 |
items:{
|
|
@@ -52,11 +79,11 @@
|
|
| 52 |
}
|
| 53 |
{%- endif -%}
|
| 54 |
{%- endif -%}
|
| 55 |
-
{%- if value['nullable'] %}
|
| 56 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 57 |
nullable:true
|
| 58 |
{%- endif -%}
|
| 59 |
-
{%- if
|
| 60 |
{%- if value['properties'] is defined and value['properties'] is mapping -%}
|
| 61 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 62 |
properties:{
|
|
@@ -68,7 +95,7 @@
|
|
| 68 |
{{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
|
| 69 |
}
|
| 70 |
{%- endif -%}
|
| 71 |
-
{%- if value['required'] -%}
|
| 72 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 73 |
required:[
|
| 74 |
{%- for item in value['required'] | default([]) -%}
|
|
@@ -78,8 +105,15 @@
|
|
| 78 |
]
|
| 79 |
{%- endif -%}
|
| 80 |
{%- endif -%}
|
| 81 |
-
{%- if
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
{%- endif -%}
|
| 84 |
{%- endfor -%}
|
| 85 |
{%- endmacro -%}
|
|
@@ -91,6 +125,9 @@
|
|
| 91 |
{%- if params['properties'] -%}
|
| 92 |
properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
|
| 93 |
{%- endif -%}
|
|
|
|
|
|
|
|
|
|
| 94 |
{%- if params['required'] -%}
|
| 95 |
required:[
|
| 96 |
{%- for item in params['required'] -%}
|
|
@@ -120,6 +157,8 @@
|
|
| 120 |
{{- '<|"|>' + argument + '<|"|>' -}}
|
| 121 |
{%- elif argument is boolean -%}
|
| 122 |
{{- 'true' if argument else 'false' -}}
|
|
|
|
|
|
|
| 123 |
{%- elif argument is mapping -%}
|
| 124 |
{{- '{' -}}
|
| 125 |
{%- set ns = namespace(found_first=false) -%}
|
|
@@ -351,4 +390,4 @@
|
|
| 351 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 352 |
{%- endif -%}
|
| 353 |
{%- endif -%}
|
| 354 |
-
{%- endif -%}
|
|
|
|
| 11 |
description:<|"|>{{ value['description'] }}<|"|>
|
| 12 |
{%- set add_comma = true -%}
|
| 13 |
{%- endif -%}
|
| 14 |
+
{%- set type_names = namespace(names=[]) -%}
|
| 15 |
+
{%- if value['type'] is defined and value['type'] -%}
|
| 16 |
+
{%- if value['type'] is string -%}
|
| 17 |
+
{%- set type_names.names = [value['type'] | upper] -%}
|
| 18 |
+
{%- else -%}
|
| 19 |
+
{%- set type_names.names = value['type'] | map('upper') | list -%}
|
| 20 |
{%- endif -%}
|
| 21 |
+
{%- endif -%}
|
| 22 |
+
{%- if value['anyOf'] is defined and value['anyOf'] -%}
|
| 23 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 24 |
+
anyOf:{{ format_argument(value['anyOf'], escape_keys=false) }}
|
| 25 |
+
{%- endif -%}
|
| 26 |
+
{%- if value['oneOf'] is defined and value['oneOf'] -%}
|
| 27 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 28 |
+
oneOf:{{ format_argument(value['oneOf'], escape_keys=false) }}
|
| 29 |
+
{%- endif -%}
|
| 30 |
+
{%- if value['allOf'] is defined and value['allOf'] -%}
|
| 31 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 32 |
+
allOf:{{ format_argument(value['allOf'], escape_keys=false) }}
|
| 33 |
+
{%- endif -%}
|
| 34 |
+
{%- if value['$ref'] is defined and value['$ref'] -%}
|
| 35 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 36 |
+
$ref:<|"|>{{ value['$ref'] }}<|"|>
|
| 37 |
+
{%- endif -%}
|
| 38 |
+
{%- if value['enum'] is defined and value['enum'] -%}
|
| 39 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 40 |
+
enum:{{ format_argument(value['enum']) }}
|
| 41 |
+
{%- endif -%}
|
| 42 |
+
{%- if value['const'] is defined -%}
|
| 43 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 44 |
+
const:{{ format_argument(value['const'], escape_keys=false) }}
|
| 45 |
+
{%- endif -%}
|
| 46 |
+
{%- if 'ARRAY' in type_names.names -%}
|
| 47 |
{%- if value['items'] is mapping and value['items'] -%}
|
| 48 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 49 |
items:{
|
|
|
|
| 79 |
}
|
| 80 |
{%- endif -%}
|
| 81 |
{%- endif -%}
|
| 82 |
+
{%- if value['nullable'] is defined and value['nullable'] %}
|
| 83 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 84 |
nullable:true
|
| 85 |
{%- endif -%}
|
| 86 |
+
{%- if 'OBJECT' in type_names.names -%}
|
| 87 |
{%- if value['properties'] is defined and value['properties'] is mapping -%}
|
| 88 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 89 |
properties:{
|
|
|
|
| 95 |
{{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
|
| 96 |
}
|
| 97 |
{%- endif -%}
|
| 98 |
+
{%- if value['required'] is defined and value['required'] -%}
|
| 99 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 100 |
required:[
|
| 101 |
{%- for item in value['required'] | default([]) -%}
|
|
|
|
| 105 |
]
|
| 106 |
{%- endif -%}
|
| 107 |
{%- endif -%}
|
| 108 |
+
{%- if value['type'] is defined and value['type'] -%}
|
| 109 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 110 |
+
{%- if value['type'] is string -%}
|
| 111 |
+
type:<|"|>{{ value['type'] | upper }}<|"|>
|
| 112 |
+
{%- else -%}
|
| 113 |
+
type:{{ format_argument(value['type'] | map('upper') | list) }}
|
| 114 |
+
{%- endif -%}
|
| 115 |
+
{%- endif -%}
|
| 116 |
+
}
|
| 117 |
{%- endif -%}
|
| 118 |
{%- endfor -%}
|
| 119 |
{%- endmacro -%}
|
|
|
|
| 125 |
{%- if params['properties'] -%}
|
| 126 |
properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
|
| 127 |
{%- endif -%}
|
| 128 |
+
{%- if params['$defs'] is defined and params['$defs'] -%}
|
| 129 |
+
$defs:{{ format_argument(params['$defs'], escape_keys=false) }},
|
| 130 |
+
{%- endif -%}
|
| 131 |
{%- if params['required'] -%}
|
| 132 |
required:[
|
| 133 |
{%- for item in params['required'] -%}
|
|
|
|
| 157 |
{{- '<|"|>' + argument + '<|"|>' -}}
|
| 158 |
{%- elif argument is boolean -%}
|
| 159 |
{{- 'true' if argument else 'false' -}}
|
| 160 |
+
{%- elif argument is none -%}
|
| 161 |
+
{{- 'null' -}}
|
| 162 |
{%- elif argument is mapping -%}
|
| 163 |
{{- '{' -}}
|
| 164 |
{%- set ns = namespace(found_first=false) -%}
|
|
|
|
| 390 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 391 |
{%- endif -%}
|
| 392 |
{%- endif -%}
|
| 393 |
+
{%- endif -%}
|