Skip to content

FIX: Retry only rate limits in OpenAITextEmbedding and raise the original error - #4834

Open
Swaraj Unde (swaraj-unde) wants to merge 1 commit into
microsoft:mainfrom
swaraj-unde:fix/embedding-retry-reraise
Open

Swaraj Unde (swaraj-unde) wants to merge 1 commit into
microsoft:mainfrom
swaraj-unde:fix/embedding-retry-reraise

Conversation

@swaraj-unde

Copy link
Copy Markdown

Description

OpenAITextEmbedding.generate_text_embedding_async retried every exception for 3 seconds (about 29 calls) and then raised tenacity.RetryError, hiding the real error. This switches it to the shared pyrit_target_retry so permanent errors fail on the first call with their original exception. Fixes #4833.

  • Replace the bare @tenacity.retry(wait_fixed(0.1), stop_after_delay(3)) with @pyrit_target_retry, as the OpenAI targets use, and drop the tenacity import.
  • 401/400/404/500 and non-API errors now surface the original exception after one call; a persistent 429 raises openai.RateLimitError after the configured attempts instead of RetryError.
  • Add tests/unit/embedding/test_openai_text_embedding.py; six of its seven tests fail on the old decorator.

Behavior changes to be aware of:

  • Callers now see the original openai exception instead of tenacity.RetryError. Nothing in pyrit catches RetryError from embeddings.
  • 5xx and connection errors are no longer retried by the outer decorator, only by the OpenAI SDK's own retries (previously roughly 69 requests on a persistent 500, now 3).
  • A persistent 429 now follows the shared retry settings (default 10 attempts, 5-220s backoff) instead of a 3s cap, so an embedding call, and the memory write that awaits it, can wait for minutes. If you would rather keep a short 429-only budget for embeddings, I can add a local retry instead.

…inal error

generate_text_embedding_async used a bare tenacity.retry with no retry
predicate and no reraise, so every error (401, 400, 404, programming
errors) was retried for 3 seconds and surfaced as tenacity.RetryError.
Use the shared pyrit_target_retry, which retries only rate limits with
exponential backoff and re-raises the original exception.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 12:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the shared retry-policy change.

No open issues 🎉

What changed in this PR

Addresses #4833 by replacing embedding-specific retries with PyRIT’s shared retry policy, preserving original exceptions.

Changes:

  • Non-rate-limit errors now propagate without outer retries.
  • Rate limits use shared backoff and attempt settings.
  • Adds regression tests for error propagation and rate-limit recovery.
File Description
tests/​unit/​embedding/​test_openai_text_embedding.py Tests immediate failures, retry recovery, and retry exhaustion.
pyrit/​embedding/​openai_text_embedding.py Replaces the broad Tenacity decorator with pyrit_target_retry.

Review effort: Balanced

@swaraj-unde

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch has not been deployed

No deployments
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.

BUG: OpenAITextEmbedding retries every error and raises tenacity.RetryError instead of the real exception

2 participants