Problem/Motivation
Running an embeddings op (e.g. Ollama nomic-embed-text) breaks metering in two ways, both because the pipeline assumes a chat-shaped output.
It crashes: extractOutputText() calls method_exists() on getNormalized(), which for embeddings is the vector array, not an object. The subscriber swallows the TypeError and no row is written.
Once that is guarded, the counts are wrong: extractTokens() only handles ChatOutput, so EmbeddingsOutput falls through to the chat estimation. Input is guessed with mb_strlen/4 instead of the provider count, and output is forced to the 50-token floor, though an embedding has no output tokens.
Proposed resolution
- Guard
is_object()beforemethod_exists()inextractOutputText(). - Handle
EmbeddingsOutputinextractTokens(): real input tokens, output = 0, no estimation. - Kernel test: an embeddings op records input > 0, output = 0, no crash.
Environment
Drupal 11.3, ai 1.4.3, ai_provider_ollama (nomic-embed-text), ai_metering 1.0.0-beta1.
AI assistance
Found through live multi-provider testing; fix with AI assistance.
Issue fork ai_metering-3609167
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 #4
codeitwisely commented