mem0ai/mem0 · error · Error

Databricks vector store only accepts finite numbers.

Error message

Databricks vector store only accepts finite numbers.

What it means

formatSqlValue() serializes values into SQL literals for Databricks writes. NaN and Infinity are valid JS numbers but have no SQL numeric literal, so writing them would produce broken or dangerous SQL. They are rejected with this error before the statement is built.

Source

Thrown at mem0-ts/src/oss/src/vector_stores/databricks.ts:140

// against lemmatized text. Same contract as baidu.ts / pgvector.ts.
function lemmatizedText(payload: Record<string, any>): string {
  const data = typeof payload.data === "string" ? payload.data : "";
  return typeof payload.textLemmatized === "string" &&
    payload.textLemmatized.length > 0
    ? payload.textLemmatized
    : data;
}

function formatSqlValue(value: any): string {
  if (value === null || value === undefined) {
    return "NULL";
  }
  if (typeof value === "boolean") {
    return value ? "TRUE" : "FALSE";
  }
  if (typeof value === "number") {
    if (!Number.isFinite(value)) {
      throw new Error("Databricks vector store only accepts finite numbers.");
    }
    return String(value);
  }
  if (Array.isArray(value)) {
    return `array(${value.map((entry) => formatSqlValue(entry)).join(", ")})`;
  }
  const json =
    typeof value === "string" ? value : JSON.stringify(value ?? {}) || "{}";
  // Databricks/Spark SQL treats backslash as an escape char in string literals,
  // so a trailing "\" would consume the closing quote (breaking out of the
  // literal) and any backslash would be dropped on write. Escape backslashes
  // before doubling quotes so the literal is injection-safe and round-trips.
  const escaped = json.replace(/\\/g, "\\\\").replace(/'/g, "''");
  return `'${escaped}'`;
}

function extractRowValue(row: Record<string, any>, keys: string[]): any {
  for (const key of keys) {

View on GitHub (pinned to 001c235229)

Solutions

  1. Sanitize numeric fields before saving: replace non-finite numbers with null or omit the field
  2. Fix the upstream computation producing NaN/Infinity (guard divide-by-zero, validate parseFloat results)

Example fix

// before
await memory.add('result', { metadata: { ratio: num / denom } }); // denom may be 0

// after
const ratio = Number.isFinite(num / denom) ? num / denom : null;
await memory.add('result', { metadata: { ratio } });
Defensive patterns

Strategy: validation

Validate before calling

function sanitizeNumbers(value: any): any {
  if (typeof value === 'number' && !Number.isFinite(value)) return null;
  if (Array.isArray(value)) return value.map(sanitizeNumbers);
  if (value && typeof value === 'object') {
    return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, sanitizeNumbers(v)]));
  }
  return value;
}
await memory.add(text, { metadata: sanitizeNumbers(metadata) });

Type guard

const isFiniteNumber = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);

Prevention

When it happens

Trigger: A memory payload or metadata field containing NaN/Infinity — e.g. a score computed as 1/0, parseFloat('abc') producing NaN, or JSON like {"score": Infinity} surviving a JSON.parse round-trip — reaching add()/update() on the Databricks store.

Common situations: Aggregated metrics (divisions by zero) stored alongside memories; numeric fields parsed from freeform user input; data pipelines that don't sanitize floats before persistence.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/34bec14e585b33eb. Report an issue: GitHub.