Skip to content

fix(router): honor stream_timeout on the SDK-native passthrough route (/v1/messages, /converse) - #41875

Merged
mateo-berri merged 7 commits into
mainfrom
litellm_passthrough_stream_timeout
Sep 18, 2026
Merged

mateo-berri merged 7 commits into
mainfrom
litellm_passthrough_stream_timeout

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Replaces #41206 (fork head 74e9fb2, opened by @clonylu) on a litellm_ branch so CircleCI runs on it; the commits and their authors are carried over unchanged

TLDR

Problem this solves:

  • stream_timeout is ignored on /v1/messages (and Bedrock /converse)
  • those routes resolve timeouts separately from /chat/completions, which honors it
  • an idle stream is cut at the 600s default as a clean, empty 200

How it solves it:

  • the passthrough timeout resolver checks stream_timeout first for streaming calls
  • same precedence as Router._get_stream_timeout: kwargs -> deployment -> router
  • the stream flag is read by truthiness, like Router._get_timeout, so "stream": null still resolves
  • only the winning value is validated as seconds, so a non-numeric value in a lower-precedence field never fails the call
  • litellm_settings.request_timeout ranks on this route exactly where /chat/completions ranks it: above router_settings.timeout, and below stream_timeout for streams. The alternative, leaving this route on router_settings.timeout alone, keeps /v1/messages and /chat/completions on different timeouts whenever both settings are configured, which is the split this PR exists to close
  • non-streaming resolution otherwise keeps its precedence (timeout / request_timeout / pass_through_request_timeout / 600s)

User Flow

Before: a proxy admin sets stream_timeout: 1800 on a Claude deployment; long thinking turns over /v1/messages still die at 600s

  1. Admin sets stream_timeout: 1800 under the deployment's litellm_params (or router_settings.stream_timeout: 1800) and restarts the proxy
  2. A Claude Code user sends POST https://litellm-domain/v1/messages with "stream": true, max_tokens: 64000, adaptive thinking; the provider sends message_start and then nothing for ~14 minutes while it thinks
  3. At 600s the stream ends: the client receives message_start, pings, then a clean close with stop_reason: "stop" and ~2 output tokens; HTTP status is 200
  4. https://litellm-domain/ui/?page=logs shows the request as a success with a 600s duration and near-zero output

After: the same deployment setting is respected on /v1/messages

  1. Admin sets stream_timeout: 1800 under the deployment's litellm_params (or router_settings.stream_timeout: 1800) and restarts the proxy
  2. The user sends the same POST https://litellm-domain/v1/messages with "stream": true
  3. The stream stays open through the ~14 minutes of provider silence and delivers the full response (stop_reason: "max_tokens", 64000 output tokens)
  4. https://litellm-domain/ui/?page=logs shows the request as a success with the real output token count

Non-streaming /v1/messages calls on the same deployment keep the previous behaviour (timeout / request_timeout / pass_through_request_timeout / 600s)

Relevant issues

Router._update_kwargs_with_deployment routes _ageneric_api_call_with_fallbacks (anthropic /v1/messages, bedrock /converse) through resolve_llm_passthrough_timeout, which only reads timeout / request_timeout and never stream_timeout, so the documented stream_timeout knob (deployment or router_settings) has no effect on those routes. /chat/completions goes through Router._get_timeout and honors it

Affected release

Linear ticket

Resolves LIT-7847

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally: uv run pytest tests/test_litellm/proxy/pass_through_endpoints/test_pass_through_endpoints.py tests/test_litellm/test_router.py -k timeout -v (28 passed; the new streaming, winner-only validation, and request_timeout precedence cases fail on main without the fix)
  • My PR passes all required CI/CD checks (make check clean at the tip)
  • 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

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

QA run at e75ad61, the PR tip

Both legs boot the proxy from the repo checkout with --num_workers 2, no DB, real Anthropic and OpenAI keys, the same config, the same client, and the same request bodies. Before is the merge base 15bd8b0, After is e75ad61

Timeouts are scaled so a run takes seconds instead of minutes: pass_through_request_timeout: 1 is the generic timeout the bug falls back to and router_settings.stream_timeout: 600 is the setting the bug ignores. The provider's time to first byte on a large prompt (about 2s for claude-sonnet-5 on ~93k tokens, about 3.7s for gpt-5.6 at reasoning effort high on ~198k tokens) stands in for the minutes of mid-stream silence in the User Flow, since both are the same per-read idle timeout on the same socket

model_list:
  - model_name: claude-sonnet-5
    litellm_params:
      model: anthropic/claude-sonnet-5
      api_key: os.environ/ANTHROPIC_API_KEY
  - model_name: claude-*
    litellm_params:
      model: anthropic/claude-*
      api_key: os.environ/ANTHROPIC_API_KEY
  - model_name: gpt-5.6
    litellm_params:
      model: openai/gpt-5.6
      api_key: os.environ/OPENAI_API_KEY
  - model_name: claude-short
    litellm_params:
      model: anthropic/claude-sonnet-5
      api_key: os.environ/ANTHROPIC_API_KEY
      timeout: 600
      stream_timeout: 1
router_settings:
  stream_timeout: 600
  num_retries: 0
litellm_settings:
  num_retries: 0
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  pass_through_request_timeout: 1

Boot (each leg on its own random port): python litellm/proxy/proxy_cli.py --config config.yaml --port $PORT --host 127.0.0.1 --num_workers 2

Claude Code over /v1/messages (the User Flow's client)

  1. export ANTHROPIC_AUTH_TOKEN=<proxy key> then ANTHROPIC_BASE_URL=http://127.0.0.1:$PORT ANTHROPIC_MODEL=claude-sonnet-5 claude
  2. Paste a 1901-line ledger (308 KB, ~93k tokens) whose last line is 01901 FINAL_LINE marker=lit7847-qa-sentinel, preceded by "Below is a ledger. Reply with only the marker value on its final line.", and press Enter

Before (15bd8b0): every streamed attempt dies at the 1s generic timeout although stream_timeout is 600; Claude Code retries 10 times over 4 minutes and gives up with the 408

pr41206-74e9fb2323-claude-before-final.png

After (e75ad61): the same prompt against the same config answers lit7847-qa-sentinel in 11s

pr41875-e75ad61fce-claude-after-answer.png

curl over every unified endpoint, plus the controls

One command per leg, run against each proxy in turn:

curl -sS -N -o $name.body -w 'http=%{http_code} ttfb=%{time_starttransfer}s total=%{time_total}s' \
  -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'content-type: application/json' \
  -d @$body "http://127.0.0.1:$PORT$route"
leg route body (ledger elided)
messages_stream POST /v1/messages {"model": "claude-sonnet-5", "stream": true, "max_tokens": 64, "messages": [{"role": "user", "content": "Below is a ledger. ...<ledger, 308 KB>"}]}
messages_nostream POST /v1/messages same with "stream": false (control)
chat_stream POST /v1/chat/completions same body as messages_stream (parity control, this route already honored stream_timeout)
responses_stream POST /v1/responses {"model": "gpt-5.6", "stream": true, "reasoning": {"effort": "high"}, "input": "Below is a ledger. ...<ledger x4, 1.2 MB>"}
messages_stream_null POST /v1/messages same as messages_stream with "stream": null (the endpoint treats it as non-streaming)
messages_stream_short POST /v1/messages same as messages_stream with "model": "claude-short" (deployment stream_timeout: 1, timeout: 600)
messages_nostream_short POST /v1/messages same as messages_stream_short with "stream": false (control)

Before (15bd8b0):

messages_stream          http=408 ttfb=3.097s total=3.097s  bytes=255
messages_nostream        http=408 ttfb=2.646s total=2.647s  bytes=255
chat_stream              http=200 ttfb=5.366s total=5.856s  bytes=819
responses_stream         http=408 ttfb=0.010s total=8.866s  bytes=282
messages_stream_null     http=408 ttfb=3.297s total=3.297s  bytes=255
messages_stream_short    http=408 ttfb=2.806s total=2.806s  bytes=252
messages_nostream_short  http=200 ttfb=4.202s              bytes=522

messages_stream body:
{"type":"error","error":{"type":"api_error","message":"litellm.Timeout: AnthropicException - litellm.Timeout: Connection timed out. Timeout passed=1.0, time taken=1.032 seconds. Received Model Group=claude-sonnet-5\nAvailable Model Group Fallbacks=None"}}
messages_nostream body: same error, time taken=1.026 seconds
chat_stream text: lit7847-qa-sentinel
responses_stream body:
{"error":{"message":"litellm.Timeout: Timeout Error: OpenAIException - litellm.Timeout: Connection timed out. Timeout passed=1.0, time taken=1.124 seconds. Received Model Group=gpt-5.6\nAvailable Model Group Fallbacks=None","type":"invalid_request_error","param":null,"code":"408"}}
messages_stream_null body: same 408 error as messages_stream, time taken=1.041 seconds
messages_stream_short body: same 408 error, Received Model Group=claude-short, time taken=1.036 seconds
messages_nostream_short text: lit7847-qa-sentinel (stop_reason end_turn, input_tokens 93188)

After (e75ad61):

messages_stream          http=200 ttfb=3.706s total=4.199s  bytes=1476
messages_nostream        http=408 ttfb=2.531s total=2.531s  bytes=255
chat_stream              http=200 ttfb=2.950s total=3.543s  bytes=819
responses_stream         http=200 ttfb=0.003s total=9.142s  bytes=7536
messages_stream_null     http=408 ttfb=2.639s total=2.642s  bytes=255
messages_stream_short    http=408 ttfb=2.600s total=2.600s  bytes=252
messages_nostream_short  http=200 ttfb=3.869s total=3.869s  bytes=522

messages_stream events: message_start, ping, content_block_start, content_block_delta x3, content_block_stop, message_delta, message_stop
messages_stream text: lit7847-qa-sentinel
messages_nostream body: same 408 error as before, time taken=1.003 seconds
chat_stream text: lit7847-qa-sentinel
responses_stream text: lit7847-qa-sentinel
messages_stream_null body: same 408 error as before, time taken=1.029 seconds
messages_stream_short body: same 408 error, Received Model Group=claude-short, time taken=1.033 seconds
messages_nostream_short text: lit7847-qa-sentinel (stop_reason end_turn, input_tokens 93188)

litellm_settings.request_timeout parity (the last commit)

Same rig and bodies, with the config changed so the only 1s timeout in play is litellm_settings.request_timeout: router_settings.timeout: 600 in place of stream_timeout: 600, litellm_settings.request_timeout: 1, and general_settings.pass_through_request_timeout: 600. The merge base lets streamed /v1/messages through on the router timeout while streamed /chat/completions times out on request_timeout; the tip times both out the same way

Before (15bd8b0):

messages_stream  http=200 ttfb=3.486s total=4.045s  bytes=1491
chat_stream      http=408 ttfb=2.357s total=2.357s  bytes=297

messages_stream text: lit7847-qa-sentinel
chat_stream body:
{"error":{"message":"litellm.Timeout: Connection timed out. Timeout passed=1.0, time taken=1.031 seconds\n\nDeployment Info: request_timeout: None\ntimeout: None. Received Model Group=claude-sonnet-5\nAvailable Model Group Fallbacks=None","type":"invalid_request_error","param":null,"code":"408"}}

After (e75ad61):

messages_stream  http=408 ttfb=2.777s total=2.778s  bytes=255
chat_stream      http=408 ttfb=2.463s total=2.463s  bytes=297

messages_stream body:
{"type":"error","error":{"type":"api_error","message":"litellm.Timeout: AnthropicException - litellm.Timeout: Connection timed out. Timeout passed=1.0, time taken=1.041 seconds. Received Model Group=claude-sonnet-5\nAvailable Model Group Fallbacks=None"}}
chat_stream body: same 408 as before, time taken=1.039 seconds

An intermediate tip (73fddb9) answered the "stream": null body with a 500 (1 validation error for _TimeoutFields ... Input should be a valid boolean ... input_value=None); the tip reads the flag by truthiness and returns the generic 408 timeout above, matching the endpoint's non-streaming treatment of a null flag

Observations from the run:

  • Streamed /v1/responses had the same bug; this PR fixes it too
  • Non-stream /v1/messages keeps the generic timeout, unchanged by this PR
  • "stream": null resolves as non-streaming: generic timeout, no 500
  • Deployment-level stream_timeout was already enforced on /v1/messages, both legs
  • Proxy adds ~2.5s on the 308 KB body, both legs alike
  • Provider passthrough /anthropic/v1/messages keeps pass_through_request_timeout, both legs alike

Author's own report, not reproduced here: on a real deployment (v1.101.0-rc.1 with this change applied as a shim) a 15 MB Claude Code request over /v1/messages with stream_timeout: 1800 survived 839s of Vertex silence and completed with 64000 output tokens, where the unpatched proxy closed the identical request at 600s as an empty 200

Type

🐛 Bug Fix

Caveats (if any)

Low

  • stream_timeout is a per-read idle timeout applied per attempt, same as on /chat/completions; this PR does not add a total-request bound
  • Raising stream_timeout on a deployment whose provider can go silent for long stretches holds the connection longer; that is the documented trade-off of the setting
  • A router or request whose stream_timeout is shorter than its generic timeout now sees it enforced on /v1/messages, /v1/responses, and the router's SDK passthrough streams, where it was silently ignored before. Deployment-level stream_timeout was already enforced by the messages handler, so those deployments see no change (the claude-short leg above behaves the same on both sides). Likewise litellm_settings.request_timeout now bounds /v1/messages calls the way it already bounded /chat/completions (the parity leg above): a deployment that relied on /v1/messages outliving a short request_timeout needs stream_timeout or a deployment-level timeout set instead
  • default_litellm_params.stream_timeout is now read for streaming passthrough calls, mirroring Router._get_stream_timeout, while default_litellm_params.timeout is still not read for non-streaming ones; that non-streaming chain is unchanged from the merge base and stays out of scope here
  • A non-numeric winning timeout value (an httpx.Timeout object in kwargs["timeout"], say) raises a pydantic ValidationError where the merge base raised TypeError from float(); both are unhandled, and a non-numeric value in a lower-precedence field is ignored, as it was before

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

  • e75ad61 passes /live-pr-risk

clonylu and others added 6 commits September 15, 2026 15:49
Anthropic /v1/messages and Bedrock /converse resolve their upstream timeout
through resolve_llm_passthrough_timeout, which only reads timeout /
request_timeout and then falls back to the 600s pass_through default. A
stream_timeout set on the deployment or in router_settings was never
consulted on that route, while /chat/completions honors it through
Router._get_stream_timeout.

For a streaming call the resolver now checks stream_timeout at each level
before the non-stream key (kwargs -> litellm_params -> router), mirroring
_get_stream_timeout; non-streaming resolution is unchanged. The router
passes its stream_timeout alongside the explicit timeout.
@devin-ai-integration

devin-ai-integration Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@codspeed

codspeed Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_passthrough_stream_timeout (e75ad61) with main (176ac4c)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with the prior timeout-precedence finding resolved and no new actionable defects identified

Summary

This PR makes SDK-native passthrough streams honor stream_timeout with the same precedence used by completion routes

  • Streaming calls resolve request, deployment, router, and generic timeout values in order
  • Non-streaming timeout behavior remains unchanged
  • The follow-up restores explicit request_timeout precedence and resolves the previous review finding
  • Regression tests cover precedence, truthy stream flags, and winner-only validation

Reviews (2) · Last reviewed commit: "fix(router): rank litellm_settings.reque..."

Comment thread litellm/router.py Resolved
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri mateo-berri added run-ci and removed run-ci labels Sep 18, 2026
@codecov

codecov Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit e75ad61. Configure here.

@mateo-berri mateo-berri 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.

LGTM

@mateo-berri
mateo-berri merged commit c553bc9 into main Sep 18, 2026
132 of 139 checks passed
@mateo-berri
mateo-berri deleted the litellm_passthrough_stream_timeout branch September 18, 2026 20:59
mateo-berri added a commit that referenced this pull request Oct 1, 2026
Merge origin/main into the branch. #41875 already honors stream_timeout on the bridged /v1/messages path and #43126 already ends the Anthropic stream with an error event, so this branch's router, timeout and bridge changes are dropped in favor of main. What remains is the Responses streaming iterator recording the text it already streamed on the failure log, so the spend row of a stream that died mid-way bills the partial completion tokens instead of zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants