tools.generate_image module
Generate images via native Gemini API and send to the current channel.
- async tools.generate_image.run(prompt, aspect_ratio='16:9', model=None, ctx=None)[source]
Generate an image from a prompt and post it to the current channel.
Entry point for the
generate_imagetool. It resolves a Gemini key with_resolve_api_key(preferring the user’s own), and – for users on the shared fallback key – enforces the_IMAGE_DAILY_LIMITdaily quota via_default_key_image_quota_appliesandcheck_default_key_limit(both backed by Redis throughmanage_api_keys), returning theIMAGE_RATE_LIMIT_ERRORJSON when exhausted. It then calls_call_gemini_nativeto render the image, normalises the bytes to PNG via the nested_convert_to_pngonasyncio.to_thread, and uploads the result to the channel throughctx.adapter.send_file, recording it inctx.sent_files. On a successful shared-key generation it bumps the usage counter withincrement_default_key_usage. Failures are caught and returned as a JSONerrorobject.Dispatched by
tool_loader.py, which imports this module and resolvesrunviagetattr(module, "run")to register it underTOOL_NAME(“generate_image”).- Parameters:
prompt (
str) – Text description of the image to generate.aspect_ratio (
str) – Requested aspect ratio (default"16:9"), passed through to_call_gemini_native.model (
str|None) – Optional Gemini image model id; defaults are applied downstream.ctx (
ToolContext|None) – The currentToolContext; itsadapteris required to send the image and itsuser_id/redisdrive quota enforcement. Whenctxorctx.adapteris missing the call returns an error.
- Return type:
- Returns:
A JSON string with
success: Trueplusfilename, aresultmessage, and (when available)file_urlon success; otherwise a JSON object with anerrordescribing a quota block, an empty result, or a raised exception.