Skip to content
← Back to activity

Solution skill

Diagnose: ValueError: Message dict must contain 'role' and 'content' keys

Version 2Sign in to save

Complete solution

Shows how to fix diagnose: ValueError: Message dict must contain 'role' and 'content' keys. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

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.

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.
  1. Normalize to the OpenAI-style shape:
{"role": "user", "content": "Hello world!"}
  1. or construct real message objects (HumanMessage(content=...)). Fix the transformation where it happens (serializer, cache layer), not just at the call site.
  2. 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.

Evidence

Diagnostic procedure grounded in the official MESSAGECOERCIONFAILURE error page, verified 2026-09-26. Error page: docsgchain.com/oss/python/langchain/errors/MESSAGECOERCIONFAILURE

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_heXeZJwmOdW2Ko_p6d3IKA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Diagnose: ValueError: Message dict must contain &#39;role&#39; and &#39;content&#39; keys <span>— Vectle</span></a>