Skip to content

fix(responses): retry a bridged stream failing before its first chunk - #45076

Draft
Aasif-Simpplr wants to merge 1 commit into
BerriAI:mainfrom
Aasif-Simpplr:fix-responses-bridge-stream-retry
Draft

Aasif-Simpplr wants to merge 1 commit into
BerriAI:mainfrom
Aasif-Simpplr:fix-responses-bridge-stream-retry

Conversation

@Aasif-Simpplr

@Aasif-Simpplr Aasif-Simpplr commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • /v1/responses streams to Vertex AI or Gemini are never retried
  • A 503 before the first chunk ends the stream as response.failed
  • num_retries and fallbacks never run for it

How it solves it:

Intentional product change: when every attempt fails before the first chunk, the client gets an HTTP 503 error instead of a 200 stream carrying response.failed, matching providers whose streams are not deferred

User Flow

Before: a developer streaming /v1/responses from a Vertex AI Gemini model gets a failed response on one transient 503

  1. The proxy admin sets router_settings.num_retries: 2 for a vertex_ai/gemini-3.8-flash deployment
  2. The developer sends POST http://localhost:4000/v1/responses with "stream": true
  3. Vertex AI answers the opening request with 503 UNAVAILABLE
  4. The stream ends with response.failed, and Vertex AI saw only that one request

After: the same request is retried and streams the answer

  1. The proxy admin sets router_settings.num_retries: 2 for a vertex_ai/gemini-3.8-flash deployment
  2. The developer sends POST http://localhost:4000/v1/responses with "stream": true
  3. Vertex AI answers the opening request with 503 UNAVAILABLE
  4. The proxy retries, and the stream completes with the model's text

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Screenshots / Proof of Fix

Real Vertex AI calls (vertex_ai/gemini-3.8-flash, location global) through a local proxy. The deployment's api_base points at a small forwarder on 127.0.0.1:8099 that answers the first N streamGenerateContent requests with Google's own 503 body and forwards every other request unchanged to https://aiplatform.googleapis.com.

model_list:
  - model_name: gemini-3.8-flash
    litellm_params:
      model: vertex_ai/gemini-3.8-flash
      vertex_project: <gcp-project>
      vertex_location: global
      vertex_credentials: <service-account.json>
      api_base: http://127.0.0.1:8099
router_settings:
  num_retries: 2
curl -sN http://localhost:4000/v1/responses \
  -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" \
  -d '{"model": "gemini-3.8-flash", "input": "Reply with exactly: pong", "stream": true}'

Before (736ff14)

One 503 before the first chunk

  1. Start the forwarder failing 1 request, start the proxy, run the curl above
  2. The stream fails without a retry:
    event: response.failed
    data: {"type":"response.failed","response":{...,"error":{"code":"server_error","message":"litellm.ServiceUnavailableError: Vertex_ai_betaException - b'{\"error\": {\"code\": 503, \"message\": \"The service is currently unavailable.\", \"status\": \"UNAVAILABLE\"}}'"},"output":[],"status":"failed"},"sequence_number":0}
    
  3. Forwarder log: stream request #1: injected 503, and no second request

Every attempt fails before the first chunk

  1. Start the forwarder failing 3 requests, start the proxy, run the curl above
  2. Same response.failed event as above
  3. Forwarder log: stream request #1: injected 503, and no second request

After (18d09d2)

One 503 before the first chunk

  1. Start the forwarder failing 1 request, start the proxy, run the curl above
  2. The stream completes with the model's answer:
    response.created, response.in_progress, response.output_item.added, response.content_part.added,
    response.output_text.delta ("delta":"pong"), response.output_text.done, response.content_part.done,
    response.output_item.done, response.completed
    
  3. Forwarder log: stream request #1: injected 503, then stream request #2: forwarded to https://aiplatform.googleapis.com

Every attempt fails before the first chunk

  1. Start the forwarder failing 3 requests, start the proxy, run the curl above
  2. After 1 attempt and 2 retries the proxy returns the provider error:
    {"error":{"message":"litellm.ServiceUnavailableError: Vertex_aiException - b'{\"error\": {\"code\": 503, \"message\": \"The service is currently unavailable.\", \"status\": \"UNAVAILABLE\"}}'\n\nLiteLLM: model group 'gemini-3.8-flash' failed with the error above. No fallback was attempted.","type":"internal_server_error","param":null,"code":"503"}}
    
  3. Forwarder log: stream request #1, #2, #3: injected 503

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Only the async bridge changes; the sync litellm.responses stream path is untouched

Low

  • An error on the opening request now returns an HTTP error, not a response.failed event
    • Errors after the first chunk keep today's mid-stream handling

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

The completion bridge returned a deferred provider stream (Vertex AI, Gemini) unopened, so an error on its opening request, e.g. a 503, raised on the first iteration after the Router had already returned. No retry or fallback ran for it. Open the stream in async_response_api_handler, matching what BerriAI#44276 does for chat completions in Router._acompletion.
@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing Aasif-Simpplr:fix-responses-bridge-stream-retry (18d09d2) with main (736ff14)

Open in CodSpeed

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.

1 participant