Skip to content

fix(aws): raise when a Bedrock stream ends before messageStop - #8787

Open
ANDROIDHASSAN wants to merge 1 commit into
livekit:mainfrom
ANDROIDHASSAN:fix/aws-stream-end-before-message-stop
Open

ANDROIDHASSAN wants to merge 1 commit into
livekit:mainfrom
ANDROIDHASSAN:fix/aws-stream-end-before-message-stop

Conversation

@ANDROIDHASSAN

@ANDROIDHASSAN ANDROIDHASSAN commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

aws.LLMStream._run treats the end of response["stream"] as a finished reply. AWS documents messageStop as sent once per response, but nothing checks that it arrived. When the body ends early, aiobotocore stops iterating without an error, so:

  • a reply cut after some text is delivered as if complete;
  • an empty body counts as a successful turn and is not retried.

I ran the real aiobotocore 3.8.0 client against a local HTTP server that ends the event stream at different points:

Response body aiobotocore
ends cleanly between two events ends quietly, no messageStop
ends cleanly inside an event ends quietly, the partial event is dropped
ends cleanly with no events ends quietly
connection dropped mid-body raises ClientPayloadError (already handled)

#8705 fixed the same gap in the OpenAI Responses engine; #8728 is the open Anthropic counterpart.

Fix

  • Raise APIConnectionError if the stream ends before messageStop. It uses the existing retryable flag, so it is retried only while nothing has reached the caller.
  • Let that error pass through the generic except Exception instead of being wrapped again.
  • Reset the partial tool call at the start of each attempt. Without this, a retry after a stream cut inside a toolUse block emits the previous attempt's half-built call (arguments='{"a":') at the next contentBlockStop.

Tests

uv run pytest --unit tests/test_plugin_aws_llm.py -q gives 28 passed.

  • test_stream_ending_before_message_stop_is_an_error[no-output]: 3 attempts with max_retry=2, then raises.
  • test_stream_ending_before_message_stop_is_an_error[after-text]: 1 attempt, the text is kept, then raises.
  • test_retry_after_a_cut_tool_call_drops_its_partial_state: the retry yields "Hello" and no tool call.

With llm.py from main, all three fail: the first two with DID NOT RAISE APIConnectionError, the third with '' == 'Hello' because the cut stream is not retried. Removing only the state reset fails the third with the leaked FunctionToolCall.

Also ran:

  • ruff format --check and ruff check on both files: clean.
  • uv run pytest --unit --audio_eot -q: 7527 passed, 4 skipped. An earlier run on the same diff had one failure in test_plugin_zoom_ai_realtime.py::test_interrupted_request_zoom_never_answers_reconnects plus one error; that file then passed 5 of 5 runs on its own.
  • mypy (-p livekit.agents -p livekit.plugins.aws): one error, aws/stt.py:277, which my local environment also reports with llm.py from main.

Scope

aiobotocore ends iteration without an error when the event stream body ends
early, so a cut reply was accepted as complete and an empty one was not
retried. Raise APIConnectionError unless messageStop arrived, retryable only
while nothing was sent, and reset the partial tool call on each attempt.
@ANDROIDHASSAN
ANDROIDHASSAN requested a review from a team as a code owner November 25, 2026 18:51
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T18:53:18.950425Z 3790669 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@devin-ai-integration devin-ai-integration 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.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +375 to +379
if not message_stopped:
# the event stream ends quietly when the body is cut short
raise APIConnectionError(
"aws bedrock llm: stream ended before messageStop", retryable=retryable
)

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.

🟥 Truncated Bedrock replies leak into logs

When Bedrock ends early after sending text, FallbackAdapter logs the partial reply in the unmarked text_sent field. JsonFormatter writes that field unchanged to production logs.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The log line is real, but it is not specific to this change: FallbackAdapter (llm/fallback_adapter.py:386) logs text_sent for any provider whose stream fails after sending text, including Bedrock errors that already raise on main. This PR does make one more case reach it. #7832 already renames that field to lk.pii.text_sent, so I left it out of this diff and noted it under Scope.

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