Skip to content

Conversation

@Sicheng-Pan
Copy link
Contributor

@Sicheng-Pan Sicheng-Pan commented Nov 8, 2025

Description of changes

Summarize the changes made by this PR.

Test plan

How are these changes tested?

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Migration plan

Are there any migrations, or any forwards/backwards compatibility changes needed in order to make sure this change deploys reliably?

Observability plan

What is the plan to instrument and monitor this change?

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs section?

@github-actions
Copy link

github-actions bot commented Nov 8, 2025

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

Copy link
Contributor Author

Sicheng-Pan commented Nov 8, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@Sicheng-Pan Sicheng-Pan mentioned this pull request Nov 8, 2025
1 task
@Sicheng-Pan Sicheng-Pan marked this pull request as ready for review November 8, 2025 00:34
@propel-code-bot
Copy link
Contributor

propel-code-bot bot commented Nov 8, 2025

Allow Key objects for sourceKey in TypeScript vector configs

This minor bug-fix PR extends TypeScript definitions and runtime handling so that VectorIndexConfig and SparseVectorIndexConfig accept either a raw string or an execution Key instance for the sourceKey field. Constructors now normalize the value to a plain key name, and new tests verify creation, serialization, and deserialization pathways.

Key Changes

• Updated VectorIndexConfigOptions.sourceKey and SparseVectorIndexConfigOptions.sourceKey type from string to string | Key in clients/new-js/packages/chromadb/src/schema.ts
• Added normalization logic in both constructors to map a Key instance to Key.name
• Imported Key type in schema.ts
• Appended three Jest tests in clients/new-js/packages/chromadb/test/schema.test.ts to cover Key handling, serialization, and both config classes

Affected Areas

clients/new-js/packages/chromadb/src/schema.ts (vector and sparse vector config constructors, type definitions)
clients/new-js/packages/chromadb/test/schema.test.ts (unit tests)

This summary was automatically generated by @propel-code-bot

@blacksmith-sh

This comment has been minimized.

@Sicheng-Pan Sicheng-Pan requested a review from jairad26 November 8, 2025 00:36
Comment on lines 75 to 95
this.sourceKey = options.sourceKey instanceof Key ? options.sourceKey.name : options.sourceKey ?? null;
this.hnsw = options.hnsw ?? null;
this.spann = options.spann ?? null;
}
}

export interface SparseVectorIndexConfigOptions {
embeddingFunction?: SparseEmbeddingFunction | null;
sourceKey?: string | null;
sourceKey?: string | Key | null;
bm25?: boolean | null;
}

export class SparseVectorIndexConfig {
readonly type = "SparseVectorIndexConfig";
embeddingFunction?: SparseEmbeddingFunction | null;
sourceKey: string | null;
bm25: boolean | null;

constructor(options: SparseVectorIndexConfigOptions = {}) {
this.embeddingFunction = options.embeddingFunction;
this.sourceKey = options.sourceKey ?? null;
this.sourceKey = options.sourceKey instanceof Key ? options.sourceKey.name : options.sourceKey ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

The logic to resolve the sourceKey from either a string or a Key instance is duplicated in the constructors for VectorIndexConfig (line 75) and SparseVectorIndexConfig (line 95).

To improve maintainability and adhere to the DRY (Don't Repeat Yourself) principle, you could extract this logic into a shared helper function.

For example:

function resolveSourceKey(sourceKey: string | Key | null | undefined): string | null {
  return sourceKey instanceof Key ? sourceKey.name : sourceKey ?? null;
}

Then, you can simplify both constructors:

// In VectorIndexConfig constructor
this.sourceKey = resolveSourceKey(options.sourceKey);

// In SparseVectorIndexConfig constructor
this.sourceKey = resolveSourceKey(options.sourceKey);
Context for Agents
[**BestPractice**]

The logic to resolve the `sourceKey` from either a `string` or a `Key` instance is duplicated in the constructors for `VectorIndexConfig` (line 75) and `SparseVectorIndexConfig` (line 95).

To improve maintainability and adhere to the DRY (Don't Repeat Yourself) principle, you could extract this logic into a shared helper function.

For example:
```typescript
function resolveSourceKey(sourceKey: string | Key | null | undefined): string | null {
  return sourceKey instanceof Key ? sourceKey.name : sourceKey ?? null;
}
```

Then, you can simplify both constructors:
```typescript
// In VectorIndexConfig constructor
this.sourceKey = resolveSourceKey(options.sourceKey);

// In SparseVectorIndexConfig constructor
this.sourceKey = resolveSourceKey(options.sourceKey);
```

File: clients/new-js/packages/chromadb/src/schema.ts
Line: 95

Comment on lines +1713 to +1731
it("accepts Key instance for VectorIndexConfig sourceKey", () => {
const { K } = require("../src/execution");
const schema = new Schema();
const vectorConfig = new VectorIndexConfig({
sourceKey: K.DOCUMENT,
});

expect(vectorConfig.sourceKey).toBe("#document");

// Also test with custom key
const customKey = K("myfield");
const vectorConfig2 = new VectorIndexConfig({
sourceKey: customKey,
});

expect(vectorConfig2.sourceKey).toBe("myfield");
});

it("accepts Key instance for SparseVectorIndexConfig sourceKey", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

The tests accepts Key instance for VectorIndexConfig sourceKey and accepts Key instance for SparseVectorIndexConfig sourceKey are nearly identical. You can use test.each to avoid this duplication and make the tests more concise.

The schema variable is also unused in these tests and can be removed.

Here's an example of how you could refactor this:

describe.each([
  { ConfigClass: VectorIndexConfig, name: "VectorIndexConfig" },
  { ConfigClass: SparseVectorIndexConfig, name: "SparseVectorIndexConfig" },
])("for $name", ({ ConfigClass }) => {
  it("accepts Key instance for sourceKey", () => {
    const { K } = require("../src/execution");
    const config = new ConfigClass({
      sourceKey: K.DOCUMENT,
    });
    expect(config.sourceKey).toBe("#document");

    const customKey = K("myfield");
    const config2 = new ConfigClass({
      sourceKey: customKey,
    });
    expect(config2.sourceKey).toBe("myfield");
  });
});

This single test block could replace both of the individual tests from lines 1713-1747.

Context for Agents
[**BestPractice**]

The tests `accepts Key instance for VectorIndexConfig sourceKey` and `accepts Key instance for SparseVectorIndexConfig sourceKey` are nearly identical. You can use `test.each` to avoid this duplication and make the tests more concise.

The `schema` variable is also unused in these tests and can be removed.

Here's an example of how you could refactor this:
```typescript
describe.each([
  { ConfigClass: VectorIndexConfig, name: "VectorIndexConfig" },
  { ConfigClass: SparseVectorIndexConfig, name: "SparseVectorIndexConfig" },
])("for $name", ({ ConfigClass }) => {
  it("accepts Key instance for sourceKey", () => {
    const { K } = require("../src/execution");
    const config = new ConfigClass({
      sourceKey: K.DOCUMENT,
    });
    expect(config.sourceKey).toBe("#document");

    const customKey = K("myfield");
    const config2 = new ConfigClass({
      sourceKey: customKey,
    });
    expect(config2.sourceKey).toBe("myfield");
  });
});
```
This single test block could replace both of the individual tests from lines 1713-1747.

File: clients/new-js/packages/chromadb/test/schema.test.ts
Line: 1731

Copy link
Contributor

@tjkrusinskichroma tjkrusinskichroma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Sicheng-Pan Sicheng-Pan force-pushed the 11-06-_doc_fix_mistakes_in_examples branch from 1a8e5bb to 70b442b Compare November 8, 2025 00:49
@Sicheng-Pan Sicheng-Pan force-pushed the 11-06-_bug_ts_vector_config_source_key_should_accept_key branch from ed794bd to c0291c1 Compare November 8, 2025 00:49
@blacksmith-sh

This comment has been minimized.

Copy link
Contributor Author

Sicheng-Pan commented Nov 8, 2025

Merge activity

  • Nov 8, 3:25 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Nov 8, 3:27 AM UTC: @Sicheng-Pan merged this pull request with Graphite.

@Sicheng-Pan Sicheng-Pan changed the base branch from 11-06-_doc_fix_mistakes_in_examples to graphite-base/5840 November 8, 2025 03:26
@Sicheng-Pan Sicheng-Pan changed the base branch from graphite-base/5840 to main November 8, 2025 03:26
@Sicheng-Pan Sicheng-Pan merged commit dfbb6c2 into main Nov 8, 2025
64 checks passed
@Sicheng-Pan Sicheng-Pan deleted the 11-06-_bug_ts_vector_config_source_key_should_accept_key branch November 8, 2025 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants