Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Node.js, PHP and APIs · Troubleshooting walkthrough

aiogram 3 refuses an inline keyboard with a pydantic ValidationError

Short answer

InlineKeyboardMarkup in aiogram 3 is a pydantic model with one required field, inline_keyboard, holding a list of rows of buttons. Field required means the markup was built empty in the aiogram 2 style; inline_keyboard.0.0 means a flat list of buttons. Pass inline_keyboard=[[button, ...]] or use InlineKeyboardBuilder and as_markup().

  • Applies to aiogram 3.33.0, pydantic 2.14.4, Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

aiogram 3 turned every Telegram type into a pydantic model. InlineKeyboardMarkup now has a required inline_keyboard field typed list[list[InlineKeyboardButton]]: an outer list of rows, each row a list of buttons. Anything else is rejected the moment the object is created, before a request reaches Telegram.

The second line of the traceback tells you which mistake you made. Pydantic prints a location path (inline_keyboard, or inline_keyboard.0.0) and an error type in square brackets. Match those two things to the cases below and the fix is a few lines.

The text you see

  • pydantic_core._pydantic_core.ValidationError: 1 validation error for InlineKeyboardMarkup
  • inline_keyboard Field required [type=missing, input_value={}, input_type=dict]
  • inline_keyboard Field required [type=missing, input_value={'row_width': 2}, input_type=dict]
  • inline_keyboard.0.0 Input should be a valid dictionary or instance of InlineKeyboardButton [type=model_type, input_value=('text', 'Yes'), input_type=tuple]

Stage 1

Read the location path pydantic printed

Each dot in the path steps one level into the data: the field name, then the row index, then the button index. So inline_keyboard.0.0 reads as "row 0, button 0 of the field inline_keyboard". The bracketed type names the rule that failed; pydantic's error reference describes missing as a required field absent from the input, and model_type as an input that is neither an instance of the model nor a dict.

Path and typeWhat the code didSection
`inline_keyboard` with `type=missing`, `input_value={}`Created the markup with no argumentsAn empty markup from aiogram 2 code
`inline_keyboard` with `type=missing`, `input_value={'row_width': 2}`Passed `row_width`, which aiogram 3 does not knowAn empty markup from aiogram 2 code
`inline_keyboard.0.0` with `type=model_type` and a tuplePassed one flat list of buttonsButtons where rows belong
`TypeError: BaseModel.__init__() takes 1 positional argument`Gave a button or the markup positional argumentsPositional arguments

Stage 2

An empty markup from aiogram 2 code

aiogram 2 let you create InlineKeyboardMarkup(row_width=2) and fill it afterwards with add(), row() or insert(). The aiogram migration guide says the markup classes no longer have methods for extension and sends you to the markup builders instead. With nothing in inline_keyboard, validation fails on the constructor line; an unknown keyword such as row_width is simply carried into the input, which is why it appears in input_value.

InlineKeyboardBuilder replaces the whole pattern. button() takes the same keyword fields as InlineKeyboardButton, adjust() sets the row sizes, and as_markup() produces the validated markup.

aiogram 3 replacement for add() and row_width

from aiogram.utils.keyboard import InlineKeyboardBuilder

builder = InlineKeyboardBuilder()
for size in ("S", "M", "L", "XL", "XXL"):
    builder.button(text=size, callback_data=f"size:{size}")
builder.adjust(3, 2)          # first row 3 buttons, then 2
await message.answer("Pick a size", reply_markup=builder.as_markup())
  • adjust(2) on seven buttons gives rows of 2, 2, 2 and 1: when the sizes run out, the last size repeats for the rest. adjust(3, 2, repeat=True) cycles through the sizes instead.
  • Without adjust(), aiogram 3.33 packs up to eight buttons per row (thirteen buttons came out as rows of 8 and 5).
  • row(*buttons) adds a fixed row, and InlineKeyboardBuilder.from_markup(markup) turns an existing markup back into a builder you can extend.

Stage 3

Buttons where rows belong

The most confusing variant is the tuple in input_value. Hand the field [button_a, button_b] and pydantic expects button_a to be a row. A pydantic model can be iterated as (field name, value) pairs, so pydantic walks into the button, takes ('text', 'Yes') as the first "button" of that row, and reports that a tuple is not an InlineKeyboardButton. The fix is one more pair of brackets.

Rows are inner lists

from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup

# wrong: one flat list
# InlineKeyboardMarkup(inline_keyboard=[yes, no])

markup = InlineKeyboardMarkup(inline_keyboard=[
    [InlineKeyboardButton(text="Yes", callback_data="vote:yes"),
     InlineKeyboardButton(text="No", callback_data="vote:no")],      # row 0
    [InlineKeyboardButton(text="Docs", url="https://docs.aiogram.dev/")],  # row 1
])

Plain dicts such as {"text": "Yes", "callback_data": "vote:yes"} inside the rows also validate, because model_type accepts a dict. Mixing dicts and button objects works, but one style per project is easier to read.

Stage 4

Positional arguments

This one is not a ValidationError at all, but it shows up during the same migration and on the same lines. aiogram 3 constructors are keyword-only, so InlineKeyboardButton("Press me", callback_data="click") and InlineKeyboardMarkup([[...]]) stop at pydantic's __init__ with a TypeError. Name the arguments: text= for the label and inline_keyboard= for the rows.

The migration guide warns about a quieter relative. Bot API calls still accept positional arguments, but new parameters were added in the middle of the signatures: message.answer(text, "HTML") now passes "HTML" as a topic id, which fails validation on a different model, and some misbound calls succeed with the wrong values. Pass keywords everywhere while you are in the file.

Find aiogram 2 style calls left in the project

grep -rnE "InlineKeyboardButton\(\"|InlineKeyboardMarkup\(\[|InlineKeyboardMarkup\(row_width|\.answer\(\"[^\"]*\", " --include=*.py .

Stage 5

Confirm the keyboard before Telegram sees it

Because validation happens at construction time, a unit test that builds each keyboard catches every case on this page without a bot token or a network call. Pydantic does not check Telegram's own rules, though: aiogram's button reference says exactly one field besides text, icon_custom_emoji_id and style must define what the button does, and that callback_data is 1 to 64 bytes. A button with only text, or a long callback_data, builds fine and is refused by Telegram when the message is sent, so test those limits yourself.

A construction test for one keyboard

def test_size_keyboard():
    markup = size_keyboard()          # your function that returns as_markup()
    rows = markup.inline_keyboard
    assert [len(r) for r in rows] == [3, 2]
    for row in rows:
        for b in row:
            assert b.callback_data and len(b.callback_data.encode()) <= 64

If you only keep one thing

  • inline_keyboard is required and is a list of rows.
  • Field required means aiogram 2 style construction; switch to InlineKeyboardBuilder.
  • inline_keyboard.0.0 with a tuple means a flat list; wrap the buttons in a row.
  • Every aiogram 3 type and Bot API call takes keyword arguments.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.