diff --git a/src/Results/DTO/Embedding.php b/src/Results/DTO/Embedding.php index 076109c6..4dbfb4b4 100644 --- a/src/Results/DTO/Embedding.php +++ b/src/Results/DTO/Embedding.php @@ -54,6 +54,12 @@ public function __construct(array $values, int $dimensions) throw new InvalidArgumentException('Embedding values must be integers or floats.'); } + foreach ($values as $value) { + if (is_float($value) && !is_finite($value)) { + throw new InvalidArgumentException('Embedding values must be finite numbers.'); + } + } + if (count($values) !== $dimensions) { throw new InvalidArgumentException('Embedding vector length must match dimensions.'); } diff --git a/tests/unit/Results/DTO/EmbeddingTest.php b/tests/unit/Results/DTO/EmbeddingTest.php index 00d17aa5..172dfb91 100644 --- a/tests/unit/Results/DTO/EmbeddingTest.php +++ b/tests/unit/Results/DTO/EmbeddingTest.php @@ -73,4 +73,27 @@ public function testValuesMustBeNumeric(): void new Embedding([0.1, '0.2'], 2); } + + /** + * @dataProvider nonFiniteValueProvider + */ + public function testValuesMustBeFinite(float $value): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Embedding values must be finite numbers.'); + + new Embedding([0.1, $value], 2); + } + + /** + * @return array + */ + public static function nonFiniteValueProvider(): array + { + return [ + 'NAN' => [NAN], + 'INF' => [INF], + '-INF' => [-INF], + ]; + } }