Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Bad Request: can't parse entities — Telegram bot error and how to fix it

Bad Request: can't parse entities means Telegram could not read the formatting in your message: with parse_mode set to HTML, Markdown or MarkdownV2, some text — usually a user's name, a username with an underscore, a price with a dot, or an unclosed tag — breaks the markup. Escape that text for the parse mode you use, send entities instead of parse_mode, or send it without parse_mode.

By Livops team, the people who build and run LivopsUpdated

The error, as the Telegram Bot API returns it with error code 400, comes in a few shapes[2][3][4]:

Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 14
Bad Request: can't parse entities: Unsupported start tag "3" at byte offset 2
Bad Request: can't parse entities: Character '.' is reserved and must be escaped with the preceding '\'

In short: the message was sent with parse_mode (HTML, Markdown or MarkdownV2), and its text contains markup Telegram cannot read, so nothing was sent. The text that breaks it is usually inserted from outside: a name or username with _ or *, a < in HTML, a . or - that MarkdownV2 wants escaped, or a tag left open. Escape every value you insert for the parse mode you use — in HTML only <, > and & — or send entities instead of parse_mode, or send the text without parse_mode.

Each library wraps it in its own words:

Library What you see
aiogram 3 TelegramBadRequest: Telegram server says - Bad Request: can't parse entities: ...[5]
python-telegram-bot telegram.error.BadRequest: Can't parse entities: can't find end of the entity starting at byte offset 14[6]
grammY GrammyError: Call to 'sendMessage' failed! (400: Bad Request: can't parse entities: ...)[7]
Telegraf TelegramError: 400: Bad Request: can't parse entities: ...[8]

python-telegram-bot drops the Bad Request: prefix and capitalizes the rest, which also lowercases everything after the first letter[6]. This page explains what each variant means, how to find the byte it points at, the usual causes, and three fixes — escaping, entities, or no parse mode — in aiogram, python-telegram-bot, grammY and Telegraf.

What does the error mean?

A message’s text can carry formatting in two ways: as markup in the text, read according to parse_mode (HTML, MarkdownV2 or the legacy Markdown), or as a list of entities sent next to plain text, which can be specified instead of parse_mode[1]. With parse_mode set, the Bot API server parses the text first; when parsing fails, the request fails with this error and nothing is sent[4][3].

The part after can't parse entities: tells you which parser failed and why[2]:

Message Parse mode What broke
Can't find end of the entity starting at byte offset N Markdown A _, *, ` or [ opened an entity that never closes
Can't find end of ... entity at byte offset N MarkdownV2 The same, for MarkdownV2
Character 'X' is reserved and must be escaped with the preceding '\' MarkdownV2 A reserved character in plain text, not escaped
Unsupported start tag "x" at byte offset N HTML A < followed by something that is not a supported tag
Unclosed start tag at byte offset N HTML A < with no > after it
Can't find end tag corresponding to start tag "b" HTML An opened tag that is never closed

Byte offset, not character

N is a position in the text you sent, counted in bytes of its UTF-8 encoding from zero[2]. A Latin letter is one byte, a Cyrillic letter two, most emoji four. In New user: john_doe the underscore is at byte 14 and character 14; in Привет, john_doe it is at byte 18 but character 12. To see what is at the offset, slice the bytes:

offset = 18  # from the error
print(text.encode("utf-8")[offset:offset + 30].decode("utf-8", "replace"))
const offset = 18; // from the error
console.log(Buffer.from(text, "utf8").subarray(offset, offset + 30).toString());

Entities use a third unit: their offset and length are counted in UTF-16 code units[1]. That only matters if you build the entities list by hand; the library helpers below do it for you.

Causes, most common first

1. User text inside a formatted message

await message.answer(f"Welcome, <b>{user.full_name}</b>!", parse_mode="HTML")

Fine until a user’s name contains <, or with Markdown, until a username has an _. In HTML, every <, > and & that is not part of a tag or an entity must be replaced with &lt;, &gt; and &amp;[1]. A name like I <3 you fails with Unsupported start tag "3"[2]. Anything that comes from outside — names, usernames, captions, search results, error messages, API responses — has to be escaped before it goes into markup.

2. A parse mode set for every message

aiogram’s DefaultBotProperties(parse_mode=...) sets the default parse mode for all messages[14], and the other libraries have similar defaults. Then a plain message.answer(text) that echoes user text is parsed as markup too, without anything in that line saying so.

3. Legacy Markdown and MarkdownV2 are different

  • Markdown (legacy, kept for backward compatibility) has *bold*, _italic_, links and code only. Outside an entity you escape just _, *, ` and [ with \, entities cannot be nested, and escaping inside an entity is not allowed[1].
  • MarkdownV2 adds underline, strikethrough, spoilers and quotes, and outside entities requires \ before each of _ * [ ] ( ) ~ ` > # + - = | { } . ![1]. That is why Price: 10.5 or Done! fails with Character '.' is reserved.

Escaping for one and sending as the other fails: text escaped for legacy Markdown still has bare dots and dashes that MarkdownV2 rejects.

4. HTML that Telegram does not support

Only the tags in the API’s list work: b, strong, i, em, u, ins, s, strike, del, span class="tg-spoiler", tg-spoiler, a, code, pre, blockquote and a few Telegram-specific ones[1]. <br>, <p>, <div> or <ul> give Unsupported start tag. Use a newline instead of <br>. Of named HTML entities, only &lt;, &gt;, &amp; and &quot; are supported; numerical entities all work[1].

5. A message cut in the middle

A message’s text may be at most 4096 characters after entities parsing[1]. Cutting a long formatted string at a fixed length can cut a closing tag or * off, and the remaining half no longer parses. Split on line boundaries, or split the plain text and format each part.

How to fix it

Option 1: escape the text for the parse mode (prefer HTML)

HTML needs only three characters escaped[1], and every language already has a function for it. That makes it the safer mode for user text.

Python, any library: html.escape converts &, < and >, and by default " and ' too[9]:

import html

name = html.escape(user.full_name)
await bot.send_message(chat_id, f"Welcome, <b>{name}</b>!", parse_mode="HTML")

aiogram 3: html.quote() does the same, as the formatting docs show with html.quote(message.from_user.full_name)[10]; it calls html.escape without quoting " and '[11]. For MarkdownV2, md.quote() puts \ before every MarkdownV2 reserved character and before \ itself[11]. Both come from from aiogram import html, md[12].

python-telegram-bot: telegram.helpers.escape_markdown(text, version=2) for MarkdownV2. The default is version=1, legacy Markdown[15], so pass version=2 explicitly:

from telegram.helpers import escape_markdown

text = f"*Order* for {escape_markdown(user.full_name, version=2)}"
await bot.send_message(chat_id, text, parse_mode="MarkdownV2")

grammY and Telegraf (Node): neither ships an HTML escape function that we could find in its documentation; a short one is enough:

const escapeHtml = (s) =>
  s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");

await ctx.reply(`Welcome, <b>${escapeHtml(ctx.from.first_name)}</b>!`, {
  parse_mode: "HTML",
});

Escape only the inserted values, never the whole message: escaping your own <b> tags turns them into visible text.

Option 2: send entities instead of parse_mode

Send the text exactly as it is, with an entities list that marks which ranges are bold, links and so on[1]. Nothing in the text is parsed, so nothing needs escaping. Do not send both: when parse_mode is set, the Bot API server parses the text and does not use the entities you passed[4].

aiogram 3 builds the list from Text, Bold and the other elements, without tag parity or escaping user input[10]. as_kwargs() returns text and entities and sets parse_mode to None, so a default parse mode does not apply[10][13]:

from aiogram.utils.formatting import Bold, Text

content = Text("Welcome, ", Bold(message.from_user.full_name), "!")
await message.answer(**content.as_kwargs())

grammY: the parse-mode plugin’s fmt template tag tracks offsets and lengths for you, and values you put inside it are plain text[16]:

import { b, fmt } from "@grammyjs/parse-mode";

const msg = fmt`Welcome, ${b}${ctx.from.first_name}${b}!`;
await ctx.reply(msg.text, { entities: msg.entities });

Telegraf: fmt and bold from telegraf/format[17]. sendMessage accepts the result directly[19], and a formatted string with entities leaves parse_mode unset[18]:

const { fmt, bold } = require("telegraf/format");

await ctx.reply(fmt`Welcome, ${bold(ctx.from.first_name)}!`);

Option 3: drop parse_mode

parse_mode is optional[1]. A message that needs no formatting — an echo, a log line, an error report — can go out as plain text, and then no character in it means anything. If you set a default parse mode, send these messages with entities (Option 2) or turn the default off for them.

How to confirm the fix

Send yourself a message built from the hardest text you can think of, through the same code path users hit:

john_doe *star* [link] (x) 10.5 - done! a < b & c > d <3 "quotes" Привет 👋

It should arrive exactly as typed, with your own formatting around it.

On Livops

This error happens while the bot runs, the first time a message with the bad text is sent; a deploy does not send your messages, so it can be green while one command fails.

  • Logs. The error line, with its byte offset, is on the project’s Logs page; livops logs <service> -f follows it from the terminal. Log the failing text next to the error so you can slice the bytes, and remember it may be a user’s data.
  • If it crashes the bot. If the error stops the process, the bot is started again, and the project page counts the restarts. If it keeps falling over, it is stopped, and you are told with the last line it printed — in the cabinet, and in Telegram if your account is linked.
  • Ship the fix with a push. On every push the old bot is stopped before the new one starts, so two copies never answer at once.

A bot costs 19 credits a month (19,000 so’m); see Pricing.

Questions

What does can't parse entities mean in Telegram?

The message was sent with parse_mode set to HTML, Markdown or MarkdownV2, and Telegram could not read the markup in its text. Nothing was sent. The rest of the message says what broke: an entity that never ends, a tag Telegram does not support, or a character MarkdownV2 wants escaped.

What is the byte offset in the error?

The position where the broken markup starts, counted in bytes of the UTF-8 text from zero, not in characters. Latin letters are one byte, Cyrillic letters two, most emoji four, so in non-English text the byte offset is larger than the character position. Encode the text as UTF-8 and look at that byte.

Should I use HTML or MarkdownV2 for a Telegram bot?

HTML when the message contains text you did not write. In HTML only three characters need escaping, less than, greater than and ampersand, and they are easy to escape in any language. MarkdownV2 reserves eighteen characters outside entities, including the dot, the dash and brackets, so ordinary text breaks it often.

Why does my username with an underscore break the message?

In Markdown and MarkdownV2 an underscore starts italic text. A name like john_doe opens an italic entity that never closes, so Telegram cannot find its end. Escape the underscore, or send the message as HTML or with entities.

How do I escape text for MarkdownV2 in python-telegram-bot?

Use telegram.helpers.escape_markdown(text, version=2). The version matters: it defaults to 1, which escapes only the legacy Markdown characters and leaves dots, dashes and brackets that MarkdownV2 rejects.

Can I send formatting without parse_mode?

Yes. Send the text as it is with an entities list that says which ranges are bold, italic or links. Nothing in the text is parsed, so nothing needs escaping. aiogram's formatting utilities, grammY's parse-mode plugin and Telegraf's format module build the list for you.

Why does it work in English but fail for some users?

Because the text that breaks it comes from them: a name with an underscore, an asterisk or a less-than sign, a message with a dot in MarkdownV2. Test the bot with such text before you ship, and escape every value you insert into a formatted message.

Sources

  1. Telegram Bot API — Formatting options, sendMessage, MessageEntity (core.telegram.org). Checked .
  2. TDLib source — td/telegram/MessageEntity.cpp (raw.githubusercontent.com). Checked .
  3. TDLib source — td/telegram/SynchronousRequests.cpp (raw.githubusercontent.com). Checked .
  4. Telegram Bot API server source — telegram-bot-api/Client.cpp (raw.githubusercontent.com). Checked .
  5. aiogram source — aiogram/exceptions.py (raw.githubusercontent.com). Checked .
  6. python-telegram-bot source — telegram/error.py (raw.githubusercontent.com). Checked .
  7. grammY source — src/core/error.ts (raw.githubusercontent.com). Checked .
  8. Telegraf source — src/core/network/error.ts (raw.githubusercontent.com). Checked .
  9. Python — html, HyperText Markup Language support (docs.python.org). Checked .
  10. aiogram — Formatting (docs.aiogram.dev). Checked .
  11. aiogram source — aiogram/utils/text_decorations.py (raw.githubusercontent.com). Checked .
  12. aiogram source — aiogram/__init__.py (raw.githubusercontent.com). Checked .
  13. aiogram source — aiogram/utils/formatting.py (raw.githubusercontent.com). Checked .
  14. aiogram source — aiogram/client/default.py (raw.githubusercontent.com). Checked .
  15. python-telegram-bot — telegram.helpers (docs.python-telegram-bot.org). Checked .
  16. grammY — Parse Mode plugin (parse-mode) (grammy.dev). Checked .
  17. Telegraf source — src/format.ts (raw.githubusercontent.com). Checked .
  18. Telegraf source — src/core/helpers/formatting.ts (raw.githubusercontent.com). Checked .
  19. Telegraf source — src/telegram.ts (raw.githubusercontent.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your bot

Got a question? — @livops_uz