TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 519 of modules/contrib/ai/modules/ai_automators/src/Rulehelpers/GeneralHelper.php).

Created on 5 February 2025, 17 days ago

Problem/Motivation

I have setup a "Geoplace Type" Entity/Taxonomy Reference type field, with AI Automator enabled, and some settings on that ...
I am trying to make that field automatically populated with a Taxonomy term (existing or being created at runtime as new) on there basis of the analysis of an image referenced / uploaded into a Media Image field type on the same Content entity (being edited).
May be my AI Automator settings on this "Geoplace Type" Entity/Taxonomy Reference field are not correct (not the most appropriate) as I am still learning and testing with all this AI Drupal module (BTW super hyper mega thanks for all this ... ).
anyway ... whatever could be, when I try to Save the content the Drupal CMS instance breaks with the following error ...

The website encountered an unexpected error. Try again later.

TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 519 of modules/contrib/ai/modules/ai_automators/src/Rulehelpers/GeneralHelper.php).
Drupal\ai_automators\Rulehelpers\GeneralHelper->calculateTextFormat() (Line: 255)
Drupal\ai_automators\PluginBaseClasses\EntityReference->storeValues() (Line: 86)
Drupal\ai_automators\AiAutomatorRuleRunner->generateResponse() (Line: 70)
Drupal\ai_automators\Plugin\AiAutomatorProcess\DirectSaveProcessing->modify() (Line: 229)
Drupal\ai_automators\AiAutomatorEntityModifier->markFieldForProcessing() (Line: 129)
Drupal\ai_automators\AiAutomatorEntityModifier->saveEntity() (Line: 31)
ai_automators_entity_presave()
call_user_func_array() (Line: 355)
Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}() (Line: 307)
Drupal\Core\Extension\ModuleHandler->invokeAllWith() (Line: 354)
Drupal\Core\Extension\ModuleHandler->invokeAll() (Line: 217)
Drupal\Core\Entity\EntityStorageBase->invokeHook() (Line: 900)
Drupal\Core\Entity\ContentEntityStorageBase->invokeHook() (Line: 529)
Drupal\Core\Entity\EntityStorageBase->doPreSave() (Line: 753)
Drupal\Core\Entity\ContentEntityStorageBase->doPreSave() (Line: 483)
Drupal\Core\Entity\EntityStorageBase->save() (Line: 805)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (Line: 354)
Drupal\Core\Entity\EntityBase->save() (Line: 281)
Drupal\node\NodeForm->save()
call_user_func_array() (Line: 105)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers() (Line: 43)
Drupal\Core\Form\FormSubmitter->doSubmitForm() (Line: 589)
Drupal\Core\Form\FormBuilder->processForm() (Line: 144)
Drupal\autosave_form\Form\AutosaveFormBuilder->processForm() (Line: 321)
Drupal\Core\Form\FormBuilder->buildForm() (Line: 97)
Drupal\autosave_form\Form\AutosaveFormBuilder->buildForm() (Line: 73)
Drupal\Core\Controller\FormController->getContentResult() (Line: 39)
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 593)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 183)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 116)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 709)
Drupal\Core\DrupalKernel->handle() (Line: 19)

🐛 Bug report
Status

Active

Version

1.0

Component

AI Automators

Created by

🇮🇹Italy itamair

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @itamair
  • 🇬🇧United Kingdom MrDaleSmith

    Can you provide the settings you put on the field for the automator? We'll need to see what the AI was being asked to do to properly recreate the issue.

  • 🇮🇹Italy itamair

    Here it is ... @mrdalesmith

    uuid: f0db98a4-198c-463c-ad89-4156ade5cf36
    langcode: en
    status: true
    dependencies:
      config:
        - field.field.node.geo_image.field_geoplace_type
    id: node.geo_image.field_geoplace_type.default
    label: 'Geoplace Type Default'
    rule: llm_entity_reference
    input_mode: token
    weight: 100
    worker_type: direct
    entity_type: node
    bundle: geo_image
    field_name: field_geoplace_type
    edit_mode: true
    base_field: revision_log
    prompt: ''
    token: 'Select a Value based on the [node:field_geo_image_media:entity:field_media_image] of the Geo Image Media field for this content'
    plugin_config:
      automator_enabled: 1
      automator_rule: llm_entity_reference
      automator_entity_reference_bundle: geoplaces_types
      automator_entity_field_enable_tid: 0
      automator_entity_field_generate_tid: ''
      automator_entity_field_enable_revision_id: 0
      automator_entity_field_generate_revision_id: ''
      automator_entity_field_enable_revision_log_message: 0
      automator_entity_field_generate_revision_log_message: ''
      automator_entity_field_enable_name: 1
      automator_entity_field_generate_name: 'Select a Value based on the [node:field_geo_image_media:entity:field_media_image] of the Geo Image Media field for this content'
      automator_entity_field_enable_description: 0
      automator_entity_field_generate_description: ''
      automator_entity_field_enable_weight: 0
      automator_entity_field_generate_weight: ''
      automator_entity_field_enable_publish_state: 0
      automator_entity_field_generate_publish_state: ''
      automator_entity_field_enable_unpublish_state: 0
      automator_entity_field_generate_unpublish_state: ''
      automator_mode: token
      automator_base_field: revision_log
      automator_prompt: ''
      automator_token: 'Select a Value based on the [node:field_geo_image_media:entity:field_media_image] of the Geo Image Media field for this content'
      automator_edit_mode: 1
      automator_label: 'Geoplace Type Default'
      automator_weight: '100'
      automator_worker_type: direct
      automator_ai_provider: default_json
    
  • 🇬🇧United Kingdom MrDaleSmith

    OK there's a couple of things wrong with your field configuration that explains why it wasn't working:

    1. You should use the LLM: Taxonomy Reference AI Automator Type to populate a taxonomy term, rather than the generic LLM Entity Reference one.
    2. Your Automator Base Field is currently the Revision Log message - presumably this is where you're getting the error if the automator is trying to extract the value of this field and it hasn't been set when you save your node. The Automator Base Field should be the field that contains the context you want to send to the LLM.
    3. The LLM: Taxonomy Reference and LLM: Entity Reference automator types don't support sending an image as context to the AI - that requires using a different type of AI to analyse it and currently the automators for most fields only support using the "chat" operation type. Because of that, what you're trying to do won't currently be possible.
    4. The prompt you are using is using Drupal tokens to embed the value of the field: the Automator Prompts don't currently decode Drupal tokens so the image won't get sent to the AI this way either.
    5. Your prompt doesn't include the {{ context }} placeholder, so the base field you have selected won't get sent to the AI either.

    You might need to read through the instructions for the automators to help you configure them correctly - https://project.pages.drupalcode.org/ai/modules/ai_automators/

    However, I'd agree that the automator should handle misconfiguration of this kind in a more graceful manner, so I'll leave this issue open so we can prevent the fatal error.

  • Pipeline finished with Success
    16 days ago
    Total: 277s
    #417020
  • 🇮🇹Italy itamair

    Thanks al lot @mrdalesmith ... amazing explanation and support!
    I will read properly all the documentation.

    Hope you will make this error breaking everything more silently ..

  • 🇮🇹Italy itamair

    @ I QA and Tested the MR!458 state, but it just forward another error further in the ai_automator code workflow.

    It looks that I am not permitted to push commits into it,
    thus I describe my contribution / solution ...
    https://git.drupalcode.org/project/ai/-/merge_requests/458#note_458716

    That further change (that I suggest) solves this isse to me.

  • 🇬🇧United Kingdom MrDaleSmith

    If you have changes to push you need to click the green "get push access" button above and make sure you've followed the instructions at https://www.drupal.org/docs/develop/git/using-gitlab-to-contribute-to-dr... for whichever method you want to use to contribute.

  • 🇮🇹Italy itamair

    Ah damn! Right you are ... I simply forgot about that "get push access" button.
    So I did and pushed my further commit.
    As said, in this state MR!458 fixes this issue and use case of mine ...

  • Pipeline finished with Success
    15 days ago
    Total: 208s
    #417927
  • 🇬🇧United Kingdom MrDaleSmith

    I'm going to knock this back to needs review so someone can test it who didn't do the code changes.

Production build 0.71.5 2024