|
| 1 | +--- |
| 2 | +title: "Granite 4.2" |
| 3 | +description: "Dense reasoning language model family in 3B, 8B, and 30B sizes with built-in chain-of-thought thinking, flexible thinking modes, and reasoning-augmented tool calling." |
| 4 | +sidebar_custom_props: |
| 5 | + icon: "brain" |
| 6 | +--- |
| 7 | + |
| 8 | +<CardGroup cols={2}> |
| 9 | + <Card |
| 10 | + title="Model Collection" |
| 11 | + icon="arrow-right" |
| 12 | + href="https://huggingface.co/collections/ibm-granite/granite-42-language-models" |
| 13 | + > |
| 14 | + View the full Granite 4.2 collection on Hugging Face |
| 15 | + </Card> |
| 16 | + <Card |
| 17 | + title="GitHub Organization" |
| 18 | + icon="github" |
| 19 | + href="https://github.com/ibm-granite/granite-4.2-language-models" |
| 20 | + > |
| 21 | + Granite models and documentation |
| 22 | + </Card> |
| 23 | +</CardGroup> |
| 24 | + |
| 25 | +## Overview |
| 26 | + |
| 27 | +**Granite 4.2** is a family of dense reasoning language models available in three sizes: 3B, 8B, and 30B parameters. Granite 4.2 introduces native reasoning (thinking) capabilities, allowing models to perform step-by-step chain-of-thought reasoning before producing final answers. This significantly improves performance on complex math, coding, multi-step logic, and agentic tool-calling tasks. |
| 28 | + |
| 29 | +### Model Variants |
| 30 | + |
| 31 | +- **granite-4.2-3b**: Compact reasoning model optimized for edge deployment and resource-constrained environments |
| 32 | +- **granite-4.2-8b**: Balanced reasoning model for general-purpose enterprise applications |
| 33 | +- **granite-4.2-30b**: Flagship reasoning model for complex reasoning and specialized tasks |
| 34 | + |
| 35 | +All models natively support a 128K context window (with long-context extension to 512K on the 30B model) and are released under the Apache 2.0 license with cryptographic signatures, ISO certification, and full transparency disclosures, enabling unrestricted commercial and academic use. |
| 36 | + |
| 37 | +### Key Capabilities |
| 38 | + |
| 39 | +**Built-in Reasoning**: Granite 4.2 features native chain-of-thought reasoning inside `<think>...</think>` tags, significantly improving performance on math, coding, and complex multi-step problems. |
| 40 | + |
| 41 | +**Flexible Thinking Modes**: Seamlessly switch between full thinking (default), non-thinking, and low-effort modes within a single model, allowing users to balance depth vs. latency on a per-query basis. |
| 42 | + |
| 43 | +**Reasoning-Augmented Tool Calling**: The model reasons about which tools to invoke and why before making the call, producing more accurate function calls for agentic workflows. |
| 44 | + |
| 45 | +**Multilingual Dialog**: Granite 4.2 is tested across English, German, Spanish, French, Japanese, Portuguese, Arabic, Czech, Italian, Korean, Dutch, and Chinese. |
| 46 | + |
| 47 | +## Getting Started |
| 48 | + |
| 49 | +First, install the required libraries: |
| 50 | + |
| 51 | +<CodeGroup> |
| 52 | + |
| 53 | +```bash Install |
| 54 | +pip install torch torchvision torchaudio |
| 55 | +pip install accelerate |
| 56 | +pip install transformers |
| 57 | +``` |
| 58 | + |
| 59 | +</CodeGroup> |
| 60 | + |
| 61 | +### Generation |
| 62 | + |
| 63 | +This is a simple example of how to use the Granite-4.2-30B model in thinking mode: |
| 64 | + |
| 65 | +<CodeGroup> |
| 66 | + |
| 67 | +```python Python |
| 68 | +import torch |
| 69 | +from transformers import AutoModelForCausalLM, AutoTokenizer |
| 70 | + |
| 71 | +model_path = "ibm-granite/granite-4.2-30b" |
| 72 | +tokenizer = AutoTokenizer.from_pretrained(model_path) |
| 73 | +# drop device_map if running on CPU |
| 74 | +model = AutoModelForCausalLM.from_pretrained(model_path, device_map="cuda", torch_dtype=torch.bfloat16) |
| 75 | +model.eval() |
| 76 | + |
| 77 | +# change input text as desired |
| 78 | +messages = [ |
| 79 | + { "role": "user", "content": "How many r's are in the word 'strawberry'?" }, |
| 80 | +] |
| 81 | + |
| 82 | +text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=True) |
| 83 | +inputs = tokenizer(text, return_tensors="pt").to(model.device) |
| 84 | + |
| 85 | +# generate output tokens |
| 86 | +output = model.generate(**inputs, max_new_tokens=8192, temperature=1.0, top_p=0.95, do_sample=True) |
| 87 | + |
| 88 | +# decode output tokens into text |
| 89 | +print(tokenizer.decode(output[0][inputs.input_ids.shape[-1]:], skip_special_tokens=False)) |
| 90 | +``` |
| 91 | + |
| 92 | +</CodeGroup> |
| 93 | + |
| 94 | +Expected output: |
| 95 | + |
| 96 | +<CodeGroup> |
| 97 | + |
| 98 | +```text Output |
| 99 | +<think> |
| 100 | +First, I need to write out the word: s t r a w b e r r y. |
| 101 | +Now, I count the number of 'r' letters: one at position 3, one at position 8, one at position 9. |
| 102 | +Total r's = 3. |
| 103 | +</think> |
| 104 | +There are **3** r's in the word "strawberry".<|im_end|> |
| 105 | +``` |
| 106 | + |
| 107 | +</CodeGroup> |
| 108 | + |
| 109 | +> **Generation parameters:** Use `temperature=1.0` and `top_p=0.95` across **all tasks and serving backends**, including general chat, reasoning, and tool calling. |
| 110 | +
|
| 111 | +### Thinking Modes |
| 112 | + |
| 113 | +Granite 4.2 supports three thinking modes, selected via chat-template parameters: |
| 114 | + |
| 115 | +| Mode | Template Parameters | Behavior | |
| 116 | +|:-----|:-------------------|:---------| |
| 117 | +| **Thinking** (default) | `enable_thinking=True` | Full chain-of-thought reasoning inside `<think>...</think>` | |
| 118 | +| **Non-thinking** | `enable_thinking=False` | Direct answer with no reasoning overhead | |
| 119 | +| **Low-effort** | `enable_thinking=True, low_effort=True` | Brief reasoning for simpler queries | |
| 120 | + |
| 121 | +#### Non-Thinking Mode |
| 122 | + |
| 123 | +Disable reasoning for a direct answer with no chain-of-thought overhead: |
| 124 | + |
| 125 | +<CodeGroup> |
| 126 | + |
| 127 | +```python Python |
| 128 | +messages = [ |
| 129 | + {"role": "user", "content": "What is the capital of France?"}, |
| 130 | +] |
| 131 | +text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=False) |
| 132 | +inputs = tokenizer(text, return_tensors="pt").to(model.device) |
| 133 | + |
| 134 | +output = model.generate(**inputs, max_new_tokens=2048, temperature=1.0, top_p=0.95, do_sample=True) |
| 135 | +print(tokenizer.decode(output[0][inputs.input_ids.shape[-1]:], skip_special_tokens=False)) |
| 136 | +``` |
| 137 | + |
| 138 | +</CodeGroup> |
| 139 | + |
| 140 | +Expected output: |
| 141 | + |
| 142 | +<CodeGroup> |
| 143 | + |
| 144 | +```text Output |
| 145 | +<think></think>The capital of France is Paris.<|im_end|> |
| 146 | +``` |
| 147 | + |
| 148 | +</CodeGroup> |
| 149 | + |
| 150 | +### Tool Calling |
| 151 | + |
| 152 | +Granite 4.2 supports tool calling with integrated reasoning — the model thinks about which tool to call and why before making the call. Define a list of tools using OpenAI's function definition schema: |
| 153 | + |
| 154 | +<CodeGroup> |
| 155 | + |
| 156 | +```python Python |
| 157 | +import torch |
| 158 | +from transformers import AutoModelForCausalLM, AutoTokenizer |
| 159 | + |
| 160 | +model_path = "ibm-granite/granite-4.2-30b" |
| 161 | +tokenizer = AutoTokenizer.from_pretrained(model_path) |
| 162 | +# drop device_map if running on CPU |
| 163 | +model = AutoModelForCausalLM.from_pretrained(model_path, device_map="cuda", torch_dtype=torch.bfloat16) |
| 164 | +model.eval() |
| 165 | + |
| 166 | +tools = [ |
| 167 | + { |
| 168 | + "type": "function", |
| 169 | + "function": { |
| 170 | + "name": "get_current_weather", |
| 171 | + "description": "Get the current weather for a specified city.", |
| 172 | + "parameters": { |
| 173 | + "type": "object", |
| 174 | + "properties": { |
| 175 | + "city": { |
| 176 | + "type": "string", |
| 177 | + "description": "Name of the city" |
| 178 | + } |
| 179 | + }, |
| 180 | + "required": ["city"] |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +] |
| 185 | + |
| 186 | +# change input text as desired |
| 187 | +messages = [ |
| 188 | + { "role": "user", "content": "What's the weather like in Boston right now?" }, |
| 189 | +] |
| 190 | + |
| 191 | +text = tokenizer.apply_chat_template(messages, tokenize=False, tools=tools, |
| 192 | + add_generation_prompt=True, enable_thinking=True) |
| 193 | +inputs = tokenizer(text, return_tensors="pt").to(model.device) |
| 194 | + |
| 195 | +# generate output tokens |
| 196 | +output = model.generate(**inputs, max_new_tokens=4096, temperature=1.0, top_p=0.95, do_sample=True) |
| 197 | + |
| 198 | +# decode output tokens into text |
| 199 | +print(tokenizer.decode(output[0][inputs.input_ids.shape[-1]:], skip_special_tokens=False)) |
| 200 | +``` |
| 201 | + |
| 202 | +</CodeGroup> |
| 203 | + |
| 204 | +Expected output: |
| 205 | + |
| 206 | +<CodeGroup> |
| 207 | + |
| 208 | +```text Output |
| 209 | +<think> |
| 210 | +The user is asking for the weather in Boston right now. There's a function called |
| 211 | +get_current_weather that takes a city parameter. I need to call that with the city set to Boston. |
| 212 | +</think> |
| 213 | +<tool_call> |
| 214 | +<function=get_current_weather> |
| 215 | +<parameter=city> |
| 216 | +Boston |
| 217 | +</parameter> |
| 218 | +</function> |
| 219 | +</tool_call> |
| 220 | +<|im_end|> |
| 221 | +``` |
| 222 | + |
| 223 | +</CodeGroup> |
| 224 | + |
| 225 | +### Serving with vLLM |
| 226 | + |
| 227 | +Granite 4.2 is optimized for deployment with [vLLM](https://github.com/vllm-project/vllm) (v0.20+). |
| 228 | + |
| 229 | +> **Reasoning parser:** Use the custom `granite_thinking_parser`, available in each model's Hugging Face repository. The models also work with the built-in `nemotron_v3` parser, but `granite_thinking_parser` provides better formatting of reasoning output. |
| 230 | +> **Tool calling parser:** Use `qwen3_coder`. |
| 231 | +
|
| 232 | +<CodeGroup> |
| 233 | + |
| 234 | +```bash Serve |
| 235 | +vllm serve ibm-granite/granite-4.2-30b \ |
| 236 | + --served-model-name granite-4.2-30b \ |
| 237 | + --dtype bfloat16 \ |
| 238 | + --max-model-len 131072 \ |
| 239 | + --reasoning-parser granite_thinking_parser \ |
| 240 | + --reasoning-parser-plugin ./granite_thinking_parser.py \ |
| 241 | + --tool-call-parser qwen3_coder \ |
| 242 | + --enable-auto-tool-choice |
| 243 | +``` |
| 244 | + |
| 245 | +</CodeGroup> |
| 246 | + |
| 247 | +The model then exposes an OpenAI-compatible API on `http://localhost:8000/v1`, which integrates with popular agentic coding harnesses such as OpenCode, Pi, and OpenHands out of the box. |
| 248 | + |
| 249 | +<CodeGroup> |
| 250 | + |
| 251 | +```python Python |
| 252 | +from openai import OpenAI |
| 253 | + |
| 254 | +client = OpenAI(base_url="http://localhost:8000/v1", api_key="unused") |
| 255 | + |
| 256 | +response = client.chat.completions.create( |
| 257 | + model="granite-4.2-30b", |
| 258 | + messages=[{"role": "user", "content": "Explain the Riemann hypothesis in simple terms."}], |
| 259 | + temperature=1.0, |
| 260 | + top_p=0.95, |
| 261 | + max_tokens=8192, |
| 262 | +) |
| 263 | + |
| 264 | +print(response.choices[0].message.content) |
| 265 | +``` |
| 266 | + |
| 267 | +</CodeGroup> |
0 commit comments