Skip to content

Commit 6d00f6b

Browse files
authored
[docs] Chat content patterns (#42748)
* chat content patterns * review * feedback
1 parent 6217adc commit 6d00f6b

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
title: Chat basics
9797
- local: chat_templating
9898
title: Chat templates
99+
- local: chat_content_patterns
100+
title: Chat message patterns
99101
- local: chat_templating_multimodal
100102
title: Multimodal chat templates
101103
- local: chat_extras
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<!--Copyright 2025 The HuggingFace Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
12+
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
13+
rendered properly in your Markdown viewer.
14+
15+
-->
16+
17+
# Chat message patterns
18+
19+
Chat models expect conversations as a list of dictionaries. Each dictionary uses `role` and `content` keys. The `content` key holds the user message passed to the model. Large language models accept text and tools and multimodal models combine text with images, videos, and audio.
20+
21+
Transformers uses a unified format where each modality type is specified explicitly, making it straightforward to mix and match inputs in a single message.
22+
23+
This guide covers message formatting patterns for each modality, tools, batch inference, and multi-turn conversations.
24+
25+
## Text
26+
27+
Text is the most basic content type. It's the foundation for all other patterns. Pass your message to `"content"` as a string.
28+
29+
```py
30+
message = [
31+
{
32+
"role": "user",
33+
"content": "Explain the French Bread Law."
34+
}
35+
]
36+
```
37+
38+
You could also use the explicit `"type": "text"` format to keep your code consistent when you add images, videos, or audio later.
39+
40+
```py
41+
message = [
42+
{
43+
"role": "user",
44+
"content": [{"type": "text", "text": "Explain the French Bread Law."}]
45+
}
46+
]
47+
```
48+
49+
## Tools
50+
51+
[Tools](./chat_extras) are functions a chat model can call, like getting real-time weather data, instead of generating it on its own.
52+
53+
The `assistant` role handles the tool request. Set `"type": "function"` in the `"tool_calls"` key and provide your tool to the `"function"` key. Append the assistant's tool request to your message.
54+
55+
```py
56+
weather = {"name": "get_current_temperature", "arguments": {"location": "Paris, France", "unit": "celsius"}}
57+
message.append(
58+
{
59+
"role": "assistant",
60+
"tool_calls": [{"type": "function", "function": weather}]
61+
}
62+
)
63+
```
64+
65+
The `tool` role handles the result. Append it in `"content"`. This value should always be a string.
66+
67+
```py
68+
message.append({"role": "tool", "content": "22"})
69+
```
70+
71+
## Multimodal
72+
73+
Multimodal models extend this format to handle images, videos, and audio. Each input specifies its `"type"` and provides the media with `"url"` or `"path"`.
74+
75+
### Image
76+
77+
Set `"type": "image"` and use `"url"` for links or `"path"` for local files.
78+
79+
```py
80+
message = [
81+
{
82+
"role": "user",
83+
"content": [
84+
{"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
85+
{"type": "text", "text": "What pastry is shown in the image?"}
86+
]
87+
}
88+
]
89+
```
90+
91+
### Video
92+
93+
Set `"type": "video"` and use `"url"` for links or `"path"` for local files.
94+
95+
```py
96+
message = [
97+
{
98+
"role": "user",
99+
"content": [
100+
{"type": "video", "url": "https://static01.nyt.com/images/2019/10/01/dining/01Sourdough-GIF-1/01Sourdough-GIF-1-superJumbo.gif"},
101+
{"type": "text", "text": "What is shown in this video?"}
102+
]
103+
}
104+
]
105+
```
106+
107+
### Audio
108+
109+
Set `"type": "audio"` and use `"url"` for links or `"path"` for local files.
110+
111+
```py
112+
message = [
113+
{
114+
"role": "user",
115+
"content": [
116+
{"type": "audio", "url": "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"},
117+
{"type": "text", "text": "Transcribe the speech."}
118+
]
119+
}
120+
]
121+
```
122+
123+
### Mixed multiple
124+
125+
The `content` list accepts any combination of types. The model processes all inputs together, enabling comparisons or cross-modal reasoning.
126+
127+
```py
128+
message = [
129+
{
130+
"role": "user",
131+
"content": [
132+
{"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
133+
{"type": "video", "url": "https://static01.nyt.com/images/2019/10/01/dining/01Sourdough-GIF-1/01Sourdough-GIF-1-superJumbo.gif"},
134+
{"type": "text", "text": "What does the image and video share in common?"},
135+
],
136+
},
137+
{
138+
"role": "user",
139+
"content": [
140+
{"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
141+
{"type": "image", "url": "https://assets.bonappetit.com/photos/57e191f49f19b4610e6b7693/master/w_1600%2Cc_limit/undefined"},
142+
{"type": "text", "text": "What type of pastries are these?"},
143+
],
144+
}
145+
]
146+
```
147+
148+
## Batched
149+
150+
Batched inference processes multiple conversations in a single forward pass to improve throughput and efficiency. Wrap each conversation in its own list, then pass them together as a list of lists.
151+
152+
```py
153+
messages = [
154+
[
155+
{"role": "user",
156+
"content": [
157+
{"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
158+
{"type": "text", "text": "What type of pastry is this?"}
159+
]
160+
},
161+
],
162+
[
163+
{"role": "user",
164+
"content": [
165+
{"type": "image", "url": "https://assets.bonappetit.com/photos/57e191f49f19b4610e6b7693/master/w_1600%2Cc_limit/undefined"},
166+
{"type": "text", "text": "What type of pastry is this?"}
167+
]
168+
},
169+
],
170+
]
171+
```
172+
173+
## Multi-turn
174+
175+
Conversations span multiple exchanges, alternating between `"user"` and `"assistant"` roles. Each turn adds a new message to the list, giving the model access to the full conversation history. This context helps the model generate more appropriate responses.
176+
177+
```py
178+
message = [
179+
{
180+
"role": "user",
181+
"content": [
182+
{"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
183+
{"type": "text", "text": "What pastry is shown in the image?"}
184+
]
185+
},
186+
{
187+
"role": "assistant",
188+
"content": [{"type": "text", "text": "This is kouign amann, a laminated dough pastry (i.e., dough folded with layers of butter) that also incorporates sugar between layers so that during baking the sugar caramelizes."}]
189+
},
190+
{
191+
"role": "user",
192+
"content": [
193+
{"type": "image", "url": "https://static01.nyt.com/images/2023/07/21/multimedia/21baguettesrex-hbkc/21baguettesrex-hbkc-videoSixteenByNineJumbo1600.jpg"},
194+
{"type": "text", "text": "Compare it to this image now."}
195+
]
196+
}
197+
]
198+
```

0 commit comments

Comments
 (0)