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() before method_exists() in extractOutputText().
  • Handle EmbeddingsOutput in extractTokens(): 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.

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

codeitwisely created an issue. See original summary.

  • b87e54c6 committed on 1.0.x
    Issue #3609167 by codeitwisely: Track embeddings operations correctly
    
codeitwisely’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.