{"record":{"id":"a3d794533c884d0f","repo":"chroma-core/chroma","slug":"if-sourcekey-is-provided-then-embeddingfunction-mu","errorCode":null,"errorMessage":"If sourceKey is provided then embeddingFunction must also be provided since there is no default embedding function. Config: ${JSON.stringify(config)}","messagePattern":"If sourceKey is provided then embeddingFunction must also be provided since there is no default embedding function\\. Config: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clients/new-js/packages/chromadb/src/schema.ts","lineNumber":887,"sourceCode":"      new IntInvertedIndexType(false, new IntInvertedIndexConfig()),\n    );\n    current.floatValue = new FloatValueType(\n      new FloatInvertedIndexType(false, new FloatInvertedIndexConfig()),\n    );\n    current.boolean = new BoolValueType(\n      new BoolInvertedIndexType(false, new BoolInvertedIndexConfig()),\n    );\n  }\n\n  private validateSparseVectorConfig(config: SparseVectorIndexConfig): void {\n    // Validate that if source_key is provided then embedding_function is also provided\n    // since there is no default embedding function\n    if (\n      config.sourceKey !== null &&\n      config.sourceKey !== undefined &&\n      !config.embeddingFunction\n    ) {\n      throw new Error(\n        `If sourceKey is provided then embeddingFunction must also be provided since there is no default embedding function. Config: ${JSON.stringify(\n          config,\n        )}`,\n      );\n    }\n  }\n\n  private initializeDefaults(): void {\n    this.defaults.string = new StringValueType(\n      new FtsIndexType(false, new FtsIndexConfig()),\n      new StringInvertedIndexType(true, new StringInvertedIndexConfig()),\n    );\n\n    this.defaults.floatList = new FloatListValueType(\n      new VectorIndexType(false, new VectorIndexConfig()),\n    );\n\n    this.defaults.sparseVector = new SparseVectorValueType(","sourceCodeStart":869,"sourceCodeEnd":905,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/clients/new-js/packages/chromadb/src/schema.ts#L869-L905","documentation":"Thrown when enabling a SparseVectorIndexConfig (via Schema.createIndex(config, key), which routes through setIndexForKey → validateSparseVectorConfig) whose sourceKey is set but whose embeddingFunction is not. Sparse vector indexes are produced by an embedding model; there is no default sparse embedding function, so a source key without a function to embed it is meaningless and is rejected before any server call.","triggerScenarios":"schema.createIndex(new SparseVectorIndexConfig({ sourceKey: 'body' }), 'body_sparse') with no embeddingFunction in the config object. Fires whenever config.sourceKey != null/undefined && !config.embeddingFunction (schema.ts:882-886).","commonSituations":"Copying a SparseVectorIndexConfig from serialization/JSON where the function was dropped (functions do not survive JSON round-trips); upgrading from an API where the sparse function was inferred; forgetting that sourceKey and embeddingFunction are a paired requirement.","solutions":["Pass an embedding function together with sourceKey: new SparseVectorIndexConfig({ sourceKey: 'body', embeddingFunction: mySparseEF })","If you already have precomputed sparse vectors, omit sourceKey — you will supply the vectors yourself in metadatas","Re-attach the function after deserializing a schema, since JSON.stringify strips it"],"exampleFix":"// before\nschema.createIndex(\n  new SparseVectorIndexConfig({ sourceKey: \"body\" }),\n  \"body_sparse\",\n);\n// after\nimport { DefaultSparseEmbeddingFunction } from \"chromadb\";\nschema.createIndex(\n  new SparseVectorIndexConfig({\n    sourceKey: \"body\",\n    embeddingFunction: new DefaultSparseEmbeddingFunction(),\n  }),\n  \"body_sparse\",\n);","handlingStrategy":"validation","validationCode":"function assertSparseConfigReady(cfg) {\n  if ((cfg.sourceKey ?? null) !== null && !cfg.embeddingFunction) {\n    throw new Error('SparseVectorIndexConfig needs embeddingFunction when sourceKey is set');\n  }\n}","typeGuard":"function isSparseConfigComplete(c): c is SparseVectorIndexConfig { return c instanceof SparseVectorIndexConfig && (c.sourceKey == null || !!c.embeddingFunction); }","tryCatchPattern":"try { schema.createIndex(cfg, key); } catch (e) { if (e instanceof Error && /embeddingFunction must also be provided/.test(e.message)) throw new Error('Attach a sparse embedding function before enabling this index'); else throw e; }","preventionTips":["Always construct SparseVectorIndexConfig with sourceKey and embeddingFunction together","Re-hydrate embedding functions after schema deserialization (JSON drops them)","Centralize sparse index setup in one factory function so the pairing cannot be forgotten"],"tags":["chromadb","schema","sparse-vectors","embedding-function","index-management"],"backgroundTag":"missing-embedding-function","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}