Repository navigation
feat(llm): stream SageMaker and Bedrock answers as they are written - #672
Merged
Merged
Conversation
9E98
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ALjNbLobvPe7VwvDL8j7V3
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.
Description
Before: with an Amazon SageMaker endpoint or Amazon Bedrock as the LLM provider, a chat's answer arrived all at once at the end. Docs listed both streams under Refine later.
After: the answer streams as it is written. SageMaker uses InvokeEndpointWithResponseStream and Bedrock uses ConverseStream. An error the model hits mid-answer says so ("stopped mid-answer: ModelStreamError: …"). Keys whose IAM policy predates this (only
sagemaker:InvokeEndpointorbedrock:InvokeModel) keep working: the stream's 403 falls back to the whole answer, as before.How:
/invocations-response-streamwithX-Amzn-SageMaker-Accept: text/event-stream. The answer's AWS event stream is decoded with botocore's ownEventStreamBuffer, and its PayloadPart bytes are the container's server-sent events, read like any server's.converse_streamevents become OpenAI chat completion chunks (bedrock.Streamed).tests/aws_eventstream.pyencodes messages with the prelude, headers and CRCs that botocore checks. The SageMaker request is checked against botocore's model of the operation. Bedrock's goes through botocore's own HTTP path, with no Stubber.Motivation and Context
This is part of Adeel's auto scaling ask: SageMaker and Bedrock units serve Lens's chat, which should feel like any other model server.
Types of changes
!in the title)Checklist:
test_llm_sagemaker.pyandtest_llm_bedrock.py:test_a_streamed_answer_…,test_a_model_error_mid_stream_says_soandtest_keys_that_may_only_invoke_get_the_answer_whole.docs/configuration.md,docs/scaling.mdIAM lists and Refine later,docs/requirements.md).changelog.d/llm-aws-streams.md.Done questions (AGENTS.md)
llm._stream.Generated by Claude Code