Error catching in AI task automations and scripts

I find that error catching in HASS is challenging to say the least but it seems to be even worse with AI task type automations… Can someone help me implement something that works for trapping errors with AI generative task?

I have a simple example AI type task automation for which I’m trying to implement error catching, because it throws errors and falls flat on its face more often than I’d like with (Gemini not to name it).

action: ai_task.generate_data
metadata: {}
data:
  task_name: Fireplace Status
  instructions: >-
    Analyze the fireplace in this image of the living room and determine its
    condition. Classify it strictly as one of flames, embers, cold or unknown.
  structure:
    condition:
      selector:
        select:
          options:
            - flames
            - embers
            - cold
            - unknown
  attachments:
    media_content_id: media-source://camera/camera.tapo_xxxxxxxxxxxx
    media_content_type: image/jpeg
  entity_id: ai_task.google_ai_task
continue_on_error: true
response_variable: response << Crashes here...

Even though I have “continue_on_error: true”, the automation still belly flops when there’s an error due to the Gemini service not being available either from exceeding usage limits (…or God only knows reason)… not because it doesn’t continue but because it can’t continue since “response_variable” remains “undefined”, which causes the automation to crash anyway when I try to access it…

Error checking can be hard, especially when it is multilevel, with dependencies.
While the ‘keep going’ option is good, you need to allow in the next layer up for errors to be encountered, and what to do if present.

If Gemini is not available, you need to issue a HAL9000 “I’m sorry, Dave. I’m afraid I can’t do that” response.

Venn diagrams of error conditions may assist in your program design.

Thanks for the quick response!

So, if understand you correctly, what I need to do is catch that “response_variable” is undefined and fall back on my feet from there.

You’re right: that will most probably work, (I’ll try it…)

but… it would be a whole lot better if “response_variable” actually returned an actual useful value such as: “service unavailable” don’t you think?