Skip to main content
πŸ€– Learn AI: Build with LLMs

The Chat Messages Format

Modern LLM APIs (Anthropic, OpenAI) take a list of messages. Each message has a role ("user" or "assistant") and content. The model continues the conversation.

[
  { role: "user", content: "Hi!" },
  { role: "assistant", content: "Hello! How can I help?" }
]

Your task

Write toMessages(turns) that takes an array of plain strings and returns a messages array. The first string is from the "user", and roles then alternate user β†’ assistant β†’ user β†’ ...

⚑ 30 XP
solution.jsRuns in your browser
Loading editor…
Tests2 to pass
alternates roles
empty input β†’ empty array

Run your code to check these.