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
- Install the AI core module with the text classification patch from #3456768
- Install and enable
ai_provider_huggingface - Visit
/admin/config/ai/providers/huggingface— there is no "Add Text Classification Model" button - 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
- #3456768: Interface suggestion for text classification operator type — adds the
text_classificationoperation type to AI core
Issue fork ai_provider_huggingface-3583809
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
Comment #3
ahmad khader commentedComment #4
ahmad khader commentedSteps to test
/admin/config/ai/providers/huggingface— there is now "Add Text Classification Model" buttonnlptown/bert-base-multilingual-uncased-sentiment— sentiment analysis (1-5 stars) ordistilbert-base-uncased-finetuned-sst-2-english— positive/negative sentiment/admin/config/ai/explorers/text_classification_generatorAdditional 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 a400 Bad Requesterror:{"error":"'list' object has no attribute 'setdefault'"}The fix is to omit the
parameterskey when the array is empty, or cast it to(object)so it serializes as{}.Comment #5
ahmad khader commentedComment #6
ahmad khader commentedComment #7
arianraeesi commentedComment #8
csakiistvan@ahmad-khader please change the MR to not Draft it the issue is ready to test.
Comment #9
csakiistvanComment #10
csakiistvanTesting report for #3583809 — Add text classification support (Huggingface Provider)
Environment:
aimodule 1.4.x-dev (required forTextClassification*classes from #3456768)ai_provider_huggingface1.0.x-dev + MR !8 appliedStep 1 — Review and apply the MR
Applied MR !8 (
3583809-add-text-classification) on top of1.0.x. The MR adds the following toHuggingfaceProvider:TextClassificationInterfacetext_classificationtogetSupportedOperationTypes()and$supportedTypestextClassification()method with proper response normalizationHuggingfaceApi::textClassification()to send empty parameters as{}(object) instead of[](array), preventing 400 errorsStep 2 — CI pipeline failures identified and fixed
The MR had 4 failing CI jobs. Root cause:
TextClassification*classes exist only inai 1.4.x(added via #3456768, merged 2026-04-22), butcomposer.jsonrequireddrupal/ai: ^1.3.x-dev@devwhere 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.3.x-dev@devto^1.4.x-dev@devusestatement ordering to alphabetical (I → O → S → T), movingTextClassificationimports afterSummarizationAfter these fixes,
phpcs --standard=Drupal,DrupalPracticepasses with no errors.Step 3 — Enable required modules
The Text Classification Explorer at
/admin/config/ai/explorers/text_classification_generatorrequires theai_api_explorersub-module, which was not enabled by default: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) callsTextClassificationGenerator::isActive(), which returnsfalseif no usable provider exists — even for user 1. "Usable" requires the API key to be set./admin/config/ai/settings/admin/config/ai/providers/text_classification/huggingfaceExample model endpoint:
distilbert/distilbert-base-uncased-finetuned-sst-2-englishStep 5 — Test the Text Classification Explorer
Navigated to
/admin/config/ai/explorers/text_classification_generator. The form loaded correctly with:positive,negative,neutral)Submitted test input. The provider called
HuggingfaceApi::textClassification(), the response was decoded and normalized intoTextClassificationItemobjects, and the result table displayed label + confidence score correctly.Summary
TextClassification*not found in ai 1.3.x)^1.4.x-dev@devusestatement ordering fix (phpcs)ai_api_explorer{}(not[]) — no 400 errorsStatus recommendation: Reviewed & tested by the community
The MR needs two additional changes before merge:
composer.json: changedrupal/airequirement to^1.4.x-dev@dev(required forTextClassification*classes)HuggingfaceProvider.php: fixusestatement order to pass phpstan/phpcs CIComment #11
ahmad khader commentedThanks for the review. I updated the version. seems enough to fix all pipelines