Skip to content

fix(ai): halve an embedding batch only when one text can cause it #6722

Done

Description

@dan-petty

Problem

Checked on 2026-11-22 (main 6e497c2), in the build of #6717 (PR #6719).

  • With direct Ollama nodes, _embed_batch_with_subdivision (src/devops_cli/ai/rag/embeddings.py:1088-1102) halves a batch after any EmbeddingsError and tries each half again, down to one text.
  • A permanent error, such as HTTP 404 for a model that the node does not serve, gives the same reply for each half. So one batch of n texts sends about 2n-1 requests to each node before the run fails, and a batch of 128 texts sends about 255 requests for one answer.
  • Halving helps only when one text of the batch causes the error (for example an input that is too long).

Proposed remedy

  • Halve a batch only on an error that one text can cause: HTTP 400, 413 or 422 (the classes that PR fix(ai): rag index skips data fixtures and survives a failed batch #6719 adds to EmbeddingsError as status). On any other error (401, 403, 404, 5xx, a timeout or a connect error), stop the halving and raise the error at once, so the retry and the breaker of the indexer decide.
  • Write the tests first: an Ollama fake that answers 404 to each request gets one request for the batch; a fake that refuses one long text with 400 still gets the halving and gives the vectors of the other texts.

Acceptance criteria

  • The new tests pass with uv run devops ci test on the embeddings tests.
  • For a batch of 128 texts and a node that answers 404, the fake records 1 request, not about 255.

Dependencies

Alternatives check of 2026-11-22: none.
Tool: none found.
Reason: Searches found only general HTTP status references (400, 413, 422, 404) and no library that gates embedding batch halving on error class; the fix changes the project's own _embed_batch_with_subdivision, so no tool covers it.

Lacks:

  • Halving only on 400, 413 or 422 statuses
  • No halving on 401, 403, 404, 5xx, timeout, connect
  • Fake-based tests for one request on 404 and halving on 400

Proposed design

Problem statement

Halve an embedding batch only on an error that one text can cause or that a smaller batch can fix. With direct Ollama nodes, _embed_batch_with_subdivision halves a batch after any EmbeddingsError. A permanent error such as HTTP 404 gives the same reply for each half, so a batch of 128 texts sends about 255 requests to each node before the run fails. The change halves a batch on HTTP 400, 413 and 422, on a timeout, and on a malformed reply. It raises 401, 403, 404, 5xx and a connect error at once.

Acceptance criteria

Requirement: halve_on_input_refused_status

The embedding engine SHALL halve a batch and retry each half when EmbeddingsError.status is 400, 413 or 422.

Scenario: one_long_text_is_refused

  • WHEN a fake Ollama node refuses one text of a batch with HTTP 400 and answers each other text
  • THEN the engine halves the batch and returns the vectors of the other texts

Requirement: no_halve_on_not_found

The embedding engine MUST raise an EmbeddingsError with status 404 at once and MUST NOT halve the batch.

Scenario: model_not_found

  • WHEN a fake Ollama node answers HTTP 404 to each embeddings request for a batch of 128 texts
  • THEN the fake records 1 request, and the engine raises the EmbeddingsError

Requirement: no_halve_on_auth_error

The embedding engine MUST raise an EmbeddingsError with status 401 or 403 at once and MUST NOT halve the batch.

Scenario: node_refuses_the_key

  • WHEN a fake Ollama node answers HTTP 401 to the first embeddings request for a batch
  • THEN the fake records 1 request, and the engine raises the EmbeddingsError

Requirement: no_halve_on_server_error

The embedding engine MUST raise an EmbeddingsError with a 5xx status at once and MUST NOT halve the batch.

Scenario: node_answers_503

  • WHEN a fake Ollama node answers HTTP 503 to the first embeddings request for a batch
  • THEN the fake records 1 request, and the engine raises the EmbeddingsError

Requirement: no_halve_on_connect_error

The embedding engine MUST raise an EmbeddingsError on a connect error at once and MUST NOT halve the batch.

Scenario: node_refuses_the_connection

  • WHEN a fake Ollama node raises a connect error on the first embeddings request for a batch
  • THEN the engine makes 1 attempt, and it raises the EmbeddingsError

Requirement: keep_halving_on_timeout

The embedding engine SHALL halve a batch and retry each half when a request to a direct Ollama node times out.

Scenario: timeout_on_a_parallel_batch

  • WHEN a fake Ollama node times out on a 4-text batch and answers each half
  • THEN the engine sends the batch as [4, 4, 2, 2, 2, 2] and returns the vectors

Requirement: keep_halving_on_malformed_reply

The embedding engine SHALL halve a batch and retry each half when a 200 reply gives fewer vectors than texts.

Scenario: fewer_vectors_than_texts

  • WHEN a fake Ollama node answers 200 OK with one vector for a 2-text batch
  • THEN the engine halves the batch and returns the vectors of each text

Requirement: no_halve_on_unknown_error

The embedding engine MUST raise an EmbeddingsError with no status at once and MUST NOT halve the batch when the error is not a malformed reply.

Scenario: unknown_error

  • WHEN a fake Ollama node raises an EmbeddingsError with no status and a message that is not a vector count mismatch
  • THEN the engine makes 1 attempt, and it raises the EmbeddingsError

Requirement: no_halve_across_nodes

The embedding engine MUST try each direct Ollama node once for a batch and MUST NOT halve the batch when a node answers 404 and the next node raises a connect error.

Scenario: two_nodes_fail

  • WHEN a fake node answers HTTP 404 and a second fake node raises a connect error for a 2-text batch
  • THEN the fake records 2 requests, and the engine raises the EmbeddingsError

Requirement: embedding_tests_pass

The embeddings test suite SHALL pass with the new tests.

Scenario: run_the_embeddings_tests

  • WHEN the embeddings test suite runs with uv run devops ci test
  • THEN the command exits 0 and reports the new tests as passed

Existing Tool Considered

The alternatives check of 2026-11-22 in the issue body gives the verdict none and names no tool. The change therefore edits the project's own _embed_batch_with_subdivision (src/devops_cli/ai/rag/embeddings.py:1130) to gate halving on EmbeddingsError.status and the existing timeout and malformed-reply conditions. Written cost: an estimated 15 000 tokens to build, keep and fix the status gate and its tests, and about 2 hours of elapsed time.

Key questions

  • Which errors allow the engine to halve a batch?: Halve on HTTP 400, 413 and 422, on a timeout, and on a 200 reply with fewer vectors than texts. Raise 401, 403, 404, 5xx and a connect error at once. (best practice) [sources: src/devops_cli/ai/rag/embeddings.py:1130, tests/ai/rag/test_embedding_batching.py:239-263, tests/ai/rag/test_embedding_provider_failures.py:316-324, openspec/specs/embedding-batch-size/spec.md:22-24]
    • Chosen: Keep timeout and malformed-reply halving; stop on permanent HTTP errors and connect errors. The existing spec and tests require timeout and malformed-reply halving. Permanent errors and connect errors apply to the whole request, so halving repeats the error.
    • Rejected: Stop halving on timeout. It breaks test_parallel_batches_that_time_out_on_an_ollama_node_halve_the_batch_size_once and removes adaptive batching for a large batch.
    • Rejected: Stop halving on a malformed reply. It breaks test_an_ollama_node_that_gives_fewer_vectors_than_texts_gets_the_batch_cut.
    • Rejected: Halve on any 4xx. 401, 403 and 404 apply to the whole request, so halving repeats the same error.
    • Rejected: Halve on any EmbeddingsError. A permanent error such as 404 gives the same reply for each half, so one batch of n texts sends about 2n-1 requests.
  • What does the engine do on an error that does not allow halving?: It raises the EmbeddingsError at once. The retry and the breaker of the indexer then decide. (best practice) [sources: src/devops_cli/ai/rag/indexer.py, src/devops_cli/ai/rag/embeddings.py:1130]
    • Chosen: Raise at once and let the retry and the breaker of the indexer decide. The indexer already holds the retry and the breaker, so the engine adds no second policy.
    • Rejected: Retry the same batch in the engine. It duplicates the retry of the indexer.
    • Rejected: Halve on 5xx. A 5xx applies to the whole request, so each half gets the same error and the request count grows.
  • Which existing tests must change?: test_ollama_raises_after_every_node_fails in tests/ai/rag/test_embedding_provider_failures.py must assert 2 requests, not 4. The timeout and malformed-reply tests stay as they are. (best practice) [sources: tests/ai/rag/test_embedding_provider_failures.py:76-105, tests/ai/rag/test_embedding_batching.py:239-263, tests/ai/rag/test_embedding_provider_failures.py:316-324]
    • Chosen: Update the multi-node test to assert 2 requests. The engine no longer halves on 404, so it sends one request to each node.
    • Rejected: Keep the test at 4 requests and halve on 404. This keeps the request explosion that the issue fixes.
    • Rejected: Update the timeout and malformed-reply tests. The change keeps their behavior.
  • Where do the new tests go?: Add the 404 and 400 tests to tests/ai/rag/test_embedding_batching.py, with the fake in tests/support/fakes/embeddings.py. (best practice) [sources: tests/ai/rag/test_embedding_batching.py, tests/support/fakes/embeddings.py]
    • Chosen: Add to tests/ai/rag/test_embedding_batching.py. This file already holds the tests of EmbeddingsError batching.
    • Rejected: Add a new test file. The existing file already covers the batching of EmbeddingsError, so a new file splits the same subject.
  • Where does the engine read the HTTP status of an EmbeddingsError?: From EmbeddingsError.status, which fix(ai): rag index skips data fixtures and survives a failed batch #6717 adds to the class at src/devops_cli/ai/rag/embeddings.py:173. [sources: src/devops_cli/ai/rag/embeddings.py:173]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    scope/ai
    source/agent
    on Nov 22, 2026
  2. dan-petty commented on Nov 22, 2026

    @dan-petty
    OwnerAuthor
  3. added
    status/new
    value/medium
    effort/low
    priority/p2-medium
    on Nov 22, 2026
  4. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (5)

    Searches run:

    • langchain embeddings batch size reduce on error split texts, results: 5
    • ollama embeddings API batch request error handling library, results: 5
    • "EmbeddingsError" ollama embeddings 404 status code, results: 5
    • openai python embeddings batch split input too long 400 retry, results: 5
    • adaptive batching embeddings library bisect batch on 400 413 422 status, results: 5

    Sources:

  5. added
    status/ready
    and removed
    status/new
    on Nov 22, 2026
  6. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  7. dan-petty commented on Nov 25, 2026

    @dan-petty
    OwnerAuthor

    Cannot build as written on origin/main (b42683d4b): #6722 requires _embed_batch_with_subdivision (src/devops_cli/ai/rag/embeddings.py:1130-1143) to halve a batch only when EmbeddingsError.status is 400, 413, or 422 and raise every other error (401, 403, 404, 5xx, timeout, connect error, or status-less error) at once, which conflicts with existing requirements and tests:

    1. Direct Ollama timeout halving (openspec/specs/embedding-batch-size/spec.md:22-24 and tests/ai/rag/test_embedding_batching.py:239-263): test_parallel_batches_that_time_out_on_an_ollama_node_halve_the_batch_size_once explicitly specifies that a timed-out 4-text batch on a direct Ollama node is halved and retried ([4, 4, 2, 2, 2, 2]), whereas #6722 (no_halve_on_connect_error) prohibits halving on a timeout.
    2. Malformed Ollama reply halving (tests/ai/rag/test_embedding_provider_failures.py:316-324): test_an_ollama_node_that_gives_fewer_vectors_than_texts_gets_the_batch_cut expects _embed_batch_with_subdivision to halve a 2-text batch when 200 OK returns only 1 vector (EmbeddingsError with status=None).
    3. Multi-node failure test (tests/ai/rag/test_embedding_provider_failures.py:76-105): test_ollama_raises_after_every_node_fails sends HTTP 404 on one node and ConnectError on the second and asserts len(posts) == 4 (halving the 2-text batch once).

    Returning to New per AGENTS.md Rule 2 so refinement can align the allowed halving classes (for instance, keeping 400/413/422, malformed counts, and/or timeouts while stopping non-splittable 401/403/404/5xx and ConnectError, or updating openspec/specs/embedding-batch-size/spec.md and the affected tests).

  8. added
    status/new
    and removed
    status/ready
    on Nov 25, 2026
  9. added
    status/ready
    and removed
    status/new
    on Nov 25, 2026
  10. devops-cli-service commented on Nov 25, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  11. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 25, 2026
  12. removed
    status/in-review
    owner/agent
    on Nov 25, 2026
  13. devops-cli-service commented on Nov 25, 2026

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

    @devops-cli-service
    Contributor

    Live since 2026-11-25 17:09 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