Skip to content

fix(ai): Valkey embedding key ignores the request model #4569

Description

@dan-petty

Problem Description

The Valkey tier of the embedding cache keys each vector by the configured model name and the text (EmbeddingsEngine._valkey_key, src/devops_cli/ai/rag/embeddings.py). The key does not hold the provider or the model name that the request sends. _openai_request_model sends text-embedding-3-small (1,536 dimensions) for qwen3-embedding:0.6b when the provider is openai or copilot with no gateway. The gateway sends qwen3-embedding:0.6b (1,024 dimensions). Both runs write to and read from the same Valkey key. When one Valkey serves both kinds of caller (the central store of #4517), a gateway run can get a vector of 1,536 dimensions, and cosine_similarity then gives 0.0, so intake can miss a duplicate.

PR #4546 fixed the same defect in the disk tier: its key holds the provider and the request model, and it records one vector length for each key. The Valkey tier is out of the scope of that PR.

Reproduction Steps

  1. Configure ai.provider: openai with no gateway and ai.tasks.embedding.model: qwen3-embedding:0.6b, with a Valkey at valkey.url.
  2. Embed a text with EmbeddingsEngine.
  3. Configure the gateway provider with the same model and the same Valkey, and embed the same text.

Expected vs Actual Behavior

Expected: the second run misses Valkey and sends the text to the gateway.
Actual: the second run gets the vector of 1,536 dimensions from Valkey.

Environment Diagnostics

Source read on origin/main 5f75d25 and on the branch of PR #4546.

Logs, Traces, or Terminal Output

None. Found by code reading during the review fix of PR #4546.

Proposed design

Problem statement

The Valkey embedding cache keys each vector by the configured model and the text. A direct OpenAI request sends text-embedding-3-small for qwen3-embedding:0.6b, but a gateway request sends qwen3-embedding:0.6b. Both requests use the same Valkey key. A gateway run can get a 1,536-dimension vector, and cosine_similarity returns 0.0. Intake can miss a duplicate. The fix makes the Valkey key hold the provider and the request model, as the disk tier does.

Acceptance criteria

Requirement: Valkey key holds the request identity

The Valkey embedding cache SHALL key each vector by the provider and the model name that the embedding request sends.

Scenario: Direct OpenAI and gateway requests use different keys

  • WHEN A direct OpenAI engine and a gateway engine both use the configured model qwen3-embedding:0.6b and embed the same text.
  • THEN The two engines produce different Valkey keys for that text and query flag.

Requirement: Gateway run misses a direct OpenAI entry

A Valkey entry that a direct OpenAI request writes MUST NOT be returned to a gateway request for the same configured model.

Scenario: A gateway lookup after a direct OpenAI write misses

  • WHEN A direct OpenAI engine writes the vector for a text to Valkey, and a gateway engine with the same configured model reads that text.
  • THEN The gateway engine gets no vector from Valkey and sends the text to the gateway.

Requirement: Disk and Valkey tiers share one model identity

The disk tier and the Valkey tier SHALL use the same provider and request-model identity for an embedding.

Scenario: A disk hit writes to the Valkey key of the same request model

  • WHEN The disk cache holds a vector for a text under the provider and request-model identity, and the engine reads that text.
  • THEN The engine writes the vector to the Valkey key built from the same identity.

Existing Tool Considered

No alternatives check has run. No open, maintained tool or library covers this change; it edits the project's own cache-key code in EmbeddingsEngine. The change edits about 15 lines and adds tests. It costs about 3,000 output tokens to write, 1,000 tokens to review, and less than one hour. No separate code is kept; the existing cache tests cover it.

Key questions

  • Which identity must the Valkey embedding key hold?: The provider and the request model that the request sends, which is the identity _disk_cache_model already builds for the disk tier. (best practice) [sources: src/devops_cli/ai/rag/embeddings.py:1002, src/devops_cli/ai/rag/embeddings.py:1156, tests/ai/rag/test_embedding_disk_cache.py]
    • Rejected: Configured model only. A direct OpenAI request sends text-embedding-3-small for qwen3-embedding:0.6b, so a gateway request collides with it.
    • Chosen: Provider and request model. This separates a direct OpenAI request from a gateway request and matches the disk tier.
    • Rejected: Provider and configured model. The provider alone does not separate the two request models.
  • Should the disk and Valkey tiers share one key helper?: Yes. Rename _disk_cache_model to a neutral helper and use it in both tiers. (best practice) [sources: src/devops_cli/ai/rag/embeddings.py:1002, src/devops_cli/ai/rag/embeddings.py:1156]
    • Chosen: One shared helper. One identity cannot drift between the tiers.
    • Rejected: A second expression in _valkey_key. Two copies can diverge and bring back the collision.
    • Rejected: Leave the disk tier unchanged and change only Valkey. The two tiers would name the same request model differently.
  • Should a Valkey entry also record its vector length?: No. The key now names the provider and the request model, so one key maps to one vector length. A length field would change the stored value format and add migration work for a collision that the key already prevents. (best practice) [sources: src/devops_cli/ai/rag/embeddings.py, tests/valkey/test_valkey_cache_tier.py]
    • Chosen: Key identity only. The request model fixes the dimension, and the key now holds that model.
    • Rejected: Store {dimension, vector}. It changes the value format and needs a reader for old entries.
    • Rejected: Reject a vector when the known dimension differs. It adds a dimension lookup on the read path and does not separate the two request models.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-12: none.
Tool: none found.

Activity

  1. added
    source/agent
    type/bug
    scope/ai
    status/new
    value/medium
    effort/low
    on Nov 11, 2026
  2. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/ai. The (ai) scope of the title gives the scope.
    • Priority: P2-Medium. Wrong-dimension vectors cause missed duplicates, but only when two provider types share one Valkey store.
    • Value: Medium. It prevents silent data corruption in similarity scores for multi-provider setups.
    • Effort: Low. The fix mirrors PR #4546 disk-tier change: add provider and model to the key string in one function.
    • Related: perf(ai): keep intake embeddings between CLI runs #4447, which asks for a different change. Both concern the Valkey tier of the embedding cache, but #4447 enables the cache while this candidate fixes a key collision in it.
  3. added
    priority/p2-medium
    on Nov 11, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 11, 2026
  5. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 4330ee4 (see proposed design in issue body).

  6. devops-cli-service commented on Nov 12, 2026

    @devops-cli-service
    Contributor

    Alternatives check: none

    Reason: The searches turned up no open, maintained tool or library that supplies this change: they returned only generic cache, Redis/Redis Stack image and MDN pages, none of which keys an embedding cache by the provider and request model. The need is an edit to the project's own cache-key code in EmbeddingsEngine (_valkey_key, sharing the identity helper of the disk tier), which no external tool can provide.

    Tool: none found

    Lacks:

    • No tool covers building the Valkey embedding key from the provider and the request model that the request sends
    • No tool covers sharing one provider and request-model identity helper between the disk and Valkey cache tiers
    • The issue is a ~15-line fix in the project's own EmbeddingsEngine code, not an integration of an external library

    Searches run:

    • Valkey embedding cache key provider and model name collision library, results: 5
    • open source embedding cache library keyed by provider model and text, results: 5
    • GPTCache embedding cache redis store dimension mismatch different models key, results: 5
    • redis embedding cache key include model name avoid dimension mismatch bug, results: 5

    Checked on 2026-11-12. A person decides what this means for the item. The check changes no label and closes no issue.

  7. added
    status/in-review
    and removed
    status/ready
    on Nov 14, 2026
  8. removed
    status/in-review
    on Nov 14, 2026
  9. added a commit that references this issue on Nov 14, 2026
  10. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor
  11. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-14 15:02 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/aisource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions