Skip to content

feat(core): add semantic embedding text prefixes#1044

Merged
phernandez merged 2 commits into
basicmachines-co:mainfrom
lastguru-net:feat/semantic-embedding-prefixes
Jul 15, 2026
Merged

feat(core): add semantic embedding text prefixes#1044
phernandez merged 2 commits into
basicmachines-co:mainfrom
lastguru-net:feat/semantic-embedding-prefixes

Conversation

@lastguru-net

Copy link
Copy Markdown
Contributor

Summary

Adds optional role-specific literal text prefixes for semantic embeddings:

  • semantic_embedding_document_prefix
  • semantic_embedding_query_prefix

These prefixes are prepended to the actual text sent to the embedding provider:

  • document prefix for indexed chunks during sync/reindex
  • query prefix for vector and hybrid search queries

Fixes #1008.

Why

Some embedding models require role-specific text prefixes as part of the input string, not as API parameters.

Examples include models where the recommended input format is something like:

  • documents: title: none | text: ...
  • queries: task: search result | query: ...

Basic Memory already supports LiteLLM input_type settings, but that only helps providers that understand an API-level role parameter. OpenAI-compatible endpoints such as TEI just receive the input strings, so prefix-sensitive models cannot currently be used in their recommended form without a proxy or local patch.
Without this, retrieval quality evaluation is misleading: a model can look worse than it really is because documents and queries are embedded without the format it was trained/evaluated with.

Implementation

  • Adds document/query prefix config fields.
  • Wraps embedding providers with a generic prefixing provider.
  • Works across fastembed, openai, and litellm.
  • Empty or unset prefixes preserve current behavior.
  • Includes prefixes in the provider cache key.
  • Includes prefixes in stored vector identity, so changing either prefix invalidates existing embeddings and requires reindexing.
  • Exposes prefix-enabled status in runtime/project status surfaces.
  • Documents the difference between LiteLLM input_type and literal text prefixes.

Validation

  • 201 passed focused tests
  • Ruff format/check on touched Python files
  • ty check src tests test-int
  • git diff --check

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7f7198d58a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/basic_memory/repository/prefixing_provider.py Outdated
@beru-ant-king
beru-ant-king force-pushed the feat/semantic-embedding-prefixes branch from 7f7198d to 0818b8d Compare July 7, 2026 19:12
@lastguru-net

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 0818b8d7da

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@phernandez phernandez changed the title feat: add semantic embedding text prefixes feat(core): add semantic embedding text prefixes Jul 15, 2026
lastguru-net and others added 2 commits July 15, 2026 01:33
Signed-off-by: Dmitry Golubev <lastguru@gmail.com>
Signed-off-by: phernandez <paul@basicmachines.co>
@phernandez
phernandez force-pushed the feat/semantic-embedding-prefixes branch from 0818b8d to 5aebf7d Compare July 15, 2026 06:34
@phernandez

Copy link
Copy Markdown
Member

Thanks for the contribution — I rebased this onto current main and completed the maintainer hardening directly on the branch. The feature still applies independent document/query text prefixes and now also keeps raw prefix text out of process cache keys and persisted vector identities by using stable SHA-256 digests. I replaced the speculative identity lookup with a narrow provider capability and expanded coverage for prefix application, invalidation, unset-vs-literal values, cache privacy, diagnostics, and SQLite resync behavior.\n\nValidation on the final local head: 3,754 unit tests passed (33 skipped), 230 focused semantic/config/status tests passed, and Ruff + ty passed.

@phernandez

Copy link
Copy Markdown
Member

@codex review

@phernandez

Copy link
Copy Markdown
Member

@codex recheck

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Unknown error
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@phernandez

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 5aebf7df9f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@phernandez
phernandez merged commit 4e0dbfa into basicmachines-co:main Jul 15, 2026
19 checks passed
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.

[FEATURE] Add role-specific text prefixes for semantic embedding queries and documents

2 participants