MCPcopy
hub / github.com/mudler/LocalAI / messages_to_dicts

Function messages_to_dicts

backend/python/common/python_utils.py:40–65  ·  view source on GitHub ↗

Convert proto ``Message`` objects to dicts suitable for ``apply_chat_template``. Handles: ``role``, ``content``, ``name``, ``tool_call_id``, ``reasoning_content``, ``tool_calls`` (JSON string → Python list). HuggingFace chat templates (and their MLX/vLLM wrappers) expect a list of

(proto_messages)

Source from the content-addressed store, hash-verified

38
39
40def messages_to_dicts(proto_messages):
41 """Convert proto ``Message`` objects to dicts suitable for ``apply_chat_template``.
42
43 Handles: ``role``, ``content``, ``name``, ``tool_call_id``,
44 ``reasoning_content``, ``tool_calls`` (JSON string → Python list).
45
46 HuggingFace chat templates (and their MLX/vLLM wrappers) expect a list of
47 plain dicts — proto Message objects don't work directly with Jinja, so
48 this conversion is needed before every ``apply_chat_template`` call.
49 """
50 result = []
51 for msg in proto_messages:
52 d = {"role": msg.role, "content": msg.content or ""}
53 if msg.name:
54 d["name"] = msg.name
55 if msg.tool_call_id:
56 d["tool_call_id"] = msg.tool_call_id
57 if msg.reasoning_content:
58 d["reasoning_content"] = msg.reasoning_content
59 if msg.tool_calls:
60 try:
61 d["tool_calls"] = json.loads(msg.tool_calls)
62 except json.JSONDecodeError:
63 pass
64 result.append(d)
65 return result

Callers 7

_prepare_promptMethod · 0.90
_prepare_promptMethod · 0.90
_prepare_promptMethod · 0.90
_predictMethod · 0.85

Calls 1

appendMethod · 0.80