Problem/Motivation

The AI core module is adding a new text_classification operation type in #3456768 (MR !1400). The Hugging Face provider already has the low-level API client method HuggingfaceApi::textClassification() but the provider plugin (HuggingfaceProvider) does not implement TextClassificationInterface, so the operation type is not available to users.

The Hugging Face project page already lists Text Classification as a feature, and the Inference API supports it via the text-classification pipeline.

Steps to reproduce

  1. Install the AI core module with the text classification patch from #3456768
  2. Install and enable ai_provider_huggingface
  3. Visit /admin/config/ai/providers/huggingface — there is no "Add Text Classification Model" button
  4. The Text Classification Explorer at /admin/config/ai/explorers/text_classification_generator

Proposed resolution

Wire up the existing HuggingfaceApi::textClassification() client method to the provider plugin by:

Depends on

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ahmad khader created an issue. See original summary.

ahmad khader’s picture

ahmad khader’s picture

Steps to test

  1. Install the AI core module with the text classification patch from #3456768
  2. Visit /admin/config/ai/providers/huggingface — there is now "Add Text Classification Model" button
  3. Add end point like nlptown/bert-base-multilingual-uncased-sentiment — sentiment analysis (1-5 stars) or
    distilbert-base-uncased-finetuned-sst-2-english — positive/negative sentiment
  4. Go to the Text Classification Explorer at /admin/config/ai/explorers/text_classification_generator

Additional fix needed in HuggingfaceApi

The existing HuggingfaceApi::textClassification() method always sends 'parameters' => [] in the request payload. The Hugging Face Inference API interprets an empty PHP array as a JSON list [], which causes a 400 Bad Request error:

{"error":"'list' object has no attribute 'setdefault'"}

The fix is to omit the parameters key when the array is empty, or cast it to (object) so it serializes as {}.

ahmad khader’s picture

Title: Add Text Classification support to Hugging Face provider » Add Text Classification support
ahmad khader’s picture

Assigned: ahmad khader » Unassigned
arianraeesi’s picture

csakiistvan’s picture

@ahmad-khader please change the MR to not Draft it the issue is ready to test.

csakiistvan’s picture

Assigned: Unassigned » csakiistvan
csakiistvan’s picture

Assigned: csakiistvan » Unassigned
Status: Needs review » Needs work

Testing report for #3583809 — Add text classification support (Huggingface Provider)


Environment:

  • Drupal 11.3.9, PHP 8.4.12, DDEV (OrbStack)
  • ai module 1.4.x-dev (required for TextClassification* classes from #3456768)
  • ai_provider_huggingface 1.0.x-dev + MR !8 applied
  • Huggingface API key configured

Step 1 — Review and apply the MR

Applied MR !8 (3583809-add-text-classification) on top of 1.0.x. The MR adds the following to HuggingfaceProvider:

  • Implements TextClassificationInterface
  • Adds text_classification to getSupportedOperationTypes() and $supportedTypes
  • Adds the textClassification() method with proper response normalization
  • Fixes HuggingfaceApi::textClassification() to send empty parameters as {} (object) instead of [] (array), preventing 400 errors

Step 2 — CI pipeline failures identified and fixed

The MR had 4 failing CI jobs. Root cause: TextClassification* classes exist only in ai 1.4.x (added via #3456768, merged 2026-04-22), but composer.json required drupal/ai: ^1.3.x-dev@dev where these classes do not exist.

phpstan error (non-ignorable):

Line 47: Class HuggingfaceProvider implements unknown interface
         Drupal\ai\OperationType\TextClassification\TextClassificationInterface
         [interface.notFound (non-ignorable)]

Line 399: Method textClassification() has invalid return type
          Drupal\ai\OperationType\TextClassification\TextClassificationOutput
          [class.notFound]

Two fixes applied:

  1. composer.json: updated dependency from ^1.3.x-dev@dev to ^1.4.x-dev@dev
  2. HuggingfaceProvider.php: corrected use statement ordering to alphabetical (I → O → S → T), moving TextClassification imports after Summarization

After these fixes, phpcs --standard=Drupal,DrupalPractice passes with no errors.


Step 3 — Enable required modules

The Text Classification Explorer at /admin/config/ai/explorers/text_classification_generator requires the ai_api_explorer sub-module, which was not enabled by default:

drush pm:enable ai_api_explorer -y
drush cr

Routes are generated dynamically by AiApiExplorerRouteSubscriber — without this module the path returns 404.


Step 4 — Configure Huggingface API key and add a model

The explorer access check (AiApiExplorerAccessChecker) calls TextClassificationGenerator::isActive(), which returns false if no usable provider exists — even for user 1. "Usable" requires the API key to be set.

  1. Set the Huggingface API key at /admin/config/ai/settings
  2. Add a text classification model at /admin/config/ai/providers/text_classification/huggingface
    Example model endpoint: distilbert/distilbert-base-uncased-finetuned-sst-2-english

Step 5 — Test the Text Classification Explorer

Navigated to /admin/config/ai/explorers/text_classification_generator. The form loaded correctly with:

  • A text input area for the content to classify
  • An optional Labels textarea (newline-separated, e.g. positive, negative, neutral)
  • Provider/model selector dropdown

Submitted test input. The provider called HuggingfaceApi::textClassification(), the response was decoded and normalized into TextClassificationItem objects, and the result table displayed label + confidence score correctly.


Summary

What was tested Result
MR code review — interface implementation and method logic ✅ Pass
CI pipeline failures diagnosed (phpstan: TextClassification* not found in ai 1.3.x) ✅ Fixed — bumped dependency to ^1.4.x-dev@dev
use statement ordering fix (phpcs) ✅ Fixed
Explorer route available after enabling ai_api_explorer ✅ Pass
Text classification returns correct labels and confidence scores ✅ Pass
Empty parameters sent as {} (not []) — no 400 errors ✅ Pass

Status recommendation: Reviewed & tested by the community

The MR needs two additional changes before merge:

  1. composer.json: change drupal/ai requirement to ^1.4.x-dev@dev (required for TextClassification* classes)
  2. HuggingfaceProvider.php: fix use statement order to pass phpstan/phpcs CI
ahmad khader’s picture

Status: Needs work » Needs review

Thanks for the review. I updated the version. seems enough to fix all pipelines