_normalize_messages() — langchain Function Reference
Architecture documentation for the _normalize_messages() function in _utils.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 030ac02c_2931_4d20_1689_430fac9b6609["_normalize_messages()"] 52504dd3_f4d4_21a4_b28c_0c152227d20d["_utils.py"] 030ac02c_2931_4d20_1689_430fac9b6609 -->|defined in| 52504dd3_f4d4_21a4_b28c_0c152227d20d 8b851a24_8640_e308_679c_b4265f6177ef["is_openai_data_block()"] 030ac02c_2931_4d20_1689_430fac9b6609 -->|calls| 8b851a24_8640_e308_679c_b4265f6177ef efa972f5_646a_e032_9352_31040573999c["_ensure_message_copy()"] 030ac02c_2931_4d20_1689_430fac9b6609 -->|calls| efa972f5_646a_e032_9352_31040573999c 4cfce0d8_9566_3400_9721_df2216aa19de["_update_content_block()"] 030ac02c_2931_4d20_1689_430fac9b6609 -->|calls| 4cfce0d8_9566_3400_9721_df2216aa19de style 030ac02c_2931_4d20_1689_430fac9b6609 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/language_models/_utils.py lines 124–289
def _normalize_messages(
messages: Sequence["BaseMessage"],
) -> list["BaseMessage"]:
"""Normalize message formats to LangChain v1 standard content blocks.
Chat models already implement support for:
- Images in OpenAI Chat Completions format
These will be passed through unchanged
- LangChain v1 standard content blocks
This function extends support to:
- `[Audio](https://platform.openai.com/docs/api-reference/chat/create) and
`[file](https://platform.openai.com/docs/api-reference/files) data in OpenAI
Chat Completions format
- Images are technically supported but we expect chat models to handle them
directly; this may change in the future
- LangChain v0 standard content blocks for backward compatibility
!!! warning "Behavior changed in `langchain-core` 1.0.0"
In previous versions, this function returned messages in LangChain v0 format.
Now, it returns messages in LangChain v1 format, which upgraded chat models now
expect to receive when passing back in message history. For backward
compatibility, this function will convert v0 message content to v1 format.
??? note "v0 Content Block Schemas"
`URLContentBlock`:
```python
{
mime_type: NotRequired[str]
type: Literal['image', 'audio', 'file'],
source_type: Literal['url'],
url: str,
}
```
`Base64ContentBlock`:
```python
{
mime_type: NotRequired[str]
type: Literal['image', 'audio', 'file'],
source_type: Literal['base64'],
data: str,
}
```
`IDContentBlock`:
(In practice, this was never used)
```python
{
type: Literal["image", "audio", "file"],
source_type: Literal["id"],
id: str,
}
```
`PlainTextContentBlock`:
```python
{
mime_type: NotRequired[str]
type: Literal['file'],
source_type: Literal['text'],
url: str,
}
```
If a v1 message is passed in, it will be returned as-is, meaning it is safe to
always pass in v1 messages to this function for assurance.
For posterity, here are the OpenAI Chat Completions schemas we expect:
Chat Completions image. Can be URL-based or base64-encoded. Supports MIME types
png, jpeg/jpg, webp, static gif:
{
"type": Literal['image_url'],
Domain
Subdomains
Source
Frequently Asked Questions
What does _normalize_messages() do?
_normalize_messages() is a function in the langchain codebase, defined in libs/core/langchain_core/language_models/_utils.py.
Where is _normalize_messages() defined?
_normalize_messages() is defined in libs/core/langchain_core/language_models/_utils.py at line 124.
What does _normalize_messages() call?
_normalize_messages() calls 3 function(s): _ensure_message_copy, _update_content_block, is_openai_data_block.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free