Transform LLM Markdown & LaTeX
into Native Telegram Rich HTML
A lightweight Python engine that converts standard AI model output (OpenAI, Anthropic, DeepSeek, Gemini) into native Telegram Rich Messages (sendRichMessage) with zero external dependencies.
Engine Highlights
Built for production Telegram bots with strict typing and high concurrency.
Streaming Mode Safe
Auto-balances unclosed code blocks, thinking tags, and LaTeX formulas during token-by-token streaming, eliminating Telegram 400 Bad Request: can't parse entities errors.
Robust Native Tables
Transforms Markdown pipe tables into native <table bordered striped> with proper column alignment. Safely handles math pipes like $|\psi
angle$ without breaking columns.
Native LaTeX Math
Converts display equations $$...$$ into <tg-math-block> and inline math $x$ into <tg-math> for crisp native rendering in Telegram clients.
AI Thinking Blocks
Wraps reasoning blocks (<think>...</think>) from DeepSeek-R1, OpenAI o1/o3, and Qwen into expandable <details><summary> accordions.
Smart Message Splitter
Splits long messages up to 32,768 characters while safely auto-closing and re-opening nested tags without fragmenting formulas or code snippets.
Zero Dependencies
Implemented purely with Python standard libraries (re, html, argparse). Async-safe, reentrant, and ready for high-load bot clusters.
Quick Start (Python)
Ready to integrate into Aiogram, Pyrogram, or Telebot in one line.
from tg_rich_converter import to_rich
llm_markdown = '''
# Quantum Computing Report
| Algorithm | State | Complexity |
|:----------|:-----:|-----------:|
| Grover | $|\psi\rangle$ | $O(\sqrt{N})$ |
### Key Formula
$$|\psi\rangle = \alpha|0\rangle + \beta|1\rangle$$
<think>
Verifying state normalization constraint...
</think>
'''
# Convert Markdown to native Telegram Rich HTML
rich_html = to_rich(llm_markdown, streaming=False)
# Send with Telegram Bot API sendRichMessage
await bot.send_rich_message(chat_id=chat_id, text=rich_html)