{"record":{"id":"55c7baecb7c563c4","repo":"cocoindex-io/cocoindex","slug":"turbopuffer-vectors-only-support-float32-or-float1","errorCode":null,"errorMessage":"Turbopuffer vectors only support float32 or float16, got {dt}.","messagePattern":"Turbopuffer vectors only support float32 or float16, got (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/connectors/turbopuffer/_target.py","lineNumber":242,"sourceCode":"    reserved = {\"id\"} | vector_field_names\n    if row.attributes:\n        for k, v in row.attributes.items():\n            if k in reserved:\n                raise ValueError(f\"Row {row.id!r}: attribute name {k!r} is reserved.\")\n            out[k] = v\n\n    return out\n\n\ndef _vector_type_str(vs: res_schema.VectorSchema) -> str:\n    \"\"\"Render a VectorSchema as turbopuffer's ``[N]fXX`` type string.\"\"\"\n    dt = np.dtype(vs.dtype)\n    if dt == np.float32:\n        suffix = \"f32\"\n    elif dt == np.float16:\n        suffix = \"f16\"\n    else:\n        raise ValueError(\n            f\"Turbopuffer vectors only support float32 or float16, got {dt}.\"\n        )\n    return f\"[{vs.size}]{suffix}\"\n\n\ndef _build_write_schema(schema: NamespaceSchema) -> dict[str, Any]:\n    \"\"\"Build the explicit ``schema`` payload passed to ``namespace.write()``.\"\"\"\n    out: dict[str, Any] = {}\n    if isinstance(schema.vectors, _ResolvedNamedVectorsDef):\n        for name, vd in schema.vectors.vectors.items():\n            out[name] = {\"type\": _vector_type_str(vd.schema), \"ann\": True}\n    else:\n        out[_DEFAULT_VECTOR_FIELD] = {\n            \"type\": _vector_type_str(schema.vectors.schema),\n            \"ann\": True,\n        }\n    return out\n","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/connectors/turbopuffer/_target.py#L224-L260","documentation":"Turbopuffer's ANN index supports only float32 (`f32`) and float16 (`f16`) vector element types. `_vector_type_str` renders the schema's dtype into turbopuffer's `[N]fXX` type string, and raises this ValueError if the VectorSchema's dtype is anything else (e.g. float64, bfloat16, int8), because no valid wire type string exists for it.","triggerScenarios":"Declaring a VectorDef whose schema produces vectors with dtype float64 (numpy's default `np.array([...])` without dtype=), or any non-float dtype, so that `_resolve_vector_def`/`_build_write_schema` calls `_vector_type_str` and fails.","commonSituations":"NumPy defaults: embeddings loaded via np.array(list) come out as float64; models/ONNX pipelines outputting bfloat16; int8-quantized embeddings being passed to turbopuffer directly.","solutions":["Cast the vector schema's dtype to float32 (or float16): np.asarray(embedding, dtype=np.float32) at the point vectors are produced.","Set dtype=np.float32 when constructing the numpy array backing the VectorDef schema.","If you need another precision (e.g. bfloat16 or int8 quantization), use a backend that supports it instead of turbopuffer."],"exampleFix":"// before\nvec = np.array(model.encode(text))  # float64\n\n// after\nvec = np.asarray(model.encode(text), dtype=np.float32)","handlingStrategy":"validation","validationCode":"import numpy as _np\ndt = _np.dtype(vec_schema.dtype)\nassert dt in (_np.float32, _np.float16), f\"Cast {dt} to float32/float16 for turbopuffer\"","typeGuard":"def is_tp_supported_dtype(dt: _np.dtype) -> bool:\n    return dt in (_np.float32, _np.float16)","tryCatchPattern":"try:\n    target = await NamespaceSchema.create(vectors=vdef, ...)\nexcept ValueError as e:\n    if \"only support float32 or float16\" in str(e):\n        raise ConfigError(\"Recast vector schema dtype to float32\") from e\n    raise","preventionTips":["Always construct embedding arrays with dtype=np.float32 explicitly","Never rely on np.array() defaults — float64 is the default for Python float lists","Validate vector schema dtype at app startup, before any write path runs"],"tags":["python","dtype","vector","turbopuffer"],"backgroundTag":"unsupported-dtype","analyzedSha":"e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b","analyzedAt":"2026-09-08T15:59:19.997Z","contentChangedAt":"2026-09-08T15:59:19.997Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}