Skip to content

Embedding accepts non-finite floating-point values #262

Description

@henryperkins

Summary

Embedding currently accepts NAN, INF, and -INF. Its internal validation checks only whether each value is an integer or float, so these non-finite floats pass validation even though they cannot be represented in JSON.

I observed this on PHP 8.3 against 860ce29 while #244 was open. The same validation remains in 66fa4d7, the final head that was merged.

Reproduction

use WordPress\AiClient\Results\DTO\Embedding;

foreach ([NAN, INF, -INF] as $value) {
    $embedding = new Embedding([$value], 1);

    var_dump(json_encode($embedding));
    echo json_last_error_msg() . PHP_EOL;
}

Each Embedding constructs successfully, but json_encode() returns false with:

Inf and NaN cannot be JSON encoded

The relevant check is in Embedding::isEmbeddingList().

Expected behavior

I think the safest boundary is for Embedding to reject non-finite floats with InvalidArgumentException, consistent with its existing list, numeric-type, and dimension validation. That prevents an invalid result object from reaching JSON serialization, logging, caching, persistence, or a custom provider integration.

Suggested coverage

Add regression coverage for:

  • NAN
  • INF
  • -INF
  • ordinary finite floats and integers continuing to be accepted

A focused check such as is_float($value) && !is_finite($value) would preserve the existing integer behavior.

Context

Embedding support was introduced in #244, which implemented #242.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions