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

Bad Request: query is too old and response timeout expired or query ID is invalid — how to fix it

Bad Request: query is too old and response timeout expired or query ID is invalid means answerCallbackQuery came too late for a button press, or got the wrong ID. The handler did slow work before answering, or the bot was down and is now working through presses that queued up. Answer the callback query first, do the slow work after, and ignore this error for stale presses.

By Livops team, the people who build and run LivopsUpdated

The error, as the Telegram Bot API returns it for answerCallbackQuery:

{"ok":false,"error_code":400,"description":"Bad Request: query is too old and response timeout expired or query ID is invalid"}

Each library wraps it in its own words:

Library What you see
aiogram 3 TelegramBadRequest: Telegram server says - Bad Request: query is too old and response timeout expired or query ID is invalid[4][5]
python-telegram-bot telegram.error.BadRequest: Query is too old and response timeout expired or query id is invalid[10][11]
grammY GrammyError: Call to 'answerCallbackQuery' failed! (400: Bad Request: query is too old ...)[14]
Telegraf TelegramError: 400: Bad Request: query is too old and response timeout expired or query ID is invalid[17]

This page explains what the error means, lists the causes from the most common, and shows the fixes — answer first, deal with the backlog after a restart, ignore stale presses — in aiogram, python-telegram-bot, grammY and Telegraf. It ends with how it looks on Livops.

What does the error mean?

When a user presses an inline button with callback data, the bot receives a CallbackQuery. Telegram clients show a progress bar until the bot calls answerCallbackQuery, so the bot must call it even when it has nothing to show the user[1]. The answer can be a notification at the top of the chat or an alert; its only required parameter is callback_query_id, the unique identifier of the query[1].

Telegram’s servers report a refused answer as QUERY_ID_INVALID, and the Bot API server turns it into the text above with code 400 and the prefix Bad Request[2]. As the text says, there are two possibilities: the answer came after the query timed out, or the ID is not a valid query ID.

How long is too long? The Bot API page does not give a number for answerCallbackQuery[1]. grammY’s documentation notes that some clients may show a loading animation for up to a minute when a bot does not react to a press[3]; that is how long the user may wait, not a deadline you can plan around. Treat it simply: answer as soon as the update arrives.

Match the error by its description, not only by its code: a failed request has ok set to false and the reason in description, and the contents of error_code are subject to change[1].

Causes, most common first

1. Slow work before the answer

The handler calls a database, an external API or a language model, builds a file or sends a broadcast, and calls answerCallbackQuery only at the end. By then the press is too old.

A middleware can hide this. aiogram’s CallbackAnswerMiddleware answers every handled callback query automatically, and by default it does that after the handler has finished[6][7], so a slow handler means a late answer even though you never wrote callback.answer() at the end yourself.

2. The bot was down and is working through a backlog

Updates wait on Telegram’s server until the bot receives them, for up to 24 hours[1]. Buttons pressed while the bot was stopped, crashing or restarting — during a deploy, for example — arrive together when it starts again. They are already too old to answer, so each one fails in a row in the log, and then the errors stop.

3. Updates handled one at a time

If one slow handler holds the queue, every press behind it is answered late too. grammY’s built-in bot.start() handles a batch of updates one by one, waiting for each[15]. aiogram’s start_polling runs each update as its own task by default (handle_as_tasks=True)[9], so one slow handler does not hold up the others there.

4. The wrong ID, or an answer the query does not expect

callback_query_id must be the id of the CallbackQuery the bot received[1]. A message ID, the callback data, a chat ID or an ID saved from an earlier press does not work. If the error comes even on fast, fresh presses, check what you pass — and check that one press is answered once: with aiogram’s middleware on, change the answer through the callback_answer argument, or disable it for that handler[6], rather than also calling callback.answer() yourself.

How to fix it

Answer first, then do the slow work

Call answerCallbackQuery at the top of the handler — with a short “Working on it…” text if the work takes a while — and send the result afterwards as a new message or an edit.

# aiogram 3
@router.callback_query(F.data == "report")
async def report(callback: CallbackQuery):
    await callback.answer("Preparing the report…")
    text = await build_report()          # slow
    await callback.message.answer(text)
# python-telegram-bot
async def report(update: Update, context: ContextTypes.DEFAULT_TYPE):
    query = update.callback_query
    await query.answer("Preparing the report…")
    text = await build_report()
    await query.message.reply_text(text)
// grammY
bot.callbackQuery("report", async (ctx) => {
  await ctx.answerCallbackQuery("Preparing the report…");
  const text = await buildReport();
  await ctx.reply(text);
});
// Telegraf
bot.action("report", async (ctx) => {
  await ctx.answerCbQuery("Preparing the report…");
  const text = await buildReport();
  await ctx.reply(text);
});

query.answer(), ctx.answerCallbackQuery() and ctx.answerCbQuery() are each library’s shortcut for answering the current query[12][16][19]. In aiogram with CallbackAnswerMiddleware, pass pre=True to answer before the handler runs; then the answer’s text can no longer be changed inside the handler[6][7].

For work that takes minutes, answer, then hand the job to a queue or a background worker so the handler returns at once.

After a restart: drop the backlog, or ignore it

Both setWebhook and deleteWebhook take drop_pending_updates: pass true to drop all pending updates[1]. Each library exposes it at start:

Library On start
aiogram 3 await bot.delete_webhook(drop_pending_updates=True) before start_polling[8]
python-telegram-bot application.run_polling(drop_pending_updates=True)[13]
grammY bot.start({ drop_pending_updates: true })[15]
Telegraf bot.launch({ dropPendingUpdates: true })[18]

This drops every pending update — messages and commands too — not only old presses. Use it where a missed message does not matter; otherwise keep the backlog and ignore this one error for the stale presses.

Catch this exact error and ignore it

A stale press has no one left waiting for its answer. Catch the error around the answer, check its text, and raise everything else.

# aiogram 3: TelegramBadRequest carries Telegram's description
from aiogram.exceptions import TelegramBadRequest

try:
    await callback.answer()
except TelegramBadRequest as e:
    if "query is too old" not in e.message:
        raise
# python-telegram-bot: the text starts with "Query is too old", so compare in lower case
from telegram.error import BadRequest

try:
    await query.answer()
except BadRequest as e:
    if "query is too old" not in e.message.lower():
        raise
// grammY
import { GrammyError } from "grammy";

await ctx.answerCallbackQuery().catch((e) => {
  if (!(e instanceof GrammyError && e.description.includes("query is too old"))) throw e;
});
// Telegraf
import { TelegramError } from "telegraf";

await ctx.answerCbQuery().catch((e) => {
  if (!(e instanceof TelegramError && e.description.includes("query is too old"))) throw e;
});

The error classes are aiogram’s TelegramBadRequest[4][5], python-telegram-bot’s BadRequest, which drops the Bad Request: prefix and capitalizes the rest[10][11], grammY’s GrammyError with description[14], and Telegraf’s TelegramError with description[17].

Catching matters most in grammY and Telegraf. grammY with no bot.catch stops the bot on an uncaught error while bot.start() runs[15], and Telegraf’s default handler rethrows it[18] — so a backlog of old presses after a restart can take the bot down again.

On Livops

  • A bot is down during a deploy. A bot’s old version is stopped before the new one starts, because two copies polling Telegram would conflict, so the bot is down until a deploy succeeds. Presses made in that gap wait on Telegram and reach the new version when it starts — cause 2 above, briefly, after a deploy. See The app did not start.
  • If the backlog stops the bot. If an uncaught error stops the bot right after it starts, the deploy fails as start_failed, and the bot’s last words are on the Logs page.
  • Logs. The error lines are on the project’s Logs page; livops logs <service> -f follows them from the terminal (CLI), and get_logs over MCP returns up to 5000 lines. A short burst right after a deploy is the backlog; errors on fresh presses mean a slow answer.
  • Polling or webhook. Both work. With a webhook, Livops gives Telegram the address itself, so do not call setWebhook from your code — nor deleteWebhook, which removes the webhook[1]. The start options in the table above are for a bot that polls.

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

Questions

What does query is too old and response timeout expired mean?

Your bot called answerCallbackQuery for a button press that Telegram no longer accepts an answer for, or with an ID that is not a valid callback query ID. Most often the answer simply came too late: the handler did slow work first, or the press waited in the queue while the bot was down.

How long does a bot have to answer a callback query?

The Bot API documentation does not give a number for answerCallbackQuery. It says Telegram clients show a progress bar until the bot calls answerCallbackQuery, and that the bot should call it even when it has nothing to show. Answer as soon as the update arrives, before any slow work.

Why do I see it after a restart or deploy?

Telegram keeps updates for up to 24 hours until the bot receives them. Buttons pressed while the bot was down arrive all at once when it starts again, and by then they are too old to answer. The error stops once the backlog is worked through.

Is it safe to ignore this error?

Yes, for answerCallbackQuery on a press that is already stale: the user has stopped waiting, and nothing you send now will show. Catch only this exact error and raise the rest. If you see it on fresh presses, the handler is answering too late and should answer first.

Should I use drop_pending_updates?

Only if losing the backlog is fine. drop_pending_updates throws away every pending update, messages and commands included, not just old button presses. It suits bots whose buttons only make sense for a short time; bots that take orders or payments should rather process the backlog and ignore this one error.

Does the error mean the button did not work?

Not necessarily. Only the answer failed, so the user saw the loading indicator run out instead of a notification. The rest of your handler, such as editing the message, can still run if you catch the error or answered before the slow part.

Sources

  1. Telegram Bot API — Making requests, Getting updates, getUpdates, setWebhook, deleteWebhook, CallbackQuery, answerCallbackQuery (core.telegram.org). Checked .
  2. Telegram Bot API server source — telegram-bot-api/Client.cpp (raw.githubusercontent.com). Checked .
  3. grammY — Keyboards (Inline and Custom Keyboards) (grammy.dev). Checked .
  4. aiogram source — aiogram/exceptions.py (raw.githubusercontent.com). Checked .
  5. aiogram source — aiogram/client/session/base.py (raw.githubusercontent.com). Checked .
  6. aiogram — Callback answer (docs.aiogram.dev). Checked .
  7. aiogram source — aiogram/utils/callback_answer.py (raw.githubusercontent.com). Checked .
  8. aiogram source — aiogram/methods/delete_webhook.py (raw.githubusercontent.com). Checked .
  9. aiogram source — aiogram/dispatcher/dispatcher.py (raw.githubusercontent.com). Checked .
  10. python-telegram-bot source — telegram/error.py (raw.githubusercontent.com). Checked .
  11. python-telegram-bot source — telegram/request/_baserequest.py (raw.githubusercontent.com). Checked .
  12. python-telegram-bot source — telegram/_callbackquery.py (raw.githubusercontent.com). Checked .
  13. python-telegram-bot source — telegram/ext/_application.py (raw.githubusercontent.com). Checked .
  14. grammY source — src/core/error.ts (raw.githubusercontent.com). Checked .
  15. grammY source — src/bot.ts (raw.githubusercontent.com). Checked .
  16. grammY source — src/context.ts (raw.githubusercontent.com). Checked .
  17. Telegraf source — src/core/network/error.ts (raw.githubusercontent.com). Checked .
  18. Telegraf source — src/telegraf.ts (raw.githubusercontent.com). Checked .
  19. Telegraf source — src/context.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