Repository navigation
fix(sagemaker,bedrock): answer 502 when a 200 AWS event stream decodes to no events or ends mid-frame - #53482
Merged
mateo-berri merged 6 commits intoNov 22, 2026
Conversation
…s to no events or ends mid-frame SageMaker streaming (sagemaker and sagemaker_chat) and Bedrock invoke-agent turned an HTTP 200 whose event-stream body decoded to no events, or ended with bytes that never formed a whole frame, into an empty successful turn. They now raise a 502 naming the content type, the x-amzn-requestid, and the byte count, through the same tally the Bedrock converse and invoke decoders use, so router retries and fallbacks apply. Invoke-agent exception frames now raise with the status the bedrock-agent-runtime model gives them instead of being dropped, and error frames keep their error code and message.
Contributor
Contributor
6 of 8 tasks
Contributor
[Medium impact] The PR appears safe to merge, with the previous test-convention issue fixed and no new blocking findings.
|
…t error frames The no-events 502 now names the content-type, request id and byte count only, since a cut frame can hold model text that skips message redaction. Untyped error frames on Bedrock agents answer 500 so the router retries them, and Bedrock chat keeps its body-only error text for those frames, as on main
Contributor
… is not an AWS event stream
Contributor
…oding The Rust Messages route now ends a 200 Bedrock stream whose body is not an AWS event stream, or turns corrupt after some events, with the same 502 Python's decoder gives, naming the botocore parser error class instead of a bare checksum mismatch.
Contributor
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…s, not message text
Contributor
Contributor
|
bugbot run |
Contributor
There was a problem hiding this comment.
✅ 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 8d98527. Configure here.
This was referenced Nov 23, 2026
4 of 6 tasks
3 of 6 tasks
narmstrong-berri
added a commit
that referenced
this pull request
Nov 24, 2026
…our (#54223) Main's rust-wheel job fails 10 tests in tests/test_litellm_rust/messages, each pinned to behaviour a later PR changed on purpose. The stub check failed first until #53922, so the pytest step never reported them. Credential failures on Bedrock invoke and Mantle now answer a 401 AuthenticationError with the botocore error as its context (#53362). A 200 Bedrock stream whose body is JSON answers BedrockError 502 (#53482). A cut Bedrock stream drops the message_start output placeholder and re-counts the streamed text (#53394). Logging copies the resolved provider into litellm_params before callbacks run (#52709)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Handwritten 1-3 sentence description
When SageMaker or a Bedrock agent answers HTTP 200 but the stream holds no usable events, or its last event is cut short, the proxy now returns a 502 that names the AWS request instead of an empty reply that looks successful. Retries and fallbacks now kick in, as they have for Bedrock chat streams since #43213
Details
TLDR
Problem this solves:
How it solves it:
Bedrock chat streams got this fix in #43213. A paired PR brings it to lts/1.108.x
User Flow
Before: an app streaming from a
sagemaker/,sagemaker_chat/orbedrock/agent/model through the proxy gets a blank reply marked finished when AWS answers 200 with no events"stream": truefor that modelfinish_reason: "stop"and no text, then[DONE], as HTTP 200After: the same request fails fast with a 502 naming the AWS request, so the fallback answers instead
"stream": truefor that modelBadGatewayErrornaming the content-type,x-amzn-requestidand bytes receivedPre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
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 moreDelays 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
A local stand-in serves the broken AWS 200s, and live rows call real SageMaker and Bedrock
Every row ran at 8d98527, with 3115254 as the before leg
Setup
Both legs run the proxy with 2 workers from their own tree, the same config.yaml and the same live SageMaker endpoint (TGI 3.3.6, Qwen2.5-0.5B-Instruct, us-east-1). AWS will not send an empty or cut 200 on demand, so aws_upstream.py serves them on 127.0.0.1:20431 in real AWS event-stream framing. Agent cases are scripted, since AWS refuses new Bedrock agents for our account (Maintenance Mode). Live Bedrock rows use Claude Haiku 5.5
python aws_upstream.py 20431python litellm/proxy/proxy_cli.py --config config.yaml --port <port> --num_workers 2zsh cases.sh <port> <case>...,zsh matrix.sh <port>andzsh rust-cases.sh <port>from the gist, each printing the command it runs and then its outputzsh direct.shsends the matrix prompts straight to AWSsagemaker/stream, empty bodysagemaker/, first event cutsagemaker/, last event cutsagemaker_chat/stream, empty bodysagemaker_chat/, last event cutbedrock/agent/, empty bodysagemaker/stream, HTML pagesagemaker_chat/stream, JSON bodybedrock/stream, HTML page/v1/messagesstream, empty body/v1/messages, agent cut/v1/responsesstream, empty body/v1/messagesstream, body not an event streamBefore (3115254)
sm-tgi-empty
After (8d98527)
sm-tgi-empty
Raw output for every case is in the gist
sm-tgi-truncated
Before (3115254)
After (8d98527)
sm-tgi-midstream
Before (3115254)
After (8d98527)
sm-chat-empty
Before (3115254)
After (8d98527)
sm-chat-midstream
Before (3115254)
After (8d98527)
agent-empty
Before (3115254)
After (8d98527)
agent-truncated
Before (3115254)
After (8d98527)
agent-midstream
Before (3115254)
After (8d98527)
agent-empty-stream
Before (3115254)
After (8d98527)
agent-throttled
Before (3115254)
After (8d98527)
agent-error-frame
Before (3115254)
After (8d98527)
sm-tgi-html
Before (3115254)
After (8d98527)
sm-chat-json
Before (3115254)
After (8d98527)
agent-html
Before (3115254)
After (8d98527)
agent-zeros
Before (3115254)
After (8d98527)
bedrock-html
Before (3115254)
After (8d98527)
agent-trace-ok
Before (3115254)
After (8d98527)
agent-trace-ok-stream
Before (3115254)
After (8d98527)
messages-sm-tgi-empty
Before (3115254)
After (8d98527)
messages-agent-midstream-drop
Before (3115254)
After (8d98527)
responses-sm-chat-empty
Before (3115254)
The output is long, so it is in the gist under
=== responses-sm-chat-emptyAfter (8d98527)
fallback
Before (3115254)
After (8d98527)
The output is long, so it is in the gist under
=== fallbackmessages-bedrock-html
Before (3115254)
After (8d98527)
The JSON and zero-byte rows are in the gist (before, after)
Surprises
/v1/messagesto agents 400s onmax_tokenswithoutdrop_params, unchanged/live-pr-risk at 8d98527, no breaking change found
Base 3115254 and head 8d98527 each ran 2 workers with their own Postgres and Redis. Live cells called real Bedrock and SageMaker through a recorder that logs each AWS request and forwards it, and broken-200 cells used a local AWS stand-in. Every table, body and recorded request is in the risk report
Breaking
None found. The 12 live cells (Bedrock converse and invoke on
/v1/chat/completions,/v1/messagesand/v1/responses, SageMaker and SageMaker chat, stream and non-stream) match on status, body shape, header names, and the AWS requests sentBackward incompatible
Each change below is the fix or follows from it, and the Decisions block settles it
A 200 with no usable event stream answers 502, or 429 for agent throttling, instead of an empty 200. Same as #43213 for Bedrock chat
The router retries that 502, so the client waits about 4.5 to 6 s and AWS gets 3 calls, where base made 1 call and answered in under 0.4 s. Base already retried a 500 the same way
A multi-deployment group cools the broken deployment down, so
sm-poolrequests 2 to 4 went to the healthy deployment on head and to the broken one on base. A single-deployment group with no fallback answered 502 three times in a row, with no lockoutFallbacks now run, since the error no longer looks like a success
Spend logs write a failure row with
error_code502 or 429. Base wrote no row for an empty SageMaker stream and a 0-token success row for an empty agent turn. Prometheus failure labels move fromChecksumMismatchor none toSagemakerErrorandBedrockErrorwith status 502Regression risk
Non-Claude Bedrock invoke families share the decoder, and only Claude ran live over invoke, so their streams rest on the decoder's unit tests. With
LITELLM_RUST_FIRSTset, a Rust gateway serves Bedrock invoke streams through the Rust decoder this PR also changes. That path was traced and not run liveDependency graph
Not verified
A live Bedrock agent, since the QA AWS account has no agent and refuses CreateAgent. The Rust gateway path, as above
Merged with main b21e530 (c6dcb773a6), the tree merges clean and matches head on every cell. The lts/1.108.x twin #53483 (c50d78d vs 2b4abd7) shows the same changes on the same cells and nothing else. The integration groups that reach the changed modules passed at the tip, except 2 of 633 providers tests that hit timeouts on a loaded host and passed when run alone on both tip and base
Type
🐛 Bug Fix
Caveats (if any)
Medium
Low
SagemakerChatHandlerstill builds its decoder without response headersDecisions
A broken 200 answers 502
BadGatewayError, the same as #43213 for Bedrock chat. The other options were a 500, which reads as a proxy bug, or keeping the empty 200, which is the bug. A 502 lets router retries and fallbacks run. Mid-stream, the client still gets every complete chunk first and then the 502 as a stream error, so text already sent is never thrown awayA stream counts as broken when no event decodes, bytes are left over after the last complete event, or the body stops decoding as an AWS event stream. Bedrock chat also treats a stream of only unknown event types as broken, unchanged from #43213. SageMaker and agents skip that check, since any decoded event there carries real data or an AWS exception
These errors name the content-type,
x-amzn-requestidand bytes received, with no sample of the body. A cut frame can hold model text, and error text is not covered by message redaction, so a sample could copy a reply into logs and alerts. This drops the 200-byte sample Bedrock chat's no-events error has carried since #43213. The other option was keeping that sample to tell an HTML error page from a broken frame, and the content-type now does that. Bedrock chat's error for a stream of only unknown event types keeps its payload sample, unchanged from #43213, because it shows an AWS rejection message litellm does not model yet. The AWS request id also goes back as a response header, which is enough to find the call in AWSA 200 body that is not an AWS event stream at all, such as an HTML or JSON page from something in front of AWS, used to fail with a 500
Checksum mismatchthat named neither the request nor the content-type. It now answers the same 502 after any complete events and names the decoder's error class. The decoder's own message is left out, since it quotes 4 bytes of the bodyBedrock agent exception events used to be dropped, which turned a throttled call into an empty 200. They now raise with the AWS status, so a
throttlingExceptionanswers 429. Without this, a stream holding only an exception event would still pass the new checkAn agent error frame that names no exception type used to be dropped too, so the turn ended early with 200 and cut text. It now answers 500 with its
:error-codeand:error-messageas the text, which maps to a 503 the router retries, and a code likeThrottlingExceptionmaps to 429 through the existing message match. AWS sends client errors as typed exceptions, so an untyped frame is treated as a server fault. The other option was a 400 from the frame's missing status, which the router never retries. Bedrock chat keeps its body-only text for these frames, unchanged from mainThe router retries these 502, 503 and 429 errors like any other, so a failed agent turn runs again, up to
num_retriestimes, and its action groups can run again too. Main already does this when an agent call fails with HTTP 500 or 429 (3 calls on both legs), so agents keep one retry rule for both. The other options were no retries forbedrock/agent/, a status the router never retries, or the AWS SDK's rule of no retry once a stream started. Each makes a mid-stream failure harder to recover from than the same failure at the HTTP level, and OpenAI SDK clients retry 5xx and 429 on their own anyway. A deployment that must never repeat a turn setsDefaultRetries: 0for that model groupThese 502s also count toward
allowed_failsand cool the deployment down, like any provider 502. That now covers an empty SageMaker or agent 200, which used to count as a success, and an HTML or JSON 200, which failed asAPIConnectionError(a rawChecksumMismatchon Rust/v1/messages) and skipped cooldown. A deployment that answers 200 with no usable event stream fails every request, so routing around it is the point, and Bedrock chat's empty-body 502 from #43213 already cools down. The other option was an error the cooldown skips, which keeps sending traffic to the broken deployment. A model group that should tolerate more of these before cooling down raisesBadGatewayErrorAllowedFailsinallowed_fails_policyAll three readers share one counter, moved out of the Bedrock chat handler, so they judge a stream the same way
Bugbot summary