Skip to content

📐 feat: add EMBEDDINGS_DIMENSIONS env for text-embedding-3 models - #287

Merged
danny-avila merged 2 commits into
mainfrom
claude/embeddings-dimensions
Apr 23, 2026
Merged

danny-avila merged 2 commits into
mainfrom
claude/embeddings-dimensions

Conversation

@danny-avila

Copy link
Copy Markdown
Collaborator

Summary

Adds a new EMBEDDINGS_DIMENSIONS environment variable that lets users request a reduced output vector size from text-embedding-3-small / text-embedding-3-large. OpenAI and Azure both support the dimensions parameter on these models; lower dimensionality reduces pgvector storage and makes similarity search faster, at some retrieval-quality trade-off.

  • Plumbed through init_embeddings for the openai and azure providers only (other providers don't accept the parameter and silently ignore it).
  • Unset by default — uses the model's native dimensionality (1536 / 3072).

Closes #67.

Caveat documented in README

Cannot be changed on an existing collection — all vectors in a pgvector column must share the same dimensionality. Changing it requires dropping and re-embedding the collection.

Test plan

  • Deploy with EMBEDDINGS_PROVIDER=openai, EMBEDDINGS_MODEL=text-embedding-3-small, EMBEDDINGS_DIMENSIONS=512 on a fresh collection; verify stored vectors have 512 dimensions and retrieval works.
  • Omit EMBEDDINGS_DIMENSIONS; verify behavior matches pre-change (native dimensionality).
  • CI (build-and-test) should pass.

Passes the dimensions parameter through to OpenAI / Azure OpenAI
embeddings clients when set, letting users shrink text-embedding-3-*
vectors below the model's native size (1536 / 3072). Lower
dimensionality reduces pgvector storage and similarity-search cost
at some retrieval-quality trade-off. Only the OpenAI and Azure
providers honor the parameter; other providers ignore it.

Closes #67
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 8627a15250

ℹ️ 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".

Comment thread app/config.py Outdated
Comment on lines +307 to +310
_embeddings_dimensions_raw = get_env_variable("EMBEDDINGS_DIMENSIONS", None)
EMBEDDINGS_DIMENSIONS = (
int(_embeddings_dimensions_raw)
if _embeddings_dimensions_raw not in (None, "")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Gate EMBEDDINGS_DIMENSIONS parsing by provider

EMBEDDINGS_DIMENSIONS is converted to int unconditionally during module import, before provider branching, so deployments using non-OpenAI/Azure providers can still fail at startup if this env var is present but non-numeric (for example, a leftover value from another environment). That contradicts the intended “silently ignore for other providers” behavior and turns an unrelated optional setting into a hard boot failure.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in e95f0de. Moved the int() cast inside a provider gate so it only runs for OpenAI / Azure; other providers short-circuit and leave EMBEDDINGS_DIMENSIONS=None even if the env var is set to a stale or non-numeric value.

Parsing the env var unconditionally at import turned an unrelated,
stale value (e.g. EMBEDDINGS_DIMENSIONS=foo left over from a prior
OpenAI deployment) into a hard boot failure under bedrock / hf /
ollama / vertexai even though those providers ignore the value.
Gate the int cast on a provider that actually honors it.
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

ℹ️ 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".

@danny-avila danny-avila changed the title ✨ feat: add EMBEDDINGS_DIMENSIONS env for text-embedding-3 models 📐 feat: add EMBEDDINGS_DIMENSIONS env for text-embedding-3 models Apr 23, 2026
@danny-avila
danny-avila merged commit 8d2a6a0 into main Apr 23, 2026
1 check passed
danny-avila added a commit that referenced this pull request Apr 23, 2026
Same class of bug codex caught on #287's EMBEDDINGS_DIMENSIONS: the
float() cast ran unconditionally at import, so a stale or templated
non-numeric value would prevent startup for atlas-mongo deployments
where the setting is already documented as ignored. Gate the parse
on VECTOR_DB_TYPE=pgvector so Atlas boots cleanly regardless of the
env var value.
danny-avila added a commit that referenced this pull request Apr 23, 2026
Same class of bug codex caught on #287's EMBEDDINGS_DIMENSIONS: the
float() cast ran unconditionally at import, so a stale or templated
non-numeric value would prevent startup for atlas-mongo deployments
where the setting is already documented as ignored. Gate the parse
on VECTOR_DB_TYPE=pgvector so Atlas boots cleanly regardless of the
env var value.
danny-avila added a commit that referenced this pull request Apr 24, 2026
* ✨ feat: add RAG_DISTANCE_THRESHOLD to drop weak retrieval matches

Applies a post-search filter on /query and /query_multiple that
discards any (document, score) pairs whose vector distance exceeds
the configured threshold. The score returned by
similarity_search_with_score_by_vector is a distance (lower = more
similar), so this prunes the less-relevant tail that top-k search
would otherwise surface. Unset by default — behavior unchanged for
existing deployments.

Closes #109

* 🐛 fix: scope RAG_DISTANCE_THRESHOLD to pgvector only

Atlas Vector Search returns similarity scores (higher = better),
the opposite of pgvector's distances (lower = better), so the
naive `score <= threshold` comparison would keep the weakest
matches and drop the strongest under VECTOR_DB_TYPE=atlas-mongo.
Skip the filter on Atlas and emit a one-time startup warning so
operators notice their setting is being ignored. A dedicated
Atlas-aware min-similarity knob can be added if there is demand.

* 🐛 fix: only parse RAG_DISTANCE_THRESHOLD under pgvector

Same class of bug codex caught on #287's EMBEDDINGS_DIMENSIONS: the
float() cast ran unconditionally at import, so a stale or templated
non-numeric value would prevent startup for atlas-mongo deployments
where the setting is already documented as ignored. Gate the parse
on VECTOR_DB_TYPE=pgvector so Atlas boots cleanly regardless of the
env var value.

* 🐛 fix: use raw env for Atlas RAG_DISTANCE_THRESHOLD warning

The parsed value is deliberately None under atlas-mongo (we skip the
float() cast so a stale non-numeric value can't break startup), so
checking it for the warning branch guaranteed the warning never fires.
Inspect os.getenv("RAG_DISTANCE_THRESHOLD") directly so Atlas operators
actually see that their configured threshold is being ignored instead
of assuming weak-match filtering is silently active.
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.

Allow specifying a dimensions parameters for size of text-embedding-3 models

1 participant