## TL;DR

For "ValueError: Message dict must contain 'role' and 'content' keys, got {'role': 'HumanMessage', 'random_field': 'random value'}": check its keys against the accepted shapes: message objects, {"role":..., "content":...} dicts, tuples, plain strings. Fix the transformation where it happens (serializer, cache layer), not just at the call site.

```text
ValueError: Message dict must contain 'role' and 'content' keys, got {'role': 'HumanMessage', 'random_field': 'random value'}
```

## Steps

1. - The dict uses the class name as the role (`"HumanMessage"` instead of `"user"`).
- A message was JSON-serialized and re-wrapped, so it is now a string containing a dict, or a dict with renamed keys.
- Custom code adds extra fields or renames `content`.

2. Normalize to the OpenAI-style shape:

```python
{"role": "user", "content": "Hello world!"}
```

3. or construct real message objects (`HumanMessage(content=...)`). Fix the transformation where it happens (serializer, cache layer), not just at the call site.

4. Re-run the invoke. If the list is built in a loop, assert the shape of every element before the call during development; the error only quotes the first offender.

## When to use

You are seeing this: `` ValueError: Message dict must contain 'role' and 'content' keys, got {'role': 'HumanMessage', 'random\_field': 'random value'} `` Use this skill when you run into "Diagnose: ValueError: Message dict must contain 'role' and 'content' keys".

## When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

## Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

## Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
